/****************** ** sample array program ** ** the Sieve of Erostothenese ** this progam finds and prints all the prime numbers ** between 2 and 100 (MAX) ** ** the indexes into the arrary represent the integers ** ** DL FREY ** 11/20/98 ** *********************************/ #include #define MAX 1000 #define TRUE 1 #define FALSE 0 main () { /* declare an array that contains MAX integers */ int primes[MAX]; int i, j, columns; /* initialize each element of the array to TRUE */ for (i = 0; i < MAX; i++) { primes[i] = TRUE; } /* 0 and 1 are NOT primes, by definition, so ** make them FALSE */ primes[0] = primes[1] = FALSE; /* for each integer.... */ for (i = 2; i < MAX; i++) { /* check all the integers that are larger ** any integer, j, that is a multiple of i ** is NOT prime */ for (j = i + 1; j < MAX; j++) { /* this statement checks if j is evenly divisible by i ** i.e. the remainder is 0... if so j is NOT prime */ if ((j % i) == 0) { primes[j] = FALSE; } } } /* now print all the primes ** in neat columns, 10 per row */ columns = 1; for (i = 0; i < MAX; i++) { if (primes[i] == TRUE) { /* using %6d gives us columns that are neatly aligned */ printf ("%6d", i); /* see if we have printed 10 columns ** is so, print a newline and go to the next row ** and start counting columns over again */ if (++columns > 10) { printf ("\n"); columns = 1; } } } /* print a newline at the end ** for the last row */ printf ("\n"); }