Homework 7: Due Tuesday, October 3rd

Read Chapter 4, sections 4.5, 4.7, and 4.8. Complete the following problems:

The following expressions make sense but do not follow the C++ syntax rules. Rewrite each expression so that it is a valid logical expression in C++.

1.  x < y <= z

2.  x, y, and z are all greater than 0

3.  x is equal to neither y nor z

4.  x is equal to y and z

5.  x is greater than y or z

The following code is almost unreadable because of the inconsistent indentation. Fix the indentation. Make sure you align the braces properly.

6.   if (a > 0)
    if (a < 20)
       {
      cout << "A is in range" << endl;
    b = 5;
     }
       else
              {
   cout << "A is too large" << endl;
     b = 3;
   }
    else
  cout << "A is too small" << endl;
    cout << "All done" << endl;

Show the output of the following statements:

7. x = 5;
    y = 3;
    z = 2;
    if (x > y)
       if (y + z < x)
          cout << z << "  " << y << endl;
       else
          cout << y << "  " << z << endl;
    cout << y << "  " << x << endl;

8. x = 5;
    y = 3;
    z = 2;
    if (y > z)
       if (x + y < z)
          cout << z << "  " << y << endl;
       else
          cout << y << "  " << z << endl;
    else
       cout << y << "  " << x << endl;

Write the C++ statements to do each of the following.

9. Read the amount of sales. Print the amount of sales and the
commission. The commission is calculated as follows:
       sales under 2500              no commission
       sales from 2500 up to 5000    2% commission
       sales from 5000 up to 10000   3% commission
       sales 10000 and over          4% commission

10. In Iowa, pesticide can be sprayed from an airplane only if the
temperature is at least 70 degrees, the relative humidity is between
15 and 35 percent, and the wind speed is no more than 10 miles per hour.
Write the statements to read the temperature, humidity, and wind speed,
and print one of the following messages:
       Conditions are acceptable for spraying
       No spraying is possible today


Remember to type your answers and to make two copies of each homework assignment. One copy is submitted to me and the other you keep to use in the group discussions.


Email Me | Office Hours | My Home Page | Department Home | MCC Home Page

© Copyright Emmi Schatz 2004