### CS255 C Program Assignment 1 - Array and Bit manipulations in C

• Preparation

• To do this project, you must obtain a copy of the prepared files with these commands:

 ``` mkdir ~/cs255/c1 cp /home/cs255001/Handouts/c1/* ~/cs255/c1 ```

• Assignment

• Summary:

• The C program that you write in this project will be executed as follows:

 ``` main N (N is some integer < 32,000,000) ```

• The program will do the following:

 Compute all prime number that are ≤ N Print the number of prime numbers that was found in that range Print the value of the k1th prime number upto the k2th prime number found.

• Tasks to be done for this project:

• Create a C program file named primes.c and write (at least) the following 3 C functions in the file:

• A function void sieveOfE( int N ) that find all prime numbers that are less than or equal to N using the Sieve of Erathothenes algorithm.

 The prime numbers will be stored in a bit array that is a global variable defined in the header file header.h (The header file is explained below)

The Sieve of Erathothenes algorithm was discussed in CS170 and my notes can be found here: click here

• A function int countPrimes( int N ) that find the number of primes that are less than or equal to N.

 The countPrimes() function is called after running sieveOfE(N) that initializes the bit array. The countPrimes(N) function will read the information in the global bit array to count the prime numbers that are ≤ N.

• A function void printPrimes( int k1, int k2, int N ) that prints the prime numbers starting at the k1th prime and ending at the k2th prime for all primes ≤ N.

 The 1st prime is 2 The 2nd prime is 3 The 3rd prime is 5 The 4th prime is 7 And so on

Examples:

 ``` printPrimes( 1, 2, 10 ) will print: 2 3 printPrimes( 3, 4, 10 ) will print: 5 7 ```

• This is the content of the header.h file used in this project:

 ``` #ifndef EXTERN #define EXTERN extern #endif #define MAX 1000000 EXTERN unsigned int prime[MAX]; // Bit array EXTERN void sieveOfE( int N ); EXTERN int countPrimes( int N ); EXTERN void printPrimes( int k1, int k2, int N ); ```

I have defined a bit array inside the header.h file that you must use in your primes.c C program.

The prime[ ] array is the bit array that represent whether a number k is prime (or not prime).

I have also included the function prototype (declaration) of the 3 functions that you need to write.

• The bit array prime[ ]

• How to use the bit array variable prime[]:

 ``` Each array element prime[i] is an unsigned int (= 32 bits): prime[0] is an unsigned int (= 32 bits) prime[1] is an unsigned int (= 32 bits) prime[2] is an unsigned int (= 32 bits) .... The 32 bits in: prime[0] represents the number 0 to 31 bit 0 of prime[0] represents the number 0 bit 1 of prime[0] represents the number 1 ... bit 31 of prime[0] represents the number 31 prime[1] represents the number 32 to 63 bit 0 of prime[1] represents the number 32 bit 1 of prime[1] represents the number 33 ... bit 31 of prime[1] represents the number 63 and so on ```

• We have discussed how to implement a bit array in class.

• How to represent the fact that a number n is prime:

• Fact:

• It's easy to set all bits in an int variable to 0:

 ``` int a; a = 0; // Set all bits in a to 0 ```

• The Sieve of Erathothenes algorithm initially assumes that all numbers (in the range) are prime.

• Conclusion:

• It's easier (for initialization sake) to represent the fact that some number is prime as follows:

 ``` The number k is prime <===> the kth bit in the bit array prime[ ] is 0 ```

• Some useful functions that you should write to help you write the functions for this project

• void clearAll()

• The clearAll() function will make all elements in the prime[] array equal to 0

 You can clear all 32 bits in an int type variable at the same time by assigning the value 0 to the variable.

By clearing all bits in the bit array, we make sure that every number is a candidate to be a prime number

• void setBit(int A[], int n)

 The setBit(A, n) function will set (only) the bit in the A[ ] array that corresponds to the number n (This function was discussed in class, so I will not dwell on it).

• int isPrime(int A[ ], int n)

 The call isPrime(A, n) will return true (≠ 0) if the bit in the A[ ] array that corresponds to the number n is equal to 0 (i.e., the number n is prime) And the call will return 0 otherwise. (This function is similar to the testBit() function discussed in class).

Note: We have discussed bit array manipulation functions in class. Here is the webpage click here. You just need to adapt (may need to re-writing) them for this project.....

• Important difference in the interpretation of bits

• Normally, we interpret a bit value as follows:

 A bit = 1 means: something is true (e.g., some number is a prime A bit = 0 means: something is false (e.g., some number is a not prime

• In this program, we will use the following interpretation:

 A bit = 0 means: something is true (e.g., some number is a prime A bit = 1 means: something is false (e.g., some number is a not prime

Therefore:

• You must clear all bits at the start of the program

 This initialization will make all number be prime at the start

• You must set the corresponding bit in the bit array prime[ ] when the number k is not prime !!!

• To check if some number k is prime, you have to test if the corresponding bit is 0 !!!

• Warning

• Do NOT use:

 ``` for ( i = 0; i < N; i++ ) { ClearBit(A, k); } ```

to clear all the bits !!!

This is very inefficient.

You can clear all bits in an int typed variable using:

 ``` int x; x = 0; ```

• How to compile and run

• To compile:

 ``` gcc -g -c main.c (once only !!!) gcc -g -c primes.c (every time you make changes in the file primes.c) gcc -g -o main main.o primes.o (every time you make changes) ```

I strongly encourage you to write a Makefile for this project !

If you are successful, then all you need to do to compile the programs is typing: make....

• To run:

 ``` main N For example: main 10000 ```

Sample outputs:

 ``` main 100 Number of primes found = 25 47 53 59 61 67 71 73 79 83 89 97 main 10000 Number of primes found = 1229 9883 9887 9901 9907 9923 9929 9931 9941 9949 9967 9973 main 31999999 Number of primes found = 1973815 31999823 31999829 31999837 31999853 31999883 31999901 31999907 31999909 31999921 31999927 31999939 ```

• Warning:

• Make sure you stress test your code by running:

 ``` main 30000000 (N = 30 million !!!) Correct output is: Number of primes found = 1857859 29999873 29999881 29999887 29999899 29999903 29999923 29999927 29999941 29999947 29999989 29999999 ```

Also: main 30000000 must finish in less than 1 min !!!

If your program runs longer, the cause is usually poor bit manipulation implementation or worse: you did not implement the Sieve correctly; and we will deduct points

One common place is:

 ``` Clear all bits in the bit array using bit maniplutaions: for (i=0; i ```

Do not use this inefficient way to clear all bit.

The following assignment operation to an int variable:

 ``` int x; x = 0; ```

will clear all 32 bits in the int variable x. That will save you a LOT of time

• Debugging help

• Crashes:

 A C program can crash without giving you any hint on which statement causes the crash If your C program crashes, you must use a debugger to locate the crash site (= the statement that causes the crash)

• Turn in

• Pre-turnin check:

• If you wrote your project using your PC or MAC, then you'd better check if your code works on the MathCS machine first:

• Compile your project on a MathCS machine using:

 ``` gcc -g -c main.c gcc -g -c primes.c gcc -g -o main main.o primes.o ```

And run some tests.

Reason:

 C compilers are different on different platforms and may not be 100% compatible !!! Your program written on your PC/MAC may have compile errors on the MathCS machines !!!

• Warning:

• Your sieveOfE( ) and countPrimes( ) functions must not print any output !!!

We will need to test your code with values of N > 30000000; if your function prints test messages out, it will generate MEGA bytes of output !!!

• You must comment out print statements when you turn in your code or else:

 We will deduct 20 points !!!!        This is to encourage you to comment out the print statements; because we don't have time to edit out all the prints to grade !!!

• Use this command to turn in your primes.c C program:

 ``` /home/cs255001/turnin primes.c c1 ```

• If you have changed the header file header.h, turn the header file in using this command:

 ``` /home/cs255001/turnin header.h c1-h ```

 ``` /home/cs255001/turnin your-header-filename.h c1-x ```

• Extension request

• To request an extension for project c1, you must execute the following command before the deadline of the homework:

 ``` /home/cs255001/req-ext c1 ```

You request will be successful if you have not exceeded the maximum number of "free" (no-questions-asked) requests allowed