Module 4. If (Decision) Statements Exercises

1.  Write C++ code that prints out whether or not a thrill-seeking
person standing in line for a roller coaster should be allowed on the
ride.  A pregnant person (usually she is a woman) is not allowed on
the roller coaster.  Additionally, an altitudinally-challenged (i.e.,
a short) person is not allowed on a roller coaster if the person is
shorter than 48 inches.  If the person is not allowed on the ride,
output the string "Sorry".  Otherwise, output the string "OK: have
fun!".  Assume the following variables have been defined and
initialized to the person's height and state of pregnancy:

   int height;             // holds the person's height in inches.
   int is_pregnant;        // is true if the person is pregnant.

2.  Write C++ code to set the integer variable max to the maximum
value held in integer variables x, y, and z.  Assume all variables
have been declared.

3.  Rewrite the following series of nested if statements as one
if-else statement (with no nesting) that does the same thing....

   const int TRUE = 1;
   const int FALSE = 0;

   ...

   int honors;  //  true if the student is an honors student
   int awards;  //  true if the student has won an academic award
   int good_student;  //  true under certain circumstances...

   if (honors == TRUE)
     {
      if (awards == TRUE)
       {
        good_student = TRUE;
       }
      else
       {
        good_student = FALSE;
       }
     }
   else 
   if (honors == FALSE)
     {
      good_student = FALSE;
     }

4. 
   void main()
   {
    char choice;

    cin >> choice;
    switch (choice)
      { 
       case 'b' : cout << "High" << endl;
                     break;
       case 'c' : cout << "Medium" << endl;
       case 'D' : 
       case 'd' : cout << "Low" << endl;
                     break;
       default  : cout << "Off the scale" << endl;
      }
   }

What does this program output when the character variable choice has the
values 'c', 'x', and 'D'?

5.  What is the output of this badly-formatted C++ code?

   int x = 5;
   int z = 22;

   cout << "Cool";
   if (x > z)
   if (z < 50)
   cout << " dudes";
   else
   cout << " beans";
   cout << " is all we got";