In-Class Practice

Eratosthenes


What is the exact out of the following program?  Show your intermediate work.

 // Eratosthenes
 //
 #include <stdio.h>
 #define N 15
 int erat(int a[], int n);

 int main(void)
 {
        int i, k, a[N];

        k = erat( a, N );
        for(i = 0; i < k - 1; i++) {
                printf("%d-", a[i]);
        }
        printf("%d\n", a[k-1]);
        return 0;
 }

 int erat(int a[], int n)
 {
        int i, j;

        for(i = 2; i < n; i++)
                a[i] = 1;
        for(i = 2; i*i < n; i++) {
                if (a[i] == 1) {
                        for(j = i; j*i < n; j++) {
                                a[i*j] = 0;
                        }
                }
        }
        j = 0;
        for(i = 2; i < n; i++)
                if (a[i] == 1) {
                        a[j] = i;
                        j++;
                }
        return j;
 }



   Printer Friendly Version of this Page print this page     Top  Go Back to the Top of this Page



  Designed by Chris Szalwinski   Copying From This Site   

Creative Commons License