Midterm 2 - Comp 3010
Midterm 2 - Comp 3010
Problem 1. 2. 3. Total
Marks
out of 27 52 21 100
a. (17 points)
Does Program1.cpp compile? If you answer yes explain what it will output.
If you answer no explain why it does not compile.
Program1.cpp:
#include <iostream>
using namespace std;
int main() {
int j = 0, i = 5, total = 0;
while (i < 10) {
j = i;
j = (j * 3) % 9;
total += j;
cout << j << " + ";
i++;
}
total += j;
cout << j << "= " << total;
return 0;
}
student name, id page 2 of 3
b. (17 points) In the code Fragment3.cpp underline all classes, circle all ob-
jects, draw boxes around all member functions, and draw a line through all
arguments.
Fragment3.cpp:
ifstream in_stream;
ofstream out_stream;
in_stream.open("Input.dat");
if (in_stream.fail()) {
cout << "Failed to open input file\n";
exit(1);
}
out_stream.open("Output.dat");
if (out_stream.fail()) {
cout << "Failed to open output file\n";
exit(1);
}
switch (number) {
case 0:
out_stream << "relaxed\n";
break;
case 1:
out_stream << "truly calm\n";
break;
case 2:
out_stream << "calmly focused\n";
break;
default:
out_stream << "and enjoying it\n";
}
c. (18 points)
Does Fragment2.cpp contain a syntax error? If you answer no explain what
it will output. If you answer yes explain where the syntax error is.
student name, id page 3 of 3
Fragment2.cpp:
int i, j;
// some other code here
for (i = 1; i < 8; i += 3)
for (j = 8; j > i; j -= 2)
cout << j << " ";
Complete the body of function pow2 (without using the built--in function pow)
to calculate and return the value 2n .
int pow2(int n)
{