Quiz 2

CSE1320 sections 003 and 501                    ANSWERS                                                 Spring 2002

Name: Key                                                              Both versions of quiz are included here

# Give the correct storage classes for the following descriptions (1 pt each)

automatic      Not initialized, available until end of a function, local to a function

extern         Automatically initialized, available until program end, global to all files

register        Not initialized, local to a function, may be ignored by the compiler

static          Automatically initialized, available until end of program, local to a function

typedef        Associates an identifier with a type, available until end of program

Write the correct syntax for the following: 3 pts

a variable named temp that is a pointer to a function that takes three parameters, a float, an int, and a char, and returns type void

void (*temp)(float, int, char);

a variable named temp that is a function that takes three parameters, a float, an int, and a char, and returns type pointer to void

void *temp(float, int, char);

Give the result returned by the following cryptic function if a = 3.42 and b = 4. 3 pts

#include <stdlib.h>                                /* using the absolute value function abs */

int check (float a, int b)

{

return (a - b) == abs(a-b)) ? a + b : abs(a – b) ;

}

Because a = 3.42 and b = 4, then a – b = -0.58 and abs(a – b) = 0.58. Thus the test is false and the second choice is used, so check returns abs(a – b) truncated to an integer. Check returns 0.

#include <stdlib.h>                                /* using the absolute value function abs */

int check (float a, int b)

{

return (b - a) == abs(a-b)) ? a + b : abs(a – b) ;

}

Because a = 3.42 and b = 4, then b – a = 0.58 and abs(a – b) = 0.58. Thus the test is true and the first choice is used, so check returns a + b truncated to an integer. Check returns 7.