Fall 1998 Midterm Solutions

I. True/False Questions (20 points)

1. C is case sensitive.

TRUE

2. char variables are stored as integer values.

TRUE. char variables are stored as integer values via the ASCII table.

3. Every function must include a return statement.

FALSE. A function with a return type set to void need not have a return statement.

4. It is recommended that you use global variables in your programs.

FALSE. It is recommended that you avoid the use of global variables.

5. Every C program must include a main() function.

TRUE

6. A static variable will maintain its value across several function calls.

TRUE

7. Given the following function prototype:

mystery ();
the function, mystery() will receive an integer parameter and return void.

FALSE. This function will receive an integer parameter and (by default) return an int.

8. Every if statement must have a corresponding else statement.

FALSE. It is certainly legal to have a single if statement without a corresponding else.

9. The very first computer bug was as an actual moth stuck inside the Harvard Mark I computer.

TRUE. I showed this in class.

10. The srand() function enables you to initialize the random number generator with any positive seed value.

TRUE

II. Determine the Output (20 points)

1. [5 points] Determine the output of the following program:
#include 

main () {
	int x;
	for(x = 12; x >= 2; x -= 3) {
		printf("%d ", x);
	}
}
Solution: 12 9 6 3

2. [5 points] Determine output of the following program:

#include 

main () {
	printf ("David Wells:  \"We will win in 4.\"");
}
Solution: David Wells: "We will win in 4."
One student already pointed out that Wells actually said “We will win in 5.”

3. [5 points] Determine the output of the following program:

#include 

main () {
	int counter=0;
	if (counter++ == 1)
		printf ("Apples\n");
	else
		printf ("Oranges\n");

	if (++counter == 1) 
		printf ("Apples");
	else
		printf ("Oranges");

}

Solution:

Oranges
Oranges

This question confused a good number of students. Here’s how it works:

Initially, counter is set to 0.

We then have the following statement:


	if (counter++ == 1)
		printf ("Apples\n");
	else
		printf ("Oranges\n");

Here, we are using the post-increment operator. We therefore test the condition (e.g. is counter == 1) first. The condition fails because counter is actually equal to 0. The program therefore prints “Oranges.” After the condition is tested, counter is incremented to 1.

We then have the following statement:

	if (++counter == 1) 
		printf ("Apples");
	else
		printf ("Oranges");

Here, we are using the pre-increment operator. We therefore immediately increment counter to 2, and then test the condition. counter is not equal to 1. We therefore print out “Oranges.”

4. [5 points] Determine the output of the following program:

#include 

main () {
	int rows,columns;
	for (rows=1; rows<=3; rows++) {
		for (columns=1; columns<=rows*2; columns++)
			printf ("*");
		printf ("\n");
	}
}
Solution:
**
****
******

III. Find the Error (20 points)

1. [5 points] This program will compile, but it contains a bug. Identify the bug.

#include 

main () {
	int y = 4;
	while (y > 0) {
	  printf("%d\n", y);
	  ++y;
	}
}
Solution: y will always be greater than 0. We have therefore created an Infinite Loop.

2. [5 points] This program will not compile. Explain what the problem is.

#include 

void init (int);

main () {
	init (5);
	printf ("Counter:  %d", counter);
}

void init (int x) {
	int counter = x;
}  
Solution: This question tests your understanding of scope. counter is a local variable defined within the init() function. It is therefore not available from within main(). When we reference counter from within main, we will receive an error indicating that counter does not exist.

3. [10 points] You have just been assigned to work on the new Mastercard Electronic Cash Card.
Each card includes a tiny microprocessor which executes simple C programs and keeps track of whole dollar amounts
(for simplicity's sake, you need not worry about cents.)

The data types available for the Smart Card microprocessor are outlined below:

Data Type Range
short -16 to 16
int -128 to 128
long -1024 to 1024

Unfortunately, the software engineers at Mastercard have hit a small glitch,
and have asked you to fix the problem. Here is some sample code they are working with:

#include 

main () {
		int deposit;
		int balance;

		balance = 55;
		deposit = 100;

		balance += deposit;

		/*  Additional Processing Here */

}

Questions

a. [5 points] What’s wrong with the code above?

Solution: balance is initially set to 55. We then add 100. This should result in 155. But, 155 is outside the number range defined for int (-128 to 128.) We will therefore have incorrect results.

b. [5 points] How would you fix the problem?

Solution: Set all variables to long. Of course, even when using long, customers will never be able to store more than $1024 on their cards.

IV. Short Answer Questions (20 points)

1. [10 points] Given the following variables:

int a = 10;
int b = 20;
int c = 5;
int d = 4;

Determine the output of the following printf statements:

a. printf ("%d\n", a+b/c);

b. printf ("%d\n", (a+b)/c);

c. printf ("%d\n", a/d);

d. printf ("%d\n", a%d);

e. printf ("%.2f\n", a/(float) d);

Solution:
a. 14
b. 6
c. 2
d. 2
e. 2.50

2. Given the same variables defined above:

int a = 10;
int b = 20;
int c = 5;
int d = 4;

a. [3 points] Determine the output of the following code fragment:

	if (a>b || c>d)
		printf ("New York\n");
	else
		printf ("San Diego\n");

b. [3 points] Determine the output of the following code fragment:

	if (a>b && c>d) 
		printf ("New York\n");
	else 
		printf ("San Diego\n");

c. [4 points] Determine the output of the following code fragment:

	if ((a>b || c>d) && a>d) 
		printf ("New York\n");
	else 
		printf ("San Diego\n");
Solution:
a. New York
b. San Diego
c. New York

V. Write the Function (20 points)

1. Write a function that determines the area of a rectangle and immediately prints the result.
The function has two float parameters: width and height; and no return variable. The function must
keep track of the number of times it is called and display this number each time it is called.

For example, the following main() program:

main () {
	area (5.0, 5.0);
	area (5.0, 2.5);
	area (5.2, 6.2);
	getchar();
}
will generate the following output:
Function Call #1,  Answer:  25.00
Function Call #2,  Answer:  12.50
Function Call #3,  Answer:  32.24

a. [5 points] Write the function prototype:

Solution: void area (float, float);

Remember: You are passing two float variables, and not returning anything. Hence, the return data type is void.

b. [15 points] Write the function:

Solution:
void area (float width, float height) {
	static counter = 1;
	printf ("Function Call #%d,  ", counter);
	printf ("Answer:  %.2f\n", width*height);
	counter++;
}

This stumped many students.

Remember these keys points:

  • the function should not return anything. Its return type is void and there is no need for a return statement. Instead of returning a value, the function simply prints the answer via printf().
  • you need to keep track of the number of times the function is called. Some students used global variables to do this. While syntactically correct, you are discouraged from using global variables (and, I therefore took points off.) The only effective way of counting across function calls is to declare a static counter variable. Remember that static variables retain their values across function calls.