NAME: LAB TIME:
1.5em 1.5em
This is a closed book (and notes) examination. Answer all questions on the exam itself. Take the number of points assigned to each problem and the amount of space provided for your answer as a measure of the length and difficulty of the expected solution. The exam totals 100 points.
SCORE:
int a, *pa;
For each scanf() statement below indicate whether it would or would
not work correctly to read an integer value if the statement a = 0;
was initially executed.
scanf("%d", &a); scanf("%d", pa); scanf("%d", *pa);
For each scanf() statement below indicate whether it would or would
not work correctly to read an integer value if the statement pa = &a;
was initially executed.
scanf("%d", &a); scanf("%d", pa); scanf("%d", *pa);
typedef struct foo { int w; char ch; int rgInt[100]; struct foo *pFoo; } FooType; FooType rgFoo[50], foo, *pFoo; pFoo = (FooType *)malloc(sizeof(FooType));
What is the type of each of the following expressions? Note: some may result in errors.
pFoo
rgFoo[10]->ch
foo.rgInt[7];
pFoo->rgInt[12]
foo.w
text.o: text.c stack.h text.h gcc -c text.c -o text.o stack.o: stack.c stack.h gcc -c stack.c -o stack.o text: text.o stack.o gcc -o text text.o stack.o
Assume that the files stack.c, text.c, stack.h and text.h were created then the following make command is executed. Next the file text.h is modified. What compilations will occur when the same make command is reexecuted?
> make text
What is the output of the following program segment? For partial credit, show the contents of the stacks and the queue after each loop has executed (be sure and clearly label the stacks and queue).
Stack s1, s2; Queue q; int i, j; InitStack(&s1); InitStack(&s2); InitQueue(&q); for (i = 1; i <= 6; i++) { if (i%2 == 0) Push(i, &s1); else Push(i, &s2); } while (Pop(&j, &s1) >= 0) { if (j%3 == 0) printf("%d ", j); else AddQueue(j, &q); } while (Pop(&j, &s2) >= 0) printf("%d ", j); while (DeleteQueue(&j, &q) >= 0) printf("%d ", j); printf("\n");
#define MAXQUEUE 10 typedef struct queue { int rear; /* last slot used for insertion */ int front; /* next slot to delete */ int count; /* count of items in queue */ Item_type rgItem[MAXQUEUE]; } Queue;
Using these declarations show the code for AddQueue(), which adds the next element to the queue.
/* * AddQueue - add an item to the queue, return zero for success, -1 on error. */ int AddQueue(Item_type item, Queue *pqueue) { /* your code goes here */ }
#define MAXSTACK 10 typedef struct stack { int top; /* top of stack */ Item_type rgItem[MAXSTACK]; } Stack;
Using these declarations show the code for Peek(), which like Pop() returns the top element of the stack, but unlike Pop() does not actually remove the element from the stack.
/* * Peek - peek at top item on the stack, return zero for success, -1 on error. */ int Peek(Item_type *pitem, Stack *pstack) { /* your code goes here */ }
You must use the following declaration for storing a polynomial. Also shown is the declaration of three polynomial variables and calling ReadPoly() to read in two of the polynomials.
#define MAXCOEFF 10 typedef struct polytype { int wDegree; float rgCoeff[MAXCOEFF]; } Polynomial; Polynomial poly1, poly2, polyadd; ReadPoly(&poly1); ReadPoly(&poly2); AddPoly( ..... ); /* you need to fill in the parameters */
Write the procedure AddPoly() that could be used to add poly1 and poly2 together and store the resulting polynomial in polyadd. Be sure and indicate the parameter declarations for the procedure and how the procedure would be called in the above code.