COM 1100 Fundamentals of Computer Science Midterm Exam: November 4, 1999
 Professor Fell
Name _________________ ID Number: __________________
123456Total
152020152010100
Problem 1 (15 points) : Arithemetic and Assignment
Show the output printed by the following code. Be careful to follow the rules of integer arithmetic and the precedence of operators!
 CODE-----------------OUTPUT-----------------
a
int c = 100;
int f = 32 + c * 9 / 5;
cout << c << " : " << f << endl;
 
b
int c = 100;
int f = 32 + (9 / 5) * c;
cout << c << " : " << f << endl;
 
c
int nn = 75;
cout << nn / 12 << "ft " << nn % 12 << "in" << endl;
 
d
int a = 5, b = 7, c = 9;
a = b;
b = c;
c = a;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
 
e
int M = 11, N = 6;
M = 2 * N - M;
N = N Ð M;
cout << M << " : " << N << endl;
 

Problem 2 (20 points) : Reading Loops
Show the output printed by the following code:
 CODE-----------------OUTPUT-----------------
a
for (int x = 1; x < 100; x = 2 * x + 1){
	cout << x << endl;
}
 
b
int y = 1;
for (int z = 0; z < 100; z += y){
	cout << y << " : " << z << endl;
	y = y + 3;
}	
 
c
int M = 2, P = 5;
while ((M + P) < 100){
	cout << M << " : " << P << endl;
	if (M < P) M = M + P;
	else P = M + P;
}
 

Problem 3 (20 points) : Graphics and Loops
(a) Show the drawing produced by code below, in a drawing window.
The grid unit is 30 x 30 pixels to help you align your sketch.

for (int x = 0; x <= 180; x = x + 60) {
	PaintRect(x, x, x + 60, x + 120);
}


(b) Write a loop that will draw the following picture.

Problem 4 (15 Points) : Writing Loops
(a) The following statement can be used to simulate tossing a coin:

	int toss = RandomLong(0,1); // 0 for tails, 1 for heads
Write code (including a loop and all output statements) loop that will toss a coin 100 times and will print how many heads were rolled.

Problem 5 (20 Points) : Writing Functions
Write a C++ function Average3 that takes three value arguments and returns as function value the average of the three input values.

Function prototype:

Function definition:

 

 

Show a small segment of code that uses your function:

 

Problem 6 (10 Points) : Reading Functions
Assume the following function has been defined:

int SomeVal(int x, int y, int z){
	if (x < y && x < z) return x;
	if (y < x && y < z) return y;
	return z;
}
Show the output of the following code:
cout << "2 3 4  " << SomeVal(2, 3, 4) << endl;
	cout << "4 3 2  " << SomeVal(4, 3, 2) << endl;
	cout << "4 2 3  " << SomeVal(4, 2, 3) << endl;
	cout << "2 2 4  " << SomeVal(2, 2, 4) << endl;

Last Updated: December 8, 1999 8:44 am by

Harriet Fell
College of Computer Science, Northeastern University
360 Huntington Avenue #161CN,
Boston, MA 02115
Internet: com1100@harrietfell.com
Phone: (617) 373-2198 / Fax: (617) 373-5121
The URL for this document is: http://www.ccs.neu.edu/home/fell/COM1100/QUIZ/Midterm.html