Problem 1 (10 points): Short Answer

1.      Two ways to subtract 1 from x are “x = x -1;” and “x--;”.  What are the other two? ______________________________________________________________________________________________________

2.      Dangling-Else Problem.  Modify the following code by inserting braces ({}) in the appropriate places to produce the output shown.  Do not make any other changes apart for inserting braces. 

int examGrade, quizGrade;

if (examGrade < 60)

cout << “We have a problem” << endl;

if (quizGrade < 10)

cout << “We have a real problem” << endl;

else

cout << “Ok” << endl;

 

If examGrade = 90 and quizGrade = 5, the following output is produced:

Ok.

3.      Given the following if/else statement, rewrite it as a switch statement.  Assume all variables referenced in statement have been properly declared and initialized.


 

 

char result;

if (rank == JACK ||

    rank == QUEEN ||

    rank == KING)

   result = ‘F’;

else if (rank == ACE)

   result = ‘A’;

else

   result = rank+‘0’;

                

 

 

 

 

________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

 


4.  Identify any problems with the following line of code and discuss how you would correct them:   for (int x = 0; x < 10; x--)

__________________________________________________________________________________________________________________________________________________________________

5.      Write the function header for the function letterGrade that takes a double-precision, floating point argument grade and returns a character representing the grade.

______________________________________________________

 

 

Problem 2 (10 points): What is the output of the following program?

 

#include <iostream>

 

using std::cout;

using std::endl;

 

void foo(int);

 

int main() {

 

  cout << “a) “;

  foo(6);

  cout << endl;

  cout << “b) “;

  foo(7);

  cout << endl;

  cout << “c) “;

  foo(8);

  cout << endl;

}

 

void foo (int n) {

 

  bool found = false;

 

  for (int i = n-1; !found && i > 1; i--) {

    if (n % i == 0) {

      found = true;

      foo(n/i);

      foo(i);

    }

  }

 

  if (!found)

    cout << n << " ";

}

 

Output:

a)

b)

c)


 

Problem 3 (10 points): Complete the following program.  It should continually prompt the user for a volume of a sphere, and using the formula below, calculate the radius of the sphere.  It will then output the results until the user enters an end-of-file character.

 

#include                          // Necessary header file

#include<cmath>

 

using std::cout;

using std::endl;

 

int main() {

 

  // Declare variables

  const double PI = 3.14159265;

  double volume;

 

 

  // Prompt the user for the volume of the sphere

  cout << "Enter the volume of the sphere (Ctrl-Z to quit)"

       << endl;

 

  // Use a loop to get volumes and calculate radiuses until

  // the user wants to stop

  while (                                ) {

    // Calculate the radius

 

 

 

 

    // Output the result (include the volume in the result)

 

 

 

 

 

    // Again prompt the user for the volume of the sphere

 

 

 

 

 

  }

}