| 1 | 2 | 3 | 4 | 5 | 6 | Total |
| 15 | 20 | 20 | 15 | 20 | 10 | 100 |
| CODE | -----------------OUTPUT----------------- | |
| a |
int c = 100; int f = 32 + c * 9 / 5; cout << c << " : " << f << endl; | 100 : 212 |
| b |
int c = 100; int f = 32 + (9 / 5) * c; cout << c << " : " << f << endl; | 100 : 132 |
| c |
int nn = 75; cout << nn / 12 << "ft " << nn % 12 << "in" << endl; | 6ft 3in |
| 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; | a = 7 b = 9 c = 7 |
| e |
int M = 11, N = 6; M = 2 * N - M; N = N Ð M; cout << M << " : " << N << endl; | 1 : 5 |
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;
}
| 1 3 7 15 31 63 |
| b |
int y = 1;
for (int z = 0; z < 100; z += y){
cout << y << " : " << z << endl;
y = y + 3;
}
| 1 : 0 4 : 4 7 : 11 10 : 21 13 : 34 16 : 50 19 : 69 22 : 91 |
| c |
int M = 2, P = 5;
while ((M + P) < 100){
cout << M << " : " << P << endl;
if (M < P) M = M + P;
else P = M + P;
}
| 2 : 5 7 : 5 7 : 12 19 : 12 19 : 31 50 : 31 |
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.
for (int x = 30; x < 300; x = x + 60) {
PaintCircle(x, x, 30);
}
|
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 headsWrite code (including a loop and all output statements) that will toss a coin 100 times and will print how many heads were rolled.
int toss;
int heads = 0;
for (int N = 0; N < 100; N++){
toss = RandomLong(0,1); // 0 for tails, 1 for heads
heads += toss;
}
cout << "In 100 rolls, " << heads << " heads were rolled." << endl;
Problem 5 (20 Points) : Writing Functions Function prototype:
double Average3(double N1, double N2, double N3);
Function definition:
double Average3(double N1, double N2, double N3){
return (N1 + N2 + N3)/3;
}
Show a small segment of code that uses your function:
double A, B, C; cout << "Enter three numbers: "; cin >> A >> B >> C; cout "The average of you numbers is: << Average3(A, B, C) << endl;
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;2 3 4 2 4 3 2 2 4 2 3 2 2 2 4 4
Last Updated: December 8, 1999 3:17 pm by