#### Questions

1. A relational operator

a. assigns one operand to another.

b. yields a Boolean result.

c. compares two operands.

d. logically combines two operands.

**Answer: b, c**

2. Write an expression that uses a relational operator to return true if the variable george is not equal to sally.

**Answer: george != sally**

**Answer: –1 is true; only 0 is false.**

**Answer: The initialize expression initializes the loop variable, the test expression tests the loop**

**variable, and the increment expression changes the loop variable.**

a. the for statement itself.

b. the closing brace in a multistatement loop body.

c. each statement within the loop body.

d. the test expression.

**Answer: c, d**

**Answer: True**

**Answer:**

**for(int j=100; j<=110; j++)**

**cout << endl << j;**

**Answer: braces (curly brackets)**

a. from the point of definition onward in the program.

b. from the point of definition onward in the function.

c. from the point of definition onward in the block.

d. throughout the function.

**Answer: c**

**Answer:**

**int j = 100;**

**while( j <= 110 )**

**
**

cout << endl << j++;

**Answer: False**

**Answer: at least one**

**Answer:**

**int j = 100;**

**do**

**cout << endl << j++;**

**while( j <= 110 );**

**Answer:**

**if(age > 21)**

**cout << “Yes”;**

a. the loop in which it occurs.

b. the block in which it occurs.

c. the function in which it occurs.

d. the program in which it occurs.

**Answer: D**

**Answer:**

**if( age > 21 )**

**cout << “Yes”;**

**
**

else

cout << “No”;

a. returns a character when any key is pressed.

b. returns a character when Enter is pressed.

c. displays a character on the screen when any key is pressed.

d. does not display a character on the screen.

**Answer:a,c**

**Answer: ‘\r’**

**Answer: preceding, surrounded by braces**

**Answer: reformatting**

**Answer:**

**switch(ch)**

**{**

**case ‘y’:**

**cout << “Yes”;**

**break;**

**case ‘n’:**

**cout << “No”;**

**break;**

**default:**

**cout << “Unknown response”;**

**}**

**Answer: ticket = (speed > 55) ? 1 : 0;**

a. compare two numeric values.

b. combine two numeric values.

c. compare two Boolean values.

d. combine two Boolean values.

**Answer: d**

greater than 55.

**Answer: limit == 55 && speed > 55**

**Answer: unary, arithmetic, relational, logical, conditional, assignment**

a. only from the innermost loop.

b. only from the innermost switch.

c. from all loops and switches.

d. from the innermost loop or switch.

**Answer: d**

**Answer: the top of the loop**

a. an operator.

b. a label.

c. a variable.

d. a function.

**Answer: b**

#### Exercises

1. Assume that you want to generate a table of multiples of any given number. Write a pro- gram that allows the user to enter the number and then generates the table, formatting it into 10 columns and 20 lines. Interaction with the program should look like this (only the first three lines are shown):

Enter a number: 7

7 14 21 28 35 42 49 56 63 70

77 84 91 98 105 112 119 126 133 140

147 154 161 168 175 182 189 196 203 210

**Solution: Question 1**

Type 1 to convert Fahrenheit to Celsius,

2 to convert Celsius to Fahrenheit: 1

Enter temperature in Fahrenheit: 70

In Celsius that’s 21.111111

**Solution: Question 2**

Enter a number: 123456

Number is: 123456

**Solution: Question 3**

Enter first number, operator, second number: 10 / 3

Answer = 3.333333

Do another (y/n)? y

Enter first number, operator, second number: 12 + 100

Answer = 112

Do another (y/n)? n

**Solution: Question 4**

X

XXX

XXXXX

XXXXXXX

XXXXXXXXX

except that it should be 20 lines high, instead of the 5 lines shown here. One way to do this is to nest two inner loops, one to print spaces and one to print Xs, inside an outer loop that steps down the screen from line to line.

**Solution: Question 5**

effect.

**Solution: Question 6**

Enter initial amount: 3000

Enter number of years: 10

Enter interest rate (percent per year): 5.5

At the end of 10 years, you will have 5124.43 dollars.

At the end of the first year you have 3000 + (3000 * 0.055), which is 3165. At the end of the second year you have 3165 + (3165 * 0.055), which is 3339.08. Do this as many times as there are years. A for loop makes the calculation easy.

**Solution:**

Enter first amount: £5.10.6

Enter second amount: £3.2.6

Total is £8.13.0

Do you wish to continue (y/n)?

To add the two amounts, you’ll need to carry 1 shilling when the pence value is greater

than 11, and carry 1 pound when there are more than 19 shillings.

**Solution:**

which is 360. Write a program that calculates the number of possible arrangements for any number of guests and any number of chairs. (Assume there will never be fewer guests than chairs.) Don’t let this get too complicated. A simple for loop should do it.

**Solution:**

**Solution:**

amount by a floating-point number. (It doesn’t make sense to multiply two money amounts; there is no such thing as square money. We’ll ignore division. Use the general style of the ordinary four function calculator in Exercise 4 in this chapter.)

**Solution:**

Addition: a/b + c/d = (a*d + b*c) / (b*d)

Subtraction: a/b - c/d = (a*d - b*c) / (b*d)

Multiplication: a/b * c/d = (a*c) / (b*d)

Division: a/b / c/d = (a*d) / (b*c)

The user should type the first fraction, an operator, and a second fraction. The program

should then display the result and ask whether the user wants to continue.

**Solution:**