CPS 202
Mount
Final Exam
Answers
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)
- printf('Hello World!'); printf("Hello World!"); (must use "")
- int #_students; int num_students; (# not allowed in variable name)
- if (speed =< 65) printf("Speed is OK\n"); if (speed <= 65) printf("Speed is OK\n"); (=< is not valid)
- 4 = n; n = 4; (4 is an rvalue)
- int unsigned age; unsigned int age; (unsigned is a modifier and must be before int)
- #include <stdio.h>; #include <stdio.h> (no semicolon after an #include)
- auto static int area; static int area; (use either auto or static, but not both)
- unsigned int flag = -1; int flag = -1; (unsigned causes the -1 to be converted to an unsigned value) unsigned int flag = 1; (also accepted)
- &n = 6; *n = 6; (use the * operator, not the & operator, to assign a value to a pointer) n = 6; (also accepted)
- void timesplit(int total_minutes, int &hours, int &minutes);
void timesplit(int total_minutes, int *hours, int *minutes); (use * for pointers, not &)
Long Questions - explain each in a full sentence or two (answer on a separate piece of paper - 10 points each)
if (something == 1)
printf("something is 1\n");
printf("something is still 1\n"); // Appears to be part of the "if" but isn't
Short Programming (answer on a separate piece of paper - 15 points each)
x=0;
while (x < 10) {
printf("%d times 2 is %d\n",x,x*2);
x++;
}
for (x=0; x < 10; x++)
printf("%d time 2 is %d\n",x x*2);
float area_of_triangle(float length, float base)
{
float area;
area = (length * base) / 2.0;
return(area);
}
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. ____________ ___________ _________
/* Rationale for this program: This program illustrates many of the main
points covered in the course. It is the sort of minimum program I expect
a student to be able to write. It has an include, a variable, a loop,
it uses printf formatting, math to calculate the item numbers, the
increment operator, and logical testing. It also forces you to think about
how to solve the problem of getting lines into two columns as opposed to
one long column. It is not the most difficult exercise I've had you do,
but it has all the elements of any program of substance, and shows how a
little bit of code can go a long way to accomplishing important tasks.
*/
#include <stdio.h>
main()
{
int i;
printf(" Book Catalog\n\n");
printf(" Author Title ISBN # Author Title ISBN #\n\n");
for (i=1; i<=25; i++)
printf("%2d. ____________ ___________ ___________ "
"%2d. ____________ ___________ ___________\n\n",
i, i+25);
return(0);
}
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. ____________ ___________ _________
/* Rationale for this program: it is typical that a new program will be written
based on old code. Once a program is written, users find all sort of neat
things they would like it to do. Getting program parameters from the user is
an extremely typical thing to do. Proper testing of input data is critical
to the success of many program, certainly any that get data from a user.
*/
#include <stdio.h>
#include <ctype.h>
main()
{
int i;
int linecount;
char doublespace;
int col2;
while(1) {
printf("Enter the number of rows to print: ");
scanf("%d",&linecount);
if (linecount < 1 || linecount > 60) {
printf(" * Please enter a value from 1 to 60\n");
}
else break;
}
while(1) {
printf("Double space (y/n): ");
scanf(" %c",&doublespace);
doublespace = toupper(doublespace);
if (doublespace != 'N' && doublespace != 'Y') {
printf(" * Please enter either Y or N\n");
}
else break;
}
col2 = linecount+1;
printf(" Book Catalog\n\n");
printf(" Author Title ISBN # Author Title ISBN #\n\n");
for (i=1; i<=linecount; i++, col2++) {
printf("%2d. ____________ ___________ ___________ "
"%3d. ____________ ___________ ___________\n",
i, col2);
if (doublespace == 'Y') printf("\n");
}
return(0);
}
Extra credit long program 1
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
* lines 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. ____________ ___________ _________
/* Rationale for this program: this program has all the basic elements in the
required program, and then expands on them. Because we are now using the
command line, we will have to use pointers, and because the data is provided
in strings, we'll have to use the string libraries and string processing.
A resourceful programmer will see the need for a function to handle errors.
Aside from that, though, this version is very similar to version 2 above.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void usage(int, int, int, char *);
main(int argc, char *argv[])
{
int i;
int linecount;
char doublespace;
int col2;
if (argc == 1) usage(1,1,0,"");
if (argc == 2) usage(0,1,0,"");
linecount = atoi(argv[1]);
if (linecount < 1 || linecount > 60)
usage(0,0,1," * lines value must be between 1 and 60\n");
doublespace = toupper(argv[2][0]);
if (doublespace != 'N' && doublespace != 'Y')
usage(0,0,1," * double space value must be Y or N\n");
col2 = linecount+1;
printf(" Book Catalog\n\n");
printf(" Author Title ISBN # Author Title ISBN #\n\n");
for (i=1; i<=linecount; i++, col2++) {
printf("%2d. ____________ ___________ ___________ "
"%3d. ____________ ___________ ___________\n",
i, col2);
if (doublespace == 'Y') printf("\n");
}
return(0);
}
void usage(int badcount, int baddouble, int badmsg, char *msg)
{
printf("usage: bookform lines double\n");
if (badcount == 1) printf(" * please enter a value for lines (1-60)\n");
if (baddouble == 1) printf(" * please enter a value for double (Y or N)\n");
if (badmsg == 1) printf("%s",msg);
exit(1);
}
Extra credit long program 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 "Supernatural" by "Everything" to database... New Book entry... Enter Author's Name (blank to end):
/* Rationale for this program: this program again touches on all the basics,
but also brings in the concept of progressive development, using a stub for
a function we have not yet written. It obviously includes functions, but
also strings (and could use pointers, though it doesn't), and user
interface.
*/
#include <stdio.h>
int get_line(char *);
int get_data(void);
int write_data(void);
main()
{
while(1) {
if (get_data() == 0) break;
}
return(0);
}
int get_data(void)
{
char name[128];
char title[128];
char isbn[128];
int ret;
printf("New Book entry...\n");
printf("Enter Author's Name (blank to end): ");
ret = get_line(name);
if (ret == 0) return(0);
printf("Enter Title: ");
get_line(title);
printf("Enter ISBN #: ");
get_line(isbn);
printf("Saving \"%s\" by \"%s\" to database...\n\n",title,name);
write_data();
return(1);
}
// Read a single line of data, return length of data read
int get_line(char *buffer)
{
int i=0;
char ch;
while((ch = getchar()) != '\n') {
buffer[i] = ch;
i++;
}
buffer[i] = '\0';
return(i);
}
// Stub
int write_data(void)
{
return(1);
}
Last Modified: 29 Apr 2002