CPSC 206 507 Final Exam

Total Page:16

File Type:pdf, Size:1020Kb

CPSC 206 507 Final Exam

CPSC 206 – 507 Final Exam Spring 2006

SID/SSN:

Score Possible Points

Part One 30 pts

Part Two 40 pts

Part Three 20 pts

Part Four 20 pts

Total 110 pts

1 PART ONE (30 points)

1. What is the output of the following code?

#include #include

#define MAXSTRING 100

main() { char c=’a’, *p, s[MAXSTRING];

P=&c; printf(“%c%c%c “,*p, *p+1, *p+2); strcpy(s, “ABC”); printf(“%s %c%c%s\n”,s *s+6, *s+7,s+1); strcpy(s,”she sells sea shells by the seashore”); p=s+14; for ( ; *p !=’\0’; ++p) { if (*p == ‘e’) *p=’E’; if (*p == ‘ ‘) *p=’\n’; } printf(“%s\n”, s); }

int f(int a) { return (a + a); }

int g(int *x) { return (*x + *x++); }

int main(void) { int i = 5, j = 6;

printf(" f = %d\n", f(i + j)); printf(" g = %d\n", g(&i));

i = i + 2;

printf(" g = %d\n", g(&i)); printf(" g = %d\n", g(&j)); printf(" i = %d j = %d\n", i, j); return 0; } 2 3 2. What is the output of the following code?

#include

int go_on (int x) { return (x++ < 5); }

int tap(int y) { int i;

for ( i = y; i--;) printf ("tap "); printf ("\n"); }

int main(void) { int x, i = 0;

while (go_on(x)) tap (++x); return 0; }

4 3. What is the output of the following code?

#include

void swap (int *m, int *n) { int temp = *m;

*m = *n; *n = temp; }

int main (void) { int a[4] = {2,7,4,8}; int i, j;

for (i = 3; i > 0; --i) { for (j = 0; j < i; ++j) if (a[j] < a[j+1]) swap (&a[j], &a[j+1]); for (j = 0; j < 4; ++j) printf ("%d ",a[j]); printf ("\n"); } return 0; }

5 PART TWO (40 points). Each question is worth 5 points. Short Answers.

4. Where does C code begin execution?

5. Given a 5 × 3 × 4 array A, in C, what is the linear sequential element number of A[3, 2,3] ?

6. Define: A. Enumerators

B. Typedef

7. Fill in the table below with the correct values for each of the statements. (Assume the variables are initialized with the given statements and that the statements in the table are executed in the order given.)

If an expression is not valid, indicate this in the value column.

int i = 2, j = 3, *p = &i, *q = &j, *r; double x;

Expression Value p == & i

p = i + 9 * * & p

r = & x

9 * * p / * q + 5

6 8. What is wrong with the following line of code?

int *p = &i, i = 3;

9. Simplify the following statement:

q = *&v;

10. Explain the difference between call-by-value and call-by-reference.

11. Tell me what you know about char, int, float and double.

7 PART THREE (20 points).

12. Write a function to compute the average of the values of an array, given that the header for the function is as follows:

int arr_avg(int *A[], int n); /* where n is the size of the array */

8 PART FOUR (20 points).

13. Write a C function to calculate the first 10 terms of the infinite series below. Use another function subprogram to calculate the factorial. Print each individual term and the summation of the terms. Also print the sin(z) as calculated by the library function.

sin(z) = z – z3/3! + z5/5! – z7/7!...

9

Recommended publications