Loyola College in Maryland

CS 630 - Computing Fundamentals I - Spring 2004

Loyola College > Department of Computer Science > CS 630 > Homework > Final Exam Practice Problems

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.

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.

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.