Loyola College in Maryland

CS 630 - Computing Fundamentals I
Spring 2004


Loyola College > Department of Computer Science > CS 201 > Homework > Solutions to Final Exam Practice Problems

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.


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.

  • Assume there is a function drawOval in the header file draw.h. This function has the prototype:
    void drawOval(int x, int y, int width, int height);
    The function drawOval works in the following manner. It creates a box and draws the oval inside the box. The pair (x,y) in the parameters gives the location of the upper left corner of the box. The parameter width indicates the width of the box, and the parameter height indicates the height of the box.
    Your answer may include only one call to the function drawOval.
    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);
    
  • For this portion, your code may have only one call to drawLine
    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.


    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;