CSCI 159: exam practice questions

These are just meant as a way to get some practice answering exam-style questions with pen/paper. I'd encourage students to attempt them with minimal references to their notes, but for the final exam you will be permitted one double-sided sheet of notes.

There is one question on each side of the page.

After you've attempted the question, a good way to evaluate your answer while also getting additional practice is to coding and compiling/running the answer you came up with.

Question 1: Nested loops

Assuming ROWS and COLUMNS are positive integer values, describe the pattern the following code segment produces:
for (int r = 1; r <= ROWS; r++) {
    for (int c = 1; c <= COLUMNS; c++) {
        if ((r == 1) || (c == 1)) {
           cout << "*";
        } else if (c == COLUMNS) {
           cout << "*" << endl;
        } else if (r == ROWS) {
           cout << "*";
        } else if (c == r) {
           cout << "\";
        } else {
           cout << " ";
        }
    }
}
(Tip: try working through it on paper with small values for rows and columns, e.g. 2 and 3.)

Question 2: loops and character arrays

Suppose we have a character array defined and filled in as shown below, where SIZE is some integer constant:
int main()
{
   char mytext[SIZE];
   cout << "Enter a line of text: ";
   cin.getline(mytext, SIZE);
}
Add a loop to the main routine to print each of the alphabetic characters from their line of text, but not the nonalphabetic ones (e.g. if their text was "I took 159 and it drove me crazy!" then the output would be "Itookanditdrovemecrazy").

Tip 1: strlen(mytext) will tell you how many characters they entered
Tip 2: isalpha(ch) takes a single character as a parameter and returns true if the character was alphabetic, false otherwise