



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Rules:
Suggestions:
Problem Possible Score 1 10 2 10 3 10 4 4 5 10 6 15 Total 59
#include <stdio.h> void func (int *p, int *q) { int temp = *p; *p = *q; *q = temp; }
int main(){ int p, q; int arr[10]; for (p = 0; p < 10; p++) arr[p] = p * p; p =5; q = 6; func(&p, &q); func(&arr[p], &arr[q]); printf (" %d squared is %d, %d squared is %d\n", p, arr[p], q, arr[q]); return 0; }
Answer: Correct answer is ‘‘6 squared in 25, 5 squared is 36’’.
“5 squared in 25, 6 squared is 36” (or flipped) gets 3 points. All other answers, no points
A. In C, if you want to return more than one value from a function you have to use Answer: pointers
B. (T or F) If a is an int variable, then *(&a) is always the same as a; Answer: T
C. (T or F) If a is an int * variable, then &(*a) is always just the same as a; Answer: Should be F; Because of unexpected behaviour in some compilers we accept either answer D. (T or F) If iarr is an int array of size 5, the expression iarr[5] refers to the last element in the array. Answer: False
E. (3 points, fill in values) Assuming the matrix, declaration int imat[5][7] what are i, j for the corners of the matrix in the expression imat[i][j]? where upper left is given by i = 0, j = 0: Lower right? i = , j = , Lower left? i = , j = Upper right? i = , j = , Answer: (4,6), (0,6) and (4,0), but accepted also (6,4), (6,0) and (0,4)
F. (cross out what is not applicable) If the array iarr is as described above, then the statement iarr[10] = 0 will result in a) possible Run-time error b) compile-time error with gcc -Wall c) no warnings. Answer: Cross out part b
G. (fill in blanks, 2 points. ) When passing in a function parameter to a higher-order function, the parameter type declaration must declare type and the type of the to the function. Answer: return type and parameter types
myarr[j] = 0;
printf("First %s\n", myarr); printf("Second %s\n", &myarr[j-5]);
}
(a) (5 points) What output does peculiar(10) produce? Answer: First ACEGIKMOQS Second KMOQS
Partial Credit: First character correct in each string, worth one point. Length correct for each string worth one point. Interval, sequencing, should be A-C-E etc. worth 2 points for all of question 5. (b) (5 points) What output does pecular(6) produce? Answer: First ACEGIK Second CEGIK
#include <stdio.h> #include <math.h>
#define MATSIZE 10
float matsum( ......) { /* Part a, complete the formal parameter declaration */
int i, j; float tot = 0; /* Part b, complete the body of this function */
return tot;
int main () {
float mymatrix ..... /* part c, complete declaration of matrix */ float normval; int i,j; for (i=0; i< 10; i++) for (j=0; j< 10; j++) mymatrix[i][j] = random();
normval = matsum (mymatrix); /* Computes Sum of all the matrix elements */
printf("\nThe normalizeation constant %.3f\n", normval);
/* Part d, complete the loop (s) for the normalization step of normalizing each entry of mymatri
normval = 0;
for (i=0; i< 10; i++) for (j=0; j< 10; j++) normval += mymatrix[i][j] ; /* Check the sum of the normalized matrix */
printf("\nThe normalized sum (should be 1) %.3f\n", normval);
};
Fill in the parts here:
(a) (2 points) Answer: float mymatrix[][MATSIZE] or float mymatrix[MATSIZE][MATSIZE]
float **mymatrix, or float mymatrix[][] gets one point. Additional/wrong declarations penalized (^12) point (b) (5 points) Answer: for (i = 0; i < MATSIZE; i++) for (j = 0; j < MATSIZE; j++) tot +=mymatrix[i][j];
Two points if both nested loops; 1 point more if some attempt at proper (but incorrect) addition in loop body. (c) (3 points) Answer: float mymatrix[MATSIZE][MATSIZE]
two points if 2-dimensional array (or ptr to ptr) declaration, but with wrong dimensions in some way (d) (5 points) Answer: for (i = 0; i < MATSIZE; i++) for (j = 0; j < MATSIZE; j++) mymatrix[i][j] = mymatrix[i][j] /normval; Two points if both nested loops; 1 point more if some attempt at proper (but incorrect) division in loop body.