Wednesday, October 19, 2011

Exam #1 pretest comments -
     part the first

Quite a few students seemed to have difficulty with the questions requiring code to be written, whether on paper or in the computer. Instead of responding to each student individually, I am providing the source code solutions to the last four problems on the pretest. Questions 8 and 9 are solved here and questions 10 and 11 are in the next post.

Please be sure to review and understand all of these solutions.

    1. Show the output from the following loop.
    2. Rewrite this while loop as a for loop.

         const int INITIAL_YEAR = 2008;
         const int CURRENT_YEAR = 2011;
         int year = INITIAL_YEAR;

         while ( year < CURRENT_YEAR )
            cout << "Year " << year++ << endl;

Output

Rewritten as for loop

  1. Assuming that the variable grade is an integer provided by the user, rewrite the following if/else if code using a switch statement.

       string grade_level;

       if ( grade == 0 )
          grade_level = "Kindergarden")
       else if ( grade == 1 )
          grade_level = "First grade";
       else if ( grade == 2 )
          grade_level = "Second grade";
       else if ( grade == 3 )
          grade_level = "Third grade";
       else if ( grade == 4 )
          grade_level = "Fourth grade";
       else if ( grade == 5 )
          grade_level = "Fifth grade";
       else
          grade_level = "Middle school";

       cout << grade_level << endl;

Rewritten as switch statement

Come back soon and frequently: more recap from the pretest will follow as well as additional test prep material.