Question 1. (pointer and structures) What is the output of the following program? #include #include #include struct sample{ int a; int b; char name[21]; }; void print_struct (const struct sample *p){ printf("a : %d\nb : %d\nname : %s\n", p->a, p->b, p->name); } int main(){ struct sample * p = malloc(sizeof(struct sample)*2); struct sample *q; p->a=10; p->b=11; strcpy(p->name,"cs240"); print_struct(p); (p+1)->a=12; (p+1)->b=13; strcpy((p+1)->name,"midterm"); print_struct((p+1)); q = (p+strlen((p+5%2)->name)%2); print_struct(q); return 0; } ================================================================ Question 2. (casts) What is the output of the following program? #include int main() { char array[10] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}; char *cptr = (char *)&array; int *iptr = (int *)&array; cptr += 2; iptr += 2; printf("%c\n", *cptr); printf("%c\n", *(char *)iptr); cptr = (char *)(((int *)cptr) + 1); iptr = (int *) (((char *)iptr) + 1); printf("%c\n", *cptr); printf("%c\n", *(char *)iptr); } ================================================================ Question 3. (pointers) What are the values of i,j,ptr, *ptr, pptr, and **pptr after executing the following (write '?' if the value is unknown): int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = 8; **pptr = 9; *ptr = &i; *ptr = 2 ================================================================ Question 4. (unions) Please complete the following program to correctly output a student's grades. The output should be: Chemistry: 72/100 Computer Science: 100/100 Underwater Basket Weaving: F #define PASS_FAIL 1 #define STANDARD 2 typedef union { int points; /* out of 100 */ char pf; /* 'P' for pass, 'F' for fail */ } grade; typedef struct { int type; char name[50]; grade g; } course; void printGrades(course *courses, int numCourses) { int i; for(i = 0; i < numCourses; i++) if(__________________________________) printf("%s: %i/100\n", courses[i].name, courses[i].g.points); else ____________________________________________________________ } int main() { course cArray[3]; strcpy(cArray[0].name, "Chemistry"); strcpy(cArray[1].name, "Computer Science"); strcpy(cArray[2].name, "Underwater Basket Weaving"); ________________________ ________________________ ________________________ cArray[0].g.points = 72; cArray[1].g.points = 100; cArray[2].g.pf = 'F'; printGrades(cArray, 3); } ================================================================ Question 5. (malloc) The strcatX function returns a new string by concatenating the two strings provided as arguments. Please fill in the missing lines. char * strcatX(const char *s1, const char *s2) { char *s = malloc(________________________); __________________________ __________________________ return s; } ================================================================ Question 6. (function pointers) What is printed by the following program? Write '?' if it would print garbage. #include void add(int *arg1, int *arg2) { *arg1 += *arg2; } void mul(int *arg1, int *arg2) { *arg1 *= *arg2; } void sub(int *arg1, int *arg2) { *arg1 -= *arg2; } void (*getOp(int op))(int *, int *) { switch(op) { case 0: return &add; case 1: return &mul; default: return ⊂ } } int main() { int i; int x = 2, y = 3; for(i = 0; i < 4; i++) { getOp(i % 3)(&x, &y); printf("x = %d ... y = %d\n", x, y); } } ================================================================ Question 7. (code comprehension) The following function takes as argument two strings hay and needle. The function will return 1 if the string needle is contained in the string hay, or 0 if hay does not contain the string needle. Please fill in the missing pieces. NOTE: Your solution should NOT use any string functions such as strcpy, strlen etc. int contains(char * hay, char * needle) { int i, j; // for all positions of hay i = 0; // Have not come to the end of string hay while (________) { // Check if at needle matches at this position in hay j = 0; while (hay[____] != 0 & needle[___] != 0 && ______________) { // Match this character. Go to next one __________; } if (needle[j]==0) { // Found match return 1; } // perform search from next position in hay ___________; } return 0; } int main(int argc, char **argv) { if (argc<3) { printf("Usage: contains hay needle\n"); exit(1); } if (contains(argv[1], argv[2])) { printf("Yes\n"); } else { printf("No\n"); } }