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? Ans: --x; and x -= 1; _______________________________________________________________________________

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.

Ans:
if (examGrade < 60) {
   cout << “We have a problem” <<
   if (quizGrade < 10)
      cout << “We have a real problem” << endl;
}
else
   cout << “Ok” << endl;

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’;

                

 

 

 

 

Ans:
char result;
switch(rank) {
   case JACK:
   case QUEEN:
   case KING:
      result = 'F';
      break;
   case ACE:
      result = 'A';
      break;
   default:
      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--)

Ans: It is an infinate loop.

__________________________________________________________________________________________________________________________________________________________________

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.

Ans:
double letterGrade(char);

______________________________________________________

 

 

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: (Ans)

a) 2 3

b) 7

c) 2 2 2


 

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 <iostream>                          // 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 (   cin >> volume                ) {

    // Calculate the radius

 

    double radius = pow( 3 * volume / (4 * PI), 1.0/3); 

 

 

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

 

    cout << "A sphere with a volume of " << volume 

        << " has a radius of " << radius << endl; 

 

 

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

 

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

 

 

 

  }

}