The exam will be in a format similar to the quizzes with some short answer, some writing of code, and some reading of code. The following exam should help prepare you for the longer questions.
Problem 0: Review old quizzes, homework assignments, and the self-check for chapter 2, 3, 4, 5, 6, and 14.
Problem 1:
To draw an n-sided polygon of radius r
centered at (x, y), we need to know
the coordinates of each corner. If corners are numbered clockwise
starting with corner 0 in the 3 o'clock position, then the coordinates of
corner i are (x + r * cos(i * 360 / n), y + r * sin(i * 360 / n))
where angles are measured in degrees. Write a C++ statment that,
given x, y, r, n, and i, draws
the ith side of the polygon (that is, the line from
corner i to corner i + 1). Keep in mind that C++'s
trig functions expect angles to measured in radians.
You may assume that there exists a header file draw.h which contains the
following function:
drawLine(int x1, int y1, int x2, int y2) that draws a line between the coordinate pair (x1, y1) and (x2, y2).
Problem 2: Recall that the Card class has two instance variables rank and suit, both ints from Quiz 2.
Card a(2, Card::HEARTS); Card b(2, Card::CLUBS); Card c(2, Card::DIAMONDS); cout << a.makesPair(b) << endl; // should output false (different color) cout << a.makesPair(c) << endl; // should output true cout << a.makesPair(a) << endl; // should output false (same suit)
Card hand[] = {Card(3, Card::CLUBS), Card(4, Card::CLUBS),
Card(Card::JACK, Card::HEARTS), Card(Card::QUEEN, Card::CLUBS),
Card(9, Card::CLUBS)}
cout << Card::isFlush(hand, 5) << endl; // should output false
hand[2] = Card(Card::JACK, Card::CLUBS);
cout << Card::isFlush(hand, 5) << endl; // should output true
Problem 3: Format the following code properly. Figure out which case is unreachable. Are there any values of x and y for which two messages would be printed?
if (x > 10 && y < 5) if (x > 5 && y < 0) cout << "A" << endl; if (x > 0 && y > 0) if (x == 0) cout << "B" << endl; else cout << "C" << endl;
Problem 4: The following method attempts to compute the sum 1! + 2! + ... + n!. It does not work correctly.
int factorialSum(int n)
{
int sum = 0;
int factorial = 0;
for (int i = 1; i <= n; i++)
{
// compute i!
for (int j = 1; j <= i; j++)
factorial *= j;
// factorial should now hold i!; add it to the sum
sum += factorial;
}
return sum;
}
Problem 5:
For each of the following sequences of statements, write loops that
are equivalent.
This draws 5 concentric circles.
drawOval(0, 0, 100, 100); drawOval(10, 10, 80, 80); drawOval(20, 20, 60, 60); drawOval(30, 30, 40, 40); drawOval(40, 40, 20, 20); |
![]() |
This draws 3 triangles (polygons!) of radius 20 centered at (20, 20),
(60, 20), and (100, 20).
const double PI = 3.14159;
int sides = 3;
double sideAngle = 2 * PI / sides;
double startAngle = PI / 2;
drawLine(20 + (int)(20 * cos(startAngle)),
20 + (int)(20 * sin(startAngle)),
20 + (int)(20 * cos(startAngle + sideAngle)),
20 + (int)(20 * sin(startAngle + sideAngle)));
drawLine(20 + (int)(20 * cos(startAngle + sideAngle)),
20 + (int)(20 * sin(startAngle + sideAngle)),
20 + (int)(20 * cos(startAngle + 2 * sideAngle)),
20 + (int)(20 * sin(startAngle + 2 * sideAngle)));
drawLine(20 + (int)(20 * cos(startAngle + 2 * sideAngle)),
20 + (int)(20 * sin(startAngle + 2 * sideAngle)),
20 + (int)(20 * cos(startAngle + 3 * sideAngle)),
20 + (int)(20 * sin(startAngle + 3 * sideAngle)));
drawLine(60 + (int)(20 * cos(startAngle)),
20 + (int)(20 * sin(startAngle)),
60 + (int)(20 * cos(startAngle + sideAngle)),
20 + (int)(20 * sin(startAngle + sideAngle)));
drawLine(60 + (int)(20 * cos(startAngle + sideAngle)),
20 + (int)(20 * sin(startAngle + sideAngle)),
60 + (int)(20 * cos(startAngle + 2 * sideAngle)),
20 + (int)(20 * sin(startAngle + 2 * sideAngle)));
drawLine(60 + (int)(20 * cos(startAngle + 2 * sideAngle)),
20 + (int)(20 * sin(startAngle + 2 * sideAngle)),
60 + (int)(20 * cos(startAngle + 3 * sideAngle)),
20 + (int)(20 * sin(startAngle + 3 * sideAngle)));
drawLine(100 + (int)(20 * cos(startAngle)),
20 + (int)(20 * sin(startAngle)),
100 + (int)(20 * cos(startAngle + sideAngle)),
20 + (int)(20 * sin(startAngle + sideAngle))));
drawLine(100 + (int)(20 * cos(startAngle + sideAngle)),
20 + (int)(20 * sin(startAngle + sideAngle)),
100 + (int)(20 * cos(startAngle + 2 * sideAngle)),
20 + (int)(20 * sin(startAngle + 2 * sideAngle)));
drawLine(100 + (int)(20 * cos(startAngle + 2 * sideAngle)),
20 + (int)(20 * sin(startAngle + 2 * sideAngle)),
100 + (int)(20 * cos(startAngle + 3 * sideAngle)),
20 + (int)(20 * sin(startAngle + 3 * sideAngle)));
|
![]() |
| You are on your own for this picture. You may call drawLine only once. | ![]() |
Problem 6: Write methods to do each of the following.
Problem 7: Write a code fragment that creates a 10 row, 5 column array of ints, initializes all elements of that array to random integers from 1 to 9, and then displays the sum of all the entries and the index of the row that has the highest total.