CSE 142 Wi01 Midterm 2 page 1 of 6 Answer all of the following questions. READ EACH QUESTION CAREFULLY. Answer each question in the space provided on these pages. Keep your answers short and to the point. There are 6 questions worth a total of 45 points. Good luck. 1. (3 points) Suppose we have two variables in a C program declared like this: int list[42]; int k; Complete the following loop so it stores 0 in all 42 elements of the array list. for (k = 0 ; k < 42 ; k++) { list[k] = 0; 2. (6 points) Rewrite the following as equivalent conditions that do not use the not (! ) operator. (The solutions can, of course, use the!= operator as needed.) a)!(a==2 && b==3) (a!=2 b!=3) b)!((a==b && b>10)!(a!=0 c<=3)) (a!=b b<=10) && (a!=0 c<=3)
CSE 142 Wi01 Midterm 2 page 2 of 6 3. (10 points) The input for this problem consists of a sequence of zero or more positive integers followed by a 0 that indicates the end of the input. Write a function longestsequence that reads the input and returns the length of the longest consecutive sequence of any single integer. For example, if the input were 4 35 35 35 35 12 9 9 35 5 0 the return value should be 4, because there are 4 consecutive occurrences of 35 in the input, and no number has a longer sequence. If the input were 12 15 3 9 0 the result should be 1. If there are no input numbers (i.e., the first thing read is the 0 that marks the end of the input), the function should return 0. YoushouldcallafunctionnamedreadInt to read the integers. Assume that it returns the next integer from the input sequence if there are no input errors, and otherwise detects the input error and terminates execution if one is found. Its prototype is: int readint(void); /* return the length of the longest consecutive sequence */ /* of identical numbers in the input. */ int longestsequence(void) { int longestrunsofar; int currentrun; int lastnum; int nextnum; lastnum = 0; longestrunsofar = 0; currentrun = 0; do { nextnum = readint(); if (nextnum==lastnum && nextnum!=0) { currentrun++; else { if (currentrun > longestrunsofar) { longestrunsofar = currentrun; currentrun = 1; lastnum = nextnum; while (lastnum!= 0); return longestrunsofar;
CSE 142 Wi01 Midterm 2 page 3 of 6 Second solution using the input strategy talked about in the 9:30 lecture. /* return the length of the longest consecutive sequence */ /* of identical numbers in the input. */ int longestsequence(void) { int longestrunsofar; /* length of longest run seen so far*/ int currentrun; /* length of current run */ int lastnum; /* previous # read from input */ /* (0 if no numbers read so far) */ int currentnum; /* current # read from input */ lastnum = 0; longestrunsofar = 0; currentrun = 0; currentnum = readint(); while (currentnum!= 0) { if (currentnum == lastnum) { currentrun++; else { if (currentrun > longestrunsofar) { longestrunsofar = currentrun; currentrun = 1; lastnum = currentnum; currentnum = readint(); return longestrunsofar;
CSE 142 Wi01 Midterm 2 page 4 of 6 4. (8 points) Write a function named printtriangle that prints a right triangle with straight edges on the bottom and on the right. This function should have a single integer parameter that specifies the height (number of rows) and width (length of the last row) of the triangle. Examples: printtriangle(4) * ** *** **** printtriangle(1) * void printtriangle (int size) { int row, col; for (row = 1; row <= size; row++) { col = 1; while (col <= size-row) { printf( ); col++; while (col <= size) { printf( * ); col++; printf( \n ); or, if you prefer for loops, void printtriangle (int size) { int row, col; for (row = 1; row <= size; row++) { for (col = 1; col <= size-row; col++) { printf( ); for (col = size-row+1; col <= size; col++) { printf( * ); printf( \n );
CSE 142 Wi01 Midterm 2 page 5 of 6 5. (8 points) Trace the execution of this program and do the following: a) Draw a diagram (boxes and arrows) showing the situation right before execution of the printf in function riddle. Your diagram should contain a box for each function showing its parameters and local variables, the values of all simple integer variables, and arrows for each pointer variable showing what it points to. b) Write down the output produced by the program. #include <stdio.h> int riddle(int a, int *b) { a = a + 2; *b = *b + a; /* diagram should show boxes and */ /* arrows when execution reaches */ /* this point */ printf("%d %d\n", a, *b); return *b; int mystery(int a, int b, int *c) { b = riddle(a, c); printf("%d %d %d\n", a, b, *c); return a; int main(void) { int a, b, c; a = 0; b = 1; c = 3; b = mystery(a, b, &c); printf("%d %d %d\n", a, b, c); 2 5 0 5 5 0 0 5 riddle a 0 2 b mystery a 0 b 1 c main a 0 b 1 c 3 5
CSE 142 Wi01 Midterm 2 page 6 of 6 6. (10 points) Assume that a herd of rabbits grows as follows: 1) 1/2 of the herd (rounded down) is always female, no matter how many rabbits there are. 2) Every month, each female produces 5 new rabbits. 3) Every month, 10% of the herd dies (but only after the new rabbits have been born) Write a function monthstotakeoverus that has one integer parameter giving the initial size of the herd. The function should return the number of months needed for the rabbit population to become greater than the population of the US. Assume for this problem that the US population is 280,000,000 and does not change. Also, assume that the initial size of the herd is always at least 2 (i.e. there is always at least 1 female rabbit). Finally, remember that the number of rabbits is always an integer (there are no fractional rabbits). int monthstotakeoverus(int initialpop) { int months = 0; int rabbitpop = initialpop; while (rabbitpop <= 280000000) { months = months+1; rabbitpop = (rabbitpop + (rabbitpop/2)*5) * 0.9; return months;