|
|
|||
![]() |
Department of Engineering |
| University of Cambridge > Engineering Department > computing help |
)
void binary(unsigned int number){
/* print decimal `number' in binary */
...
}
Then write a main routine to test it. Don't worry about
leading zeroes too much at the moment. Note that binary
returns a void, i.e. nothing.
void base(unsigned int number, unsigned int base){
/* print decimal `number' to the given `base' */
...
}
Then write a main routine to test it.
#include <stdio.h>
#include <stdlib.h>
#define PRIME 1 /* Create aliases for 0 and 1 */
#define NONPRIME 0
int numbers[1000];
void mark_multiples(int num){
/* TODO: Set all elements which represent multiples of num to NONPRIME. */
}
int get_next_prime(int num){
/* find the next prime number after `num' */
int answer;
answer = num+1;
while (numbers[answer] == NONPRIME){
answer= answer +1;
if (answer == 1000)
break;
}
return answer;
}
main(){
int i;
int next_prime;
/* TODO: Set all the elements to PRIME. Remember, the 1st element is
numbers[0] and the last is numbers[999] */
/* TODO: 0 and 1 aren't prime, so set numbers[0] and numbers[1]
to NONPRIME */
next_prime = 2;
do{
mark_multiples(next_prime);
next_prime = get_next_prime(next_prime);
} while(next_prime < 1000);
/* TODO: Print out the indices of elements which are still set to PRIME */
exit(0);
}
The `TODO' lines describe what code you need to add in.
You can speed up this program considerably by replacing 1000
where appropriate by something smaller. See page
for details.