Computer Science 112a C and Data Structures Fall 1995 Midterm examination October 19, 1995 Name: 112a-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 writing a running C program. You will logon to grendel and type in, compile and debug three short C programs.Name the programs according to the instructions and submit them using submit Midterm. There may be more work than you can do in two hours. Do not panic: do first the problems that you consider to be the easiest, then allow time for the most difficult ones. Show all your work: you may get partial credit. Add extra sheets if necessary. 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) What is funcA computing? (one sentence suffices!). (3 points) What are the values printed by the 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. (10 points) Describe the structure of a C program that uses two stacks: a stack of pointers to characters and a stack of float numbers. Give the appropriate declarations of global variables and the prototypes of the functions to manipulate the two stacks. Do not include the code of the functions. 6.(6 points) Show the sequence of stack contents as the infix expression (x+y-z) * u is transformed into the postfix form. For this description use the following table, where Input shows the current symbol to be read from the input stream, Stack shows the contents of the stack at that step and Output shows what has been output so far. Input Stack Output 7. (6 points) Show the contents of the evaluation stack as you evaluate the postfix expression 1 2 + 4* 3 2 - +. Use the following table. Each row corresponds to a step during the evaluation process. Input shows the current item beeing read from the postfix expression. Stack gives the contents of the stack at that step. You may not need to use all of the rows., but if you need more, use extra an sheet. Input Stack Part 2. (50 points) 8. (15 points) Describe a C program that will take as input a sequence of decimal digits 0,1,2,...,9 (e.g. 23433233004) and will count the number of occurences of the each digit. In the previous example, 2 occurs twice, 3 four times, etc. The input stream is terminated by a special character of your choice (e.g a dot) and blancs and newlines are ignored. Name your program count.c. Before submitting it, insert a comment in it tellingwhat is its status: compiles or not, runs correctly or not. 9. (15 points) Write a C program which reads two strings of characters s1 and s2 into two arrays of characters allocated at run time via malloc. The porgram will prompt the user asking for the size of each string, then allocate memory for it, then read it. Afterwards, it will concatenate the two strings: create a new string s consisting of s1 followed by s2. You will have to also allocate space for s before concatenation. Name your program concat.c . Before submitting indicate with comments whether your program compiles or not, executes correctly or not. 10. (20 points) Write a function: int substring(char *s, char*t) which returns 0 if s points to a string contained in the string pointed to by t, -1 otherwise. For example: substring("ell", "Hello") returns 0 substring("Hll", "Hello") returns -1. For this part, provide only the pseudo-code for the function. You may not have time to write and type in a whole program executing this function. (If you do, that is extra credit). You will also get extra credit if you get to type in a file substring.c containing the C code for the function (and if that is correct!). I will insert your function into a main program and test it.