
I am still in the process of grading the exams from Monday night. In the meanwhile, you might like to review the following solutions to the short answer section of the exam. I am planning to also post some source code solutions to the programming problems presented on the test.
- Apply the pre-increment operator to ctr.
++ctr
- Fill in code to output 3.4 cubed.
cout << pow(3.4, 3.0) << endl; or
cout << 3.4 * 3.4 * 3.4 << endl;
- Complete the following table regarding the three loop types used in c++.
| loop type | minimum iterations | pre-test or post-test |
| do-while | 1 | post |
| for | 0 | pre |
| while | 0 | pre |
- Name the operation and fill in the value.
| 23 / 4 | integer | 5 |
| 23 / 4.0 | floating point | 5.75 |
| 23 % 4 | modulus | 3 |
- Complete the truth tables for
AND
OR
XOR
- Convert the following while loop to a for loop. Show the output.
const int STOP = 3;
int ctr = 0;
while ( ctr < STOP )
cout << ctr++ << endl;
Output:
0
1
2
Code:
const int STOP = 3;
for ( int ctr = 0; ctr < STOP; ctr++ )
cout << ctr << endl;
- Rewrite the following if/else if code using a switch statement and display the grade level associated with the grade entered by the user. Assume grade is an integer. Declare the constants used in the code.
string grade_level;
if ( grade == FRESHMAN )
grade_level = "Freshman")
else if ( grade == SOPHOMORE )
grade_level = "Sophomore;
else if ( grade == JUNIOR )
grade_level = "Junior";
else if ( grade == SENIOR)
grade_level = "Senior";
cout << grade_level << endl;
Code:
string grade_level;
const int FRESHMAN = 9;
const int SOPHOMORE = 10;
const int JUNIOR = 11;
const int SENIOR = 12;
switch ( grade )
{
case ( FRESHMAN ):
grade_level = "Freshman";
break;
case ( SOPHOMORE ):
grade_level = "Sophomore";
break;
case ( JUNIOR ):
grade_level = "Junior";
break;
case ( SENIOR ):
grade_level = "Senior";
break;
}
cout << grade_level << endl;