CSC 105 Section:

Name:

Student ID:

 

Final exam CSC105 - Winter 1997

 

  1. Find the value of quack after each line:

int quack = 8 ;

quack += 5 ;

quack -= 6 ;

quack *= 10 ;

quack /=2 ;

quack %= 3 ;

 

2. What is the output of the following fragment for the indicated input (assume that ch is type int and that the input is buffered)?

The input is as follows:

Duralex [enter]

 

The fragment is as follows:

while( ( ch =getchar() ) != \n )

{

putchar(ch++) ;

putchar(ch++) ;

putchar(--ch) ;

}

 

3. Consider a book database in which you have to store the following information about each book:

title - a string of maximum 30 characters

publisher - a string of maximum 30 characters

author - a string of maximum 30 characters

ISBN - a long integer

price - a floating point number

  1. Define a structure able to contain the necessary information as defined above.
  2. Declare two variables book1 and book2 of the type book defined in a).
  3. Write a function which takes as an argument a pointer to a book structure and prints the title of the book and its price (prototype and definition).

4. Consider the declarations:

char string[] = { "This is a string" } ;

char * p1, * p2 ;

char c;

Which of the following instructions are correct? If the instructions are correct, describe their effect in sequence (i.e. when an instruction is executed you assume all (correct) previous instructions have already been executed).

p1 = string ;

p2 = &string[0] ;

c = *p1++ ;

p2 = p1 + 2 ;

*p2 = t ;

putchar(c) ;

*(p2-1) = a ;

p2 = &p1 ;

printf("%s\n", string) ;

 

5. Consider the following declarations:

struct cell

{

int key ;

struct cell *next, *prev ;

} ;

struct cell c1, c2, *p1, *p2 ;

Draw two pictures which will reflect the situation in memory before and after the execution of the following instructions (if they are correct). If you find an error of any type, explain it.

p1 = &c1 ;

p2 = &c2 ;

p2->key = 10 ;

p2->next = p1 ;

(p2->next)->key = 20 ;

p2->prev = NULL ; /* NULL is just 0 */

(p2->next)->prev = p1 ;

p1->next = NULL ;

 

6) Write a C program which will:

  1. Define two arrays of 20 integer numbers
  2. Read 20 number from the standard input into the first array
  3. Use the function defined above to calculate the difference of each two adjacent elements from the first array and store it in the correspondent element of the second array
  4. Print the content of both arrays

Example: If the content of the first array was 1,2,3,4,2, the content of the second array should be 1-2=-1, 2-3=-1, 3-4=-1, 4-2=2. The program should print 1,2,3,4,2 and 1,-1,-1,2. Note that the second array has one element less than the first one.