CS 1723, Data Structures Exam 1 (20) 1. Consider an array of structures declared by: #define MAXA 100 struct atag { char ch; int num; }; struct atag a[MAXA]; (a) There are three parts above: the first line, the next four lines, and the last line. Which part or parts actually allocates storage? (b) Is the second word struct required above? (c) Give a code segment that will initialize each ch field to a blank character and each num field to a zero. (d) Write a code segment that will find the sum of all 100 numbers in the num fields. (30) 2. Consider the following program: #include void main(void) { char t[] = "UTSA"; char *p; char *q; p = t; printf("Printing t: %?\n", t ); printf("Printing p: %?\n", p ); printf("Printing t[0]: %?\n", t[0] ); printf("Printing t[1]: %?\n", t[1] ); printf("Printing *p: %?\n", *p ); printf("Printing &t[0]: %?\n", &t[0] ); printf("Printing *(p+2): %?\n", *(p+2)); printf("Printing p+2: %?\n", p+2 ); printf("Printing *&p: %?\n", *&p ); printf("Printing &t: %?\n", &t ); printf("Printing &t[1]: %?\n", &t[1] ); printf("Printing &q[0]: %?\n", &q[0] ); } In each case, say whether a character is to be printed (with a %c format), a string is to be printed (with a %s format), or whether the expression is in error and should not be printed with either a %c or a %s format. Except for the third (error) case, say what will be printed. In other words, replace each %? with %c or %c and say what will print, except for those expressions in error. (This program compiles clean and runs with the appropriate substitutions for %?, except for two of the cases, which produce lint errors, or sometimes segmentation faults, no matter what is substituted for %?.) (50) 3. (a) Consider the following stack header file, stack.h: /* * stack.h -- stack header file, stacks char values */ char pop(void); void push(char); int empty(void); int full(void); Write a complete program which will read characters until encountering a newline character ('\n'), and will push each character on the stack as it is read. (You can use getchar() to read these, and of course push() to push them.) Finally, the program will use the stack (and the pop() function) to print the characters in the opposite order from which they were read in. Thus if your input is abcd, the output should be dcba, in that order. (After printing, your stack would be empty again.) Your program should be complete, except that you may assume the above header file exists and that some file stack.c source file exists to implement the stack. (You don't need to show code for stack.c yourself. Your program will not have direct access to this stack, but just to the functions in the header file.) You must use the full() function to make sure the stack doesn't overflow. In case the stack fills up, you should ignore the rest of the input . (b) Assume that you add the following function to your program in part (a): void pop_and_print(void) { char ch; if (!empty()) { ch = pop(); pop_and_print(); putchar(ch); } } This function would be called by pop_and_print() from your main function after reading and pushing the characters, but before any other popping or printing. Notice that this function calls itself (a recursive call). Assuming that abcd is read in and pushed on the stack, and assuming that this function is then called, what will this function print? Give as good an explanation as you can of what will happen if abcd is read in, the characters pushed, and pop_and_print() is called, including describing the recursive calls, the values stored in the local variable ch, and what characters will be printed and in what order.