cout.put(char(88)); // put() prints char as character
cout << char(88) << endl; // new-style type cast value to char
cout << (char)88 << endl; // old-style type cast value to char
7. The answer depends on how large the two types are. If long is 4 bytes, there is no loss. That’s because the largest long value would be about 2 billion, which is 10 digits. Because double provides at least 13 significant figures, no rounding would be needed. The long long type, on the other hand, can reach 19 digits, which exceeds the 13 significant figures guaranteed for double.
8.
a. 8 * 9 + 2 is 72 + 2 is 74
b. 6 * 3 / 4 is 18 / 4 is 4
c. 3 / 4 * 6 is 0 * 6 is 0
d. 6.0 * 3 / 4 is 18.0 / 4 is 4.5
e. 15 % 4 is 3
9. Either of the following would work for the first task:
int pos = (int) x1 + (int) x2;
int pos = int(x1) + int(x2);
To add them as type double and then convert, you could do either of the following:
int pos = (int) (x1 + x2);
int pos = int(x1 + x2);
10.
a. int
b. float
c. char
d. char32_t
e. double
Answers to Chapter Review for Chapter 4
1.
a. char actors[30];
b. short betsie[100];
c. float chuck[13];
d. long double dipsea[64];
2.
a. array
b. array
c. array
d. array
3. int oddly[5] = {1, 3, 5, 7, 9};
4. int even = oddly[0] + oddly[4];
5. cout << ideas[1] << "\n"; // or << endl;
6. char lunch[13] = "cheeseburger"; // number of characters + 1
or
char lunch[] = "cheeseburger"; // let the compiler count elements
7. string lunch = "Waldorf Salad";
or, if you don’t have a using directive,
std::string lunch = "Waldorf Salad";
8.
struct fish {
char kind[20];
int weight;
float length;
};
9.
fish petes =
{
"trout",
12,
26.25
};
10. enum Response {No, Yes, Maybe};
11.
double * pd = &ted
cout << *pd << "\n";
12.
float * pf = treacle; // or = &treacle[0]
cout << pf[0] << " " << pf[9] << "\n";
// or use *pf and *(pf + 9)
13. This assumes that the iostream and vector header files have been included and that there is a using directive:
unsigned int size;
cout << "Enter a positive integer: ";
cin >> size;
int * dyn = new int [size];
vector
14. Yes, it is valid. The expression "Home of the jolly bytes" is a string constant; hence it evaluates as the address of the beginning of the string. The cout object interprets the address of a char as an invitation to print a string, but the type cast (int *) converts the address to type pointer-to-int, which is then printed as an address. In short, the statement prints the address of the string, assuming the int type is wide enough to hold an address.
15.
struct fish
{
char kind[20];
int weight;
float length;
};
fish * pole = new fish;
cout << "Enter kind of fish: ";
cin >> pole->kind;
16. Using cin >> address causes a program to skip over whitespace until it finds nonwhitespace. It then reads characters until it encounters whitespace again. Thus, it will skip over the newline following the numeric input, avoiding that problem. On the other hand, it will read just a single word, not an entire line.
17.
#include
#include
#include
const int Str_num {10}; // or = 10
...
std::vector
std::array
Answers to Chapter Review for Chapter 5
1. An entry-condition loop evaluates a test expression before entering the body of the loop. If the condition is initially false, the loop never executes its body. An exit-condition loop evaluates a test expression after processing the body of the loop. Thus, the loop body is executed once, even if the test expression is initially false. The for and while loops are entry-condition loops, and the do while loop is an exit-condition loop.
2. It would print the following:
01234
Note that cout << endl; is not part of the loop body (because there are no braces).
3. It would print the following:
0369
12
4. It would print the following:
6
8
5. It would print the following:
k = 8
6. It’s simplest to use the *= operator:
for (int num = 1; num <= 64; num *= 2)
cout << num << " ";
7. You enclose the statements within paired braces to form a single compound statement, or block.
8. Yes, the first statement is valid. The expression 1,024 consists of two expressions—1 and 024—joined by a comma operator. The value is the value of the right-hand expression. This is 024, which is octal for 20, so the declaration assigns the value 20 to x. The second statement is also valid. However, operator precedence causes it to be evaluated as follows:
(y = 1), 024;
That is, the left expression sets y to 1, and the value of the entire expression, which isn’t used, is 024, or 20.
9. The cin >> ch form skips over spaces, newlines, and tabs when it encounters them. The other two forms read those characters.
Answers to Chapter Review for Chapter 6