1. (a) On a 32-bit machine, a variable of type float is _____ bytes in size. (b) On a 32-bit machine, the following struct a is _____ bytes in size. struct a { int x; double y; }; (c) What is the value of NULL? (d) What C function concatenates one array of characters to the end of another array of characters? 2. The output of the following program is : M E CS Please fill in the blanks : #include #include #include struct a{ char as1[2]; char as2[2]; }; struct b{ char bs1; char bs2; char bs3[2]; }; int main(){ ____________ p1 = malloc(_______________); struct b p2; strcpy(p1->as1,strdup("ME")); strcpy(p1->as2,strdup("CS")); memcpy(&_________, p1, _______________); p2.bs3[2]='\0'; printf("%c\n", p2.bs1); printf("%c\n", p2.bs2); printf("%s\n", p2.bs3); return 0; } 3. The dot product of two vectors is computed by the following formula: sum {from i=1 to n} a_i * b_i For instance, if a = [1 2 3] b=[4 5 6] dotProduct(a,b) = 1*4+2*5+3*6 = 4+10+18 = 32 Complete the following code to compute a dot product */ double dotProduct(double *a, double *b, int n) { int i; double sum; sum = 0; for(i = 0; i < n; i++) sum += ______________; return sum; } /* In the numerical methods project, you wrote a program to compute the solution of a linear system Ax = b using the Gauss-Jordan method. There are many other methods for solving liner systems, and they differ based on how long they take and how accurate a solution they produce. We want to find the residual r = b - Ax, which will probably not be 0 due to rounding errors. Please complete the following code to compute the residual. Note: A*x is computed in the following way: A = [1 2 3 x = [10 A*x = [dotProduct(first row of A, x) = [1*10+2*11+3*12 4 5 6 11 dotProduct(second row of A, x) 4*10+5*11+6*12 7 8 9] 12] dotProduct(third row of A, x)] 7*10+8*11+9*12] Using the a and b from above, a - b = [1 - 4 2 - 5 3 - 6] You may use the dotProduct function above. */ int main() { int i, j, n; double ** A; double *x; double * b; double *Ax; double *r; /* read the number of rows/cols in the matrix, n */ scanf(__________,__________); /* allocate memory for the matrices */ A = (double**) malloc(__________________________); for(i=0; i < n; i++) A[i] = (double*)malloc(______________________); /* we assume if you can allocate one, you can allocate the rest You need only complete the code for allocating memory for x */ x = (double*)malloc(_________________________________); b = (double*)malloc(...); Ax = (double*)malloc(...); r = (double*)malloc(...); /* read in the matrix A */ for(i = 0; i < n; i++) { for(j = 0; j < n; j++) scanf(_______________,___________________); } /* read in x */ for(i = 0; i < n; i++) scanf(_________________, _________________); /* read in b - we assume if you can read in x, you can read in b */ for(i = 0; i < n; i++) scanf(...); /* calculate Ax */ for(i = 0; i < n; i++) ____________ = _______________________________________________; /* calculate r */ for(i = 0; i < n; i++) ____________ = ____________________________; /* output r */ for(i = 0; i < n; i++) printf("%f ", r[i]); printf("\n"); } 4. The function readfile below reads the entire file into a buffer and returns a pointer to it. The size of the file (and hence the buffer) is returned in filesize. Please fill in the missing lines. unsigned char *readfile(FILE *s, int *filesize) { unsigned char *buf = NULL; int length = 0; int size = 16; int ret; while (1){ size += size; __________________________________ ret = fread(&buf[length], 1, size - length, s); if (ret < size - length){ _____________________________________ break; } length = size; } __________________________________ return ___________________________ } 5. What will be the output of the following program? #include void swap(char *a, int i, int j) { char t; t = a[i]; a[i] = a[j]; a[j] = t; } void display(char *a, int n) { int i; if (n == 0) printf("%s\n", a); else for (i = 0; i < n; i++) { swap(a, i, n-1); display(a, n-1); swap(a, i, n-1); } } int main(void) { char a[] = "abc"; display(a, 3); return 0; } 6. Implement a sorted insert. This function takes a sorted linked list as input, where are nodes are arranged in ascending order of 'data' values. It inserts the new node at the correct position in this sorted list based on the 'data' field of new node. struct node { int data; struct node* next; }; void SortedInsert(struct Node** head, struct Node* newNode) { // special case: when the insertion is to be performed at head if (*head == NULL || (*head)->data >= newNode->data) { ______________________; *head = __________; } else { // handle the general case // locate the node before the point of insertion struct node* temp = *head; while ((__________) && (____________ < newNode->data)) { temp = temp->next; } _________________; temp->next = newNode; } } 7. Fill in the missing lines below so that the program outputs: The answer is 10 (where 10 is computed as 4+3+2+1+0) after the user types "^C" 5 times. (Hint: you'll need to use setjmp and longjmp.) Your answers should not include any additional assignments to i or j. #include #include #include #include int i, j; jmp_buf Env; void int_handler(int dummy) { ______________; } int main() { _________________________ ; i = 1; j = 5; i = __________; j--; if (j >= 0) { while(1); }; printf("The answer is: %d\n", i); }