CPS 202
Mount
Final Exam
Short Questions (5 points each, 100 total)
Turn each of the following descriptions into C code. For example:
Declare an int variable called "speed": int speed;
(5 points each, 25 total)
Each of the following individual lines was pulled from within large programs. Each has one or more bugs - fix them. (Note that because you don't have the context in which the line appears, some have more than one possible correct answer) (5 points each, 50 total)
Long Questions - explain each in a full sentence or two (10 points each, 50 total)
Short Programming (15 points each, 30 total)
x=0;
while (x < 10) {
printf("%d times 2 is %d\n",x,x*2);
x++;
}
Long Programming 1
You are attempting to catalog your book collection. In your first attempt at it, you decide to write a program to print a catalog sheet. The sheet will print 25 double-spaced rows of blanks to be filled in. To save space, you will print two separate columns of blanks. Your program must use at least one loop. Write the program to make the output look like this: (25 points)
Book Catalog
Author Title ISBN # Author Title ISBN #
1. ____________ ___________ _________ 26. ____________ ___________ _________
2. ____________ ___________ _________ 27. ____________ ___________ _________
3. ____________ ___________ _________ 28. ____________ ___________ _________
. . .
24. ____________ ___________ _________ 49. ____________ ___________ _________
25. ____________ ___________ _________ 50. ____________ ___________ _________
Long Programming 2
Your form works pretty well, but as you capture the data for printing, you find that different printers can fit more rows than others - and when you import the data into a word processing program, the double spacing is more trouble than it is worth. Modify the program to allow a variable number of rows, and to make double spacing optional, taking the data from prompts, using scanf to read the data. Allow a maximum of 60 rows. Here is a sample of a run of the program. (20 points)
c:\> bookform
Enter the number of rows to print: 70
* Please enter a value from 1 to 60
Enter the number of rows to print: 40
Double space (y/n): U
* Please enter either Y or N
Double space (y/n): N
Book Catalog
Author Title ISBN # Author Title ISBN #
1. ____________ ___________ _________ 41. ____________ ___________ _________
2. ____________ ___________ _________ 42. ____________ ___________ _________
3. ____________ ___________ _________ 43. ____________ ___________ _________
. . .
39. ____________ ___________ _________ 79. ____________ ___________ _________
40. ____________ ___________ _________ 80. ____________ ___________ _________
Extra Credit 1
To make your program even more usable, you decide to rewrite it to accept the "lines" and "double" values on the command line. Here is a sample of a run of the program. (15 points)
c:\> bookform
usage: bookform lines double
* please enter a value for lines (1-60)
* please enter a value for double (Y or N)
c:\> bookform 50
usage: bookform lines double
* please enter a value for double (Y or N)
c:\> bookform 100 N
usage: bookform lines double
* rows value must be between 1 and 60
c:\> bookform 40 N
Book Catalog
Author Title ISBN # Author Title ISBN #
1. ____________ ___________ _________ 41. ____________ ___________ _________
2. ____________ ___________ _________ 42. ____________ ___________ _________
3. ____________ ___________ _________ 43. ____________ ___________ _________
. . .
39. ____________ ___________ _________ 79. ____________ ___________ _________
40. ____________ ___________ _________ 80. ____________ ___________ _________
Extra Credit 2
Writing stuff down is fine, but you decide it would be better to store your book collection data on disk. You're not exactly sure how to write database records to disk, so you decide to write a stub for the "write_data" function, but you can write the rest of the program. Make it look something like this (underlines indicate user input): (15 points)
New Book entry... Enter Author's Name (blank to end): KN King Enter Title: C Programming Enter ISBN #: 0-393-96945-2 Saving "C Programming" by KN King to database... New Book entry... Enter Author's Name (blank to end):
Last Modified: 13 Apr 2002