Computer Science 112b C and Data Structures Spring 1995 Midterm examination March 16, 1995 Name: 112b-ax account: The exam is open book: you may consult your notes, books, even computer programs that you have written before. However you may not consult with your colleagues. All the work should be yours. The exam has two parts. Each should take you roughly one hour, but if you finish earlier on part 1 you can start part 2 right away. Provide the answers for Part 1 questions in the empty spaces on the exam sheet. Part 2 involves two stages. First, you should give a rough description in C of the problem. Use the empty space on the exam sheet for this part. Then, you will logon to grendel and type in, compile and debug the C program for solving the problem. Name the program according to the instructions and submit it using submit Midterm. Part 1. (50 points) 1. (7 points) You are given the following C program: #define MAX 10 int array[MAX]; main() { int i; for (i=0; i< MAX; i++) /* 1*/ { array[i]= i; printf("array[%d] = %d\n",i,array[i]); } } Replace the for loop /* 1 */ with a while loop, so that the program will still be doing the same thing. 2. You are given the following C program: #include int k = 2; int j = 6; main() { int i=3; int j; j = funcA(i); printf("%d",j); j=funcB(k); printf("%d",j); } int funcA(int j) { printf("%d",j); return(2*j); } int funcB(int j) { if (j == 1) return(1); return(j*funcB(j-1)); } (3 points) For each occurrence of the j variable, write next to it whether that is referring to: 1. the global variable j; 2. a parameter to some function; 3. a local variable j (and explain: local to what function?) (3 points) Explain what will go into the three segments of the file a.out that result from compiling this C program. code segment: data segment: stack: (6 points) Show the contents of the stack segment while this program is executing. You will start with an empty stack, then each time a function is called, show the name of the function, its parameters with their current values and the local variables. Redraw the stack each time it is modified (do not write and erase or overwrite!). (3 points) What is funcA computing? (one sentence suffices!). (3 points) What are the values printed by the two printf's of this program? 3. (6 points) Write the postfix form for the following arithmetic expression: (((a+b)*(c-d))/((a/c)-(b+c))) 4. (6 points) What is the value of the following arithmetic expression written in postfix form (assumming that we only use one-digit decimal integers): 12+3*23+- 5. (2 points) Draw the list structure, using the cell notation, for the following list: { A,C,D,G}. (4 points) Show the modification of the links when the new item B is inserted in alphabetical order in this list (i.e. after the element A). (4 points) Show the modification of the links when the item D is removed from the list. 6. (3 points) Which type of data structure would you use to model the following? a. Customers entering and leaving a bank. b. Piles of lunch trays in a school cafeteria. c. Cars waiting in line to pay a toll. Part 2. (50 points) 6. Describe a C program that will perform the following operations: a. initialize a stack of integers and a stack of characters. b. will ask the user to choose whether she wants to work with integers (I) or characters (C). c. then will sit in a loop, at each iteration asking the user what to do next. The choices are: A item => push item on the stack. B => pop the top of the stack. D (only for the stack of integers): => pop the two top elements of the stack and add them. Then push the result back on the stack. P => print the contents of the stack. Q => Quit the loop (and the program). Describe in C the global and local variables, main function and the functions that you will use in this program. For this part, the emphasis is on design, not on syntax, so do not be worried if you misplaced or forgot a semi-colon. But the program should be complete, with all the functions necessary for the manipulation of the stacks. You are allowed to use the code that I gave in class for the manipulation of stacks (push and pop), but be careful with using the proper data types. Use the remaining space on the exam sheet for describing this program. After you are done with this part, logon to grendel, type in your program (you are also allowed to copy parts of already exixting code), compile and execute it. The grade for this part is computed as follows: Design part, total of 36 points: ¥ Correct structure of the main program, with loop and choices correctly implemented: 8 points. ¥ Correct implementation of the D option on the user's menu (for adding up the top two elements in the integer stack): 8 points. ¥ Correct implementation of the function to print the contents of the stack: 10 points. ¥ Correct implementation (definition of global data and of the appropriate pop and push functions) for the two sets of stack functions (char and integer): 10 points. Implementation part, total of 14 points: ¥ Program compiles: 3 points. ¥ Program runs correctly for at least the pop, push options: 3 points. ¥ Program runs correctly for the P option: 4 points. ¥ Program runs correctly for the D option: 4 points. As you can see, the emphasis is on design - so please spend the time carefully designing your program before typing it in.