/* 1 */ #include /* 2 */ #include /* 3 */ #include /* 4 */ void run_sieve(char *
ID: 3653191 • Letter: #
Question
/* 1 */ #include /* 2 */ #include /* 3 */ #include /* 4 */ void run_sieve(char *, int); /* 5 */ void print_primes(char *, int); /* 6 */ int main() { /* 7 */ int i, top; /* 8 */ printf("compute primes up to: "); /* 9 */ scanf("%d", ⊤); /* 10 */ if (top < 2) { /* 2 is the lowest prime */ /* 11 */ printf("must be at least 2 "); /* 12 */ return 1; /* error exit */ } /* 13 */ top++; /* want top to appear as index in array */ /* 14 */ char *a = malloc(top*sizeof(char)); /* allocate array a[] */ /* 15 */ for (i = 2; i < top; i++) /* 16 */ a[i] = 1; /* initialize all indices as "potential primes" */ /* 17 */ run_sieve(a, top); /* 18 */ print_primes(a, top); /* 19 */ return 0; /* just being compulsive about a return value for main */ }Explanation / Answer
#include #include #include void run_sieve(char *, int); void print_primes(char *, int); int main() { int i, top; printf("compute primes up to: "); scanf("%d", &top); if (top < 2) { /* 2 is the lowest prime */ printf("must be at least 2 "); return 1; /* error exit */ } top++; /* want top to appear as index in array */ char *a = (char *)malloc(top*sizeof(char)); /* allocate array a[] */ for (i = 2; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.