Introduction to Computer Science and Programming in C, Homework 2. Instructor: Bert Huang Due 10/14/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 "homework2". Then, in the directory that contains your homework2 directory, compress homework2 using the command: tar -czvf bch2104_homework2.tgz homework2 Replace Bert's uni (bch2104) with yours. This identifies the submitted file as yours. This creates a single file (bch2104_homework2.tgz) that contains a compressed copy of your homework2 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. ******************************************************************************** 1. Written Section (Short answers). Write these electronically in a text file that you will include in the homework submission. ******************************************************************************** a) Examine the following code. Note that it is poorly written, not commented, the variable names show nothing about their purposes and the variable naming is confusing. Do not write code like this. That said, follow the code and for each line of the code, tell us what the values of the variables x and y are. For lines 3 and 4, show what happens each time the loop is run. Feel free to use English words and sentences to describe what happens in this code. 0 |/* Program begins now */ 1 | int x=3, y=1; 2 | 3 | for (x=0; x<2; x++) 4 | y++; 5 | 6 | if (y>0) { 7 | int x = -2000; 8 | x = y; 9 | } 10| /* Program ends here */ b) Describe the difference between a struct and a union. Give an example of when you would use a struct. Give an example of when you would use a union. ******************************************************************************** 2. Finding the max of an array. Submit your .c code file ******************************************************************************** arrayMax() Write a function that takes an array of floats and the length of the array, and returns the maximum value in the array. Use this main function to test your arrayMax() function: #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 :"); 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 */ answer = arrayMax(A,10); printf("The arrayMax function returned %f\n", answer); return 0; } ******************************************************************************** 3. Fibonacci Sequence. Submit your .c code file ******************************************************************************** Write 2 functions that each take a positive integer, N, as input and outputs the N'th Fibonacci number (THIS IS CHANGED. I originally wanted you to have the function output the whole sequence). The Fibonacci sequence starts with "1 1", and computes the next value by adding the previous two values. So the next value after "1 1" is 2. The beginning of the sequence is: 1 1 2 3 5 8 13 21... Write one version of the function using a loop with no recursion, and write another using recursion with no loops. Finally write a simple main that just calls the function with a command line input, and using a loop, outputs the whole sequence up to the user's input. The usage should look like: $ fibonacci 3 Loop: 1 1 2 Recursive: 1 1 2 $ fibonacci 10 Loop: 1 1 2 3 5 8 13 21 34 55 Recursive: 1 1 2 3 5 8 13 21 34 55 (note: both versions should produce the same output, but I want you to practice programming with loops and with recursion.) (note 2: The assignment has been changed slightly to make it a bit easier. Originally we wanted the functions to output the sequence, but now we just want the functions to return the n'th number, then have loops in your main() to output the sequence.) ******************************************************************************** 4. File input. Submit your .c code file ******************************************************************************** Write a program that reads in a text file with a (floating point) number on each line and outputs a file that describes the mean and variance of the numbers. Mean is just the average, and variance is the average squared difference from the mean. For example, the file might contain the following: 10.0 20.0 30.0 40.0 The mean is (10+20+30+40)/4 = 25. The variance is ((10-25)^2 + (20-25)^2 + (30-25)^2 + (40-25)^2)/(4-1) = 166.666 Your output file should then look like Mean: 25 Variance: 166.666 Feel free to embellish the output to make it prettier. (hint: you may need to read the file more than once.) (NOTE: The above formula for variance is the sample variance with an unbiased estimator where instead of the actual average squared error, we divide by (N-1) instead. For this homework, feel free to divide by N or N-1. If you divide by N, you get mean 25 and variance 125.)