CS 30 Winter 2008 Midterm 1. Open book and notes (no laptops or neighborly help). Write you answers on the exam. Use the back if needed. Write CLEARLY. Put you name on each page. Problem 0 (after all this is C): Suppose the file structure in a Unix file system is as shown in the figure, and that you are currently in the home directory. Show the commands you would use to do the following list of things. Each command should assume that the tasks described before it have already executed. 4pts for each task. a. move to directory past b. make a copy of grades and call it oldgrades c. remove file grades d. move the file books in the directory past to directory food e. change the protection on the file old so that the world (anyone on that computer) can read, but not change, it. Answer: I don't have the figure with me now. -------- Problem 1: What is printed out in the following program? 20pts. #include main() { int j = 10; int k, i = 0; if (i) i--; printf("%d\n",i); ++i; if (i) i++; printf("%d\n",i); if (j < 8 && i > 0) j += 3; printf("%d\n",j); for (i = 0, k = 3; k < 18; i++, k = k+2){ switch(k%3) { case 0: printf("%d --> case 0\n",k); i++; break; case 1: printf("%d --> case 1\n",k); i++; break; case 2: printf("%d --> case 2\n",k); i++; break; case 4: printf("%d --> case 4\n",k); i++; break; default: printf("%d --> default",k); } } printf("%d %d\n", i,k); } Answer: copy and paste this and run it to see. ------- Problem 2: What is printed out in the following program? 15pts #include main() { int i = 1; int j = 0, isav = 0; while (i < 40) { i = i + isav; isav = i; j++; printf("%d, %d\n", i, j); } } Answer: copy and paste this and run it to see. ------------ 3. Rewrite the program in Problem 2, to produce exactly the same output, but use a for loop instead of a while loop, and use only variables i and j (not isav and not any new variables). You can use a multiplication in one of the statements. To do this problem, you have to understand what the previous program is computing, not just a line-by-line understanding of it. 20pts Answer: #include main() { int i = 1; int j = 0; for (j = 1; j <= 7; j++) { printf("%d, %d\n", i, j); i = i*2; } } ---------- Problem 4: Several lines below are missing code. Fill in the needed codes as explained in the comments and code. 20pts #include main() { int i,j, numlines; float f; char c; printf("How many input lines will there be: "); scanf( ); /* write C code to read the answer into variable numlines */ for (i = 1; ; ) { /* finish this line so that the loop executes numlines times. */ printf ("Input a new line with three items seperated by spaces, one integer, one floating point number, one character\n"); scanf( ); /* write C code to read in the three items */ printf ("The three items for this line are %d %f %c\n", j,f,c); /*In the next line, finish the code so j is printed with a minimum of 5 spaces, left justified; f is print with a minimum of 8 spaces, with two digits after the decimal point, and right justified; and there is at least one space after f, before c */ printf ("Printed more nicely the items are: ", j, f, c); } } Answer: #include main() { int i,j, numlines; float f; char c; printf("How many input lines will there be: "); scanf("%d", &numlines); /* write C code to read in the answer to variable numlines */ for (i = 1; i <= numlines; i++) { printf ("Input a new line three items seperated by spaces: one integer, one floating point number, one character\n"); scanf("%d%f %c", &j,&f,&c); /* C code to read in the three items */ printf ("The three items for this line are %d %f %c\n", j,f,c); /*In the next line, finish the code so j has a minimum of 5 spaces and is printed left justified; f has 6 spaces, with two digits after the decimal point, and is printed right justified; there is at least one space after f, before c */ printf ("Printed more nicely the items are: %-5d%8.2f %c\n", j, f, c); } } --------------- Problem 5: (30pts) Write a program that asks the user to input two positive integers into variables i and j. Then for each pair of numbers, where the first is in the range 1 through i, and the second is in the range 1 through j, the program calculates and prints out the product of those two numbers; the first number divided by the second, with two digits after the decimal point; and if the two numbers differ by exactly 2, it prints out a message. The program uses a new line for each pair of numbers. For example, if i is input at 3 and j is input as 4, the program should print out: 1,1 1 1.00 1,2 2 0.50 1,3 3 0.33 differ by 2 1,4 4 0.25 2,1 2 0.50 2,2 4 1.00 2,3 6 0.66 2,4 8 0.50 differ by 2 3,1 3 3.00 differ by 2 3,2 6 1.50 3,3 9 1.00 3,4 12 0.75 Answer: #include main() { int i,j,ii,jj,prod; float ratio; printf("Please input two positive integers \n"); scanf ("%d%d", &i, &j); for (ii = 1; ii <= i; ii++) { for (jj = 1; jj <= j; jj++) { prod = ii*jj; ratio = (float) ii /jj; printf("%d,%d %d %.2f", ii, jj, prod, ratio); if (((ii - jj) == 2) || ((jj-ii) == 2)) printf(" differ by 2"); printf("\n"); } } }