Introduction to Computer Science and Programming in C, Homework 3. Instructor: Bert Huang Due 11/11/08 before class begins at 1:10 PM. ******************************************************************************** Submission procedure: From this assignment on, you must follow this procedure or you will receive a grade of 0 for the whole assignment. ******************************************************************************** Keep your written section separate from your programming files. Include a README file that lists the files you are submitting and what each one is. Move the writeup for the written questions and your code for the programming problems you plan to submit (writeups and code) into a separate directory named "homework3". Then, in the directory that contains your homework3 directory, compress homework3 using the command: tar -czvf bch2104_homework3.tgz homework3 Replace Bert's uni (bch2104) with yours. This identifies the submitted file as yours. This creates a single file (bch2104_homework3.tgz) that contains a compressed copy of your homework3 directory (and its contents). (For more information on the "tar" command, type "man tar"). Copy your work from your cunix account using an SFTP (secure file transfer protocol) program. Upload it to Courseworks. From the Courseworks homepage, go to the Class Files. There should be a Homework #2 folder in the Shared Files section. Post your file in the Homework #2 folder. This folder is only viewable by the TA's and the instructor, so don't worry about other students seeing your submitted file. ******************************************************************************** 0. Voting (not for credit) ******************************************************************************** If you are a citizen of the United States, I encourage you to vote. ******************************************************************************** 1. Written Section (Short answers). Write these electronically in a text file that you will include in the homework submission. ******************************************************************************** a) Describe in your own words why the standard C libraries help improve portability of the C programming language. b) In big-O notation, what is the running time of the arrayMax() function from the homework 2 solutions? c) Describe in your own words what the * and & operators in C are and how they are different. ******************************************************************************** 2. Bit counter. Submit your .c code file ******************************************************************************** (Exercise 11-4 from Practical C Programming, Steve Oualline) "Write a program that counts the number of bits set in an integer. For example, the number 5 (decimal), which is 0000000000000101 (binary), has two bits set." More specifically, write a function, countBits() that takes an int as an argument and returns an integer count of the number of set bits. Then write a main function that reads an integer from the command line and outputs the return value of countBits() to the screen. ******************************************************************************** 3. Array addresses. Submit your .c code file ******************************************************************************** In this programming assignment we will do a small experiment to see how C organizes arrays in memory. Write a program that initializes three arrays: - an array of 10 ints - an array of 10 floats - an array of 10 chars Now, for each array, write a program that prints the memory addresses of all 10 entries of the array. It will be useful to use the printf conversion for unsigned integer, "%u". Finally, in your writeup, include some brief discussion about the numbers that you printed out and what they tell you about how C stores the array. ******************************************************************************** 4. Sorting. Submit your .c code file ******************************************************************************** Write a sorting function to complete the code below. The code below creates a random array and passes it to the sort function you will write. Your code should put the entries in order from least to greatest. Feel free to use any sorting algorithm you like, as long as it works. Example output is shown below the code: #include #include #include /********************Write your code here********************/ /*****************End of your code***************************/ int main() { float A[10], answer; int i; srand (time(NULL)); /* Seed the random number generator with the time */ printf("The input array is:\n"); for (i=0; i<10; i++) { /* rand() returns a random int. We want a random float, so divide by 32767 */ A[i] = (float)rand()/32767.0; printf("%f ", A[i]); } printf("\n"); /* finish the line */ mySort(A,10); printf("After sorting, the array is:\n"); for (i=0; i<10; i++) { printf("%d ", A[i]); } printf("\n"); return 0; } /* End of code Example output: The input array is : 0.943724 0.928800 0.752251 0.662069 0.194739 0.544939 0.824610 0.118503 0.070070 0.335917 After sorting, the array is: 0.070070 0.118503 0.194739 0.335917 0.544939 0.662069 0.752251 0.824610 0.928800 0.943724 */