|
|
|
|
|
|
|
7. The following expressions make sense but are invalid according to C++'s rules of syntax. Rewrite them so that they are valid logical expressions. (All the variables are of type int.) |
|
|
|
 |
|
|
|
|
a. x < y <= z |
|
|
|
 |
|
|
|
|
b. x, y, and z are greater than 0 |
|
|
|
 |
|
|
|
|
c. x is equal to neither y nor z |
|
|
|
 |
|
|
|
|
d. x is equal to y and z |
|
|
|
|
|
|
|
|
8. Given these values for Boolean variables x, y, and z: |
|
|
|
|
|
|
|
|
x = TRUE, y = TRUE, z = FALSE |
|
|
|
 |
|
|
|
|
indicate whether each expression is TRUE (T) or FALSE (F). |
|
|
|
 |
|
|
|
|
______ a. ! (y | | z) | | x |
|
|
|
 |
|
|
|
|
______ b. z && x && y |
|
|
|
 |
|
|
|
|
______ c. ! y | | (z | | !x) |
|
|
|
 |
|
|
|
|
______ d. z | | (x && (y | | z)) |
|
|
|
 |
|
|
|
|
______ e. x | | x && z |
|
|
|
|
|
|
|
|
9. For each of the following problems, decide which is more appropriate, an If-Then-Else or an If-Then. Explain your answers. |
|
|
|
 |
|
|
|
|
a. Students who are candidates for admission to a college submit their SAT scores. If a student's score is equal to or above a certain value, print a letter of acceptance for the student. Otherwise, print a rejection notice. |
|
|
|
 |
|
|
|
|
b. For employees who work more than 40 hours a week, calculate overtime pay and add it to their regular pay. |
|
|
|
 |
|
|
|
|
c. In solving a quadratic equation, whenever the value of the discriminant (the quantity under the square root sign) is negative, print out a message noting that the roots are complex (imaginary) numbers. |
|
|
|
 |
|
|
|
|
d. In a computer-controlled sawmill, if a cross section of a log is greater than certain dimensions, adjust the saw to cut 4-inch by 8-inch beams; otherwise, adjust the saw to cut 2-inch by 4-inch studs. |
|
|
|
|
|
|
|
|
10. What causes the error message UNEXPECTED ELSE when this code fragment is compiled? |
|
|
|
|
|
|
|
|
if (mileage < 24.0)
{
cout << Gas ;
cout << guzzler.;
};
else
cout << Fuel efficient.; |
|
|
|
|
|
|
|
|
11. The following code fragment is supposed to print Type AB when Boolean variables typeA and typeB are both TRUE, and print Type O when both variables are FALSE. Instead, it prints Type O whenever just one of the variables is FALSE. Insert a { } pair to make the code segment work the way it should. |
|
|
|
 |
|
|
|
|
if (typeA || typeB)
if (typeA && typeB)
cout << Type AB;
else
cout << Type O; |
|
|
|
|
|