Problem 0: Review old quizzes and homework assignments.
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).
const double PI = 3.14159;
drawLine(x + (int)(r * cos(i * 2 * PI / n)),
y + (int)(r * sin(i * 2 * PI / n)),
x + (int)(r * cos((i + 1) * 2 * PI / n)),
y + (int)(r * sin((i + 1) * 2 * PI / n)));
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)
bool Card::makesPair(Card other)
{
if (rank == other.rank && isRed() == other.isRed() && suit != other.suit)
return true;
else
return false;
}
Card d(3, Card::HEARTS); cout << a.makesPair(d) << endl; // false -- different rank
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
static bool Card::isFlush(Card[] hand, int size)
{
int c = 1;
while (c < size && hand[c].suit == hand[0].suit)
c++;
if (c >= size)
return true;
else
return false;
}
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;
// Note that there are two separate ifs but no values lead to two messages
// displayed since it is impossible for (x > 5 && y < 0) and (x > 0 && y > 0)
// to be true at the same time.
// You can never output B
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.
Bold is for part (b)
int factorialSum(int n)
{
int sum = 0;
int factorial = 0;
for (int i = 1; i <= n; i++)
{
// compute i!
int factorial = 1;
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); |
![]() |
for (int r = 0; r < 5; r++)
drawOval(r * 10, r * 10, 100 - 20 * r, 100 - 20* r);
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)));
|
![]() |
for (int x = 20; x <= 100; x += 40)
for (int side = 0; side < sides; side++)
drawLine(x + (int)(20 * cos(startAngle + side * sideAngle)),
20 + (int)(20 * sin(startAngle + side * sideAngle)),
x + (int)(20 * cos(startAngle + (side + 1) * sideAngle)),
20 + (int)(20 * sin(startAngle + (side + 1) * sideAngle)));
| You are on your own for this picture. | ![]() |
for (sides = 3; sides < 9; sides++)
{
sideAngle = 2 * PI / sides;
for (int side = 0; side < sides; side++)
drawLine(sides * 40 - 100 + (int)(20 * cos(startAngle + side * sideAngle)),
20 + (int)(20 * sin(startAngle + side * sideAngle)),
sides * 40 - 100 + (int)(20 * cos(startAngle + (side + 1) * sideAngle)),
20 + (int)(20 * sin(startAngle + (side + 1) * sideAngle)));
}
Problem 6: Write methods to do each of the following.
int productOddCubes(int n)
{
int product = 1;
for (int term = 0; term < n; term++)
product *= pow(2 * term + 1, 3);
return product;
}
int findOutOfOrder(string s)
{
int loc = 0;
while (loc < s.length() - 1 && s[loc] < s[loc + 1])
loc++;
if (loc == s.length() - 1)
return s.length();
else
return loc;
}
int shortestLength(string sarr[], int size)
{
if (size == 0)
return -1;
int min = sarr[i].length();
for (int i = 1; i < size; i++)
if (sarr[i].length() < min)
min = sarr[i].length();
return min;
}
string shortestString(string sarr[], int size)
{
if (size == 0)
return "";
int min = sarr[0].length();
string shortest = sarr[0];
for (int i = 1; i < size; i++)
if (sarr[i].length() < min)
{
min = sarr[i].length();
shortest = sarr[i];
}
return shortest;
}
int findSpace(string sarr[], int size)
{
int loc = 0;
while (loc < size && sarr[loc].find_first_of(" ") == -1)
loc++;
if (loc == size)
return -1;
else
return loc;
}
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.
// declare and create the array
int rows = 10;
int cols = 5
int arr[rows][cols];
srand();
// initialize array to random numbers
for (int r = 0; r < rows; r++)
for (int c = 0; c < cols; c++)
arr[r][c] = rand() % 9 + 1;
int maxTotal;
int maxRow = -1; // the row with the highest total
int total = 0;
for (int r = 0; r < rows; r++)
{
// get the total for the current row
int rowTotal = 0;
for (int c = 0; c < cols; c++)
rowTotal += arr[r][c];
// add current row's total into the grand total
total += rowTotal;
// check if the current row had a higher total than any other so far
if (maxRow == -1 || rowTotal > maxTotal)
{
maxTotal = rowTotal;
maxRow = r;
}
}
cout << "TOTAL = " << total << ", MAX ROW = " << maxRow << endl;