final exam, c programming - san jose state...

5

Click here to load reader

Upload: dolien

Post on 06-Feb-2018

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Final Exam, C programming - San Jose State Universitybeeson/courses/cs49/Exams/Final2CS49Spring2010... · Final Exam, C programming May 24, 2010 Rules: Open book, open notes, open

Final Exam, C programming

May 24, 2010

Rules: Open book, open notes, open any printed or handwritten mate-

rial. No electronic devices (except a music player). If you use a music playernobody else must be able to hear it and you must not use its controls during

the exams, just put it on shuffle. For the exam assume that the basic datatypes of C have the sizes they actually have on all common compilers today,even though the ANSI standard does not specify those sizes. Five points

per problem, 21 problems, total 105 points. Solutions will be posted to thewebsite Tuesday, and grades on Wednesday or Thursday.

1. The largest double is about how big?

(c) About 10308

2. Suppose that we want to print a table of items and prices. The items are

given in an array items of strings and the prices are in an array prices ofdoubles. Write code that will print the table in two columns, with the items

left-justified in their column, and the decimal points of the prices verticallylined up, with exactly two digits to the right of the decimal point. Leading

zeroes are printed only for prices less than one dollar. You do not need

to know how many prices and items there are to write this code,

so do not introduce a number or variable for that. Three of the

five points are for correctly handling this issue.

for(i=0;i<sizeof(items)/sizeof(char*);i++)

printf("%16s%6.02lf\n",items[i],prices[i]);

3. Declare a function average to compute and return the average element ofan array of integers. You do not have to write the function, just declare it

properly. Answer with one line only.

double average(int *x, int n) // you must pass the size as well as the base address

1

Page 2: Final Exam, C programming - San Jose State Universitybeeson/courses/cs49/Exams/Final2CS49Spring2010... · Final Exam, C programming May 24, 2010 Rules: Open book, open notes, open

4. Given that x is an array of 10 integers, show how you would call the

function average that you declared in the previous problem to compute themaximum entry in x and print the answer. Answer with one line only.

double t = average(x,10);

5. Consider the code

int f(int k)

{ int x[10000];

// further code not shown

}

The space for the 10000 entries of array x is located where?

(a) on the stack

6. If you wish the function f , which takes an integer argument and returnsand integer, to be accessible only in the file where it is defined, then

(a) how should you declare f? tt static int f(int n)(b) where should the prototype of f go? in the source file where f is

defined.

7. What will be printed by the following code? Answer: no. This example

was given in lecture.

double x;

int flag = 0;

for(x=0.0; x <= 1.0; x+=0.1)

{ if(x==0.3)

{ printf("yes");

flag = 1;

}

}

if(flag == 0)

printf("no");

}

8. Write code for the following function (four lines maximum)

2

Page 3: Final Exam, C programming - San Jose State Universitybeeson/courses/cs49/Exams/Final2CS49Spring2010... · Final Exam, C programming May 24, 2010 Rules: Open book, open notes, open

void sumdif(int x, int y, int *sum, int *dif)

// put the sum of x and y into *sum and the difference (x-y) into *dif

{ *sum = x+y;

*dif = x-y;

9. Show how the function sumdif declared in the previous problem would

be called, by filling in the missing two lines of code:

int x = 10;

int y = 5;

int sum, diff; // declare two variables to hold the answers

sumdif(&sum,&diff); // call sumdif

10. Which of the following functions allocates space on the heap?

(c) strdup

11. Fill in the missing code (in four places, corresponding to the comments)

int main( int argc, char *argv[])

// declare main appropriately for using a command-line argument

{ if( argc > 1) // test if a command-line argument was supplied

{ int n;

if( sscanf("%d",&n))

// test if the command line argument is an integer

{ printf("Yes, integer %d supplied\n", n);

}

else

{ printf("Usage: %s <integer>",argv[0]); // finish this line

}

}

else // else accidentally omitted on exam

3

Page 4: Final Exam, C programming - San Jose State Universitybeeson/courses/cs49/Exams/Final2CS49Spring2010... · Final Exam, C programming May 24, 2010 Rules: Open book, open notes, open

printf("No command line argument supplied\n");

}

12. Show how to use a typedef command to make LL a synonym for long

long.

typedef long long LL;

13. Define a structure type for a pair of integers, with two members first

and second. Use a typedef so that afterwards, we can declare a variable to

be of type intpair.

typedef struct { int first, second;} intpair;

14. Declare an array x of doubles with two indices, such that the first index

can take values 0 to 9 and the second can take values 0 to 12.

double x[10][13];

15. With the array x declared as in the previous problem, write code to see

whether it contains the value 99.73, and print found if it does (and don’tprint anything if not).

int i,j;

double epsilon = 1.0e-9;

for(i=0;i<10;i++)for(j=0;j<13];j++)

if(fabs(x[i][j] - 99.73) < epsilon))

// Never use == between doubles; if you did you got zero

printf("found");

16. Consider the type intlist defined in class, for a simply linked list ofintegers. How was intlist defined?

typedef struct L { int data; struct L *next;} intlist;

4

Page 5: Final Exam, C programming - San Jose State Universitybeeson/courses/cs49/Exams/Final2CS49Spring2010... · Final Exam, C programming May 24, 2010 Rules: Open book, open notes, open

17. (Continuing with intlist) Draw a linked-list diagram of a list (2, 7, 9).

Too hard to do with TEXand almost everyone got it anyway.

18. (Continuing with intlist) If x points to the list (2, 7, 9), what is thevalue of the following expressions?

(a) x->next->data 7

(b) x->next->next->data 9

(c) x ->next ->next->next NULL

19. What is the value of the following expressions?

(a) 0xfff2 ^ 0xfff3 1

(b) 0xfff2 | 0xfff3 0xfff3

(c) 0xfff2 & 0xfff3 0xfff2

(d) 0xfff2 && 0xfff3 1

(e) 0xfff2 || 0xfff3 1

20. Fill in the blanks in the following code

int main(void)

{ FILE *fp; // declare a variable to use for a file pointer

fp = fopen("Grades.txt","r+"); // open the file "Grades.txt" for reading and modifying

fseek(fp,0,SEEK_END ); // move to the end of the file

fputs("George was here", fp); // write "George was here!" at the end

fclose(fp); // close the file

}

21. Write a rule to use in a makefile that says, *.class files depend on *.javafiles, and whenever the .java file changes, you should run javac on that file

to rebuild the class file. Indicate in a comment (that would be legal as acomment in a makefile) what character the second line begins with.

*.class : *.java

javac $*.java

# second line begins with a tab character

5