C++ Program Design/3e Answers To Self-Check Exercises: Answer
C++ Program Design/3e Answers To Self-Check Exercises: Answer
Chapter 2
Answers to Self-Check Exercises
1. What manipulator inserts a new line character in the output stream?
Answer
endl
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
2. Write a C++ statement that extracts an integer from the stream cout and places it in the
object Count.
Answer
cout >> Count;
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
3. Under what circumstances should function main() return a nonzero value?
Answer
Answer
#include <iostream>
#include <string>
using namespace std;
int main() {
// Input meal price
cout << "Price of meal ? ";
float Price;
cin >> Price;
// Compute and output recommended tip
cout << "Tip on $" << Price << " is ";
cout << "$" << Price * 0.17 << endl;
return 0;
}
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
5. Suppose a short is 16 bits on a particular machine. How big must an int be?
Answer
16 bits
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
6. How many bits does the ASCII character set use to encode a character?
Answer
7
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
7. What type of C++ object is used to represent real numbers?
Answer
float
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
8. What is the C++ escape sequence for including a newline in a string?
Answer
\n
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
9. What is the C++ escape sequence for include a double quote (‘"‘) in a string?
Answer
\”
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
11. Name the two numeric components of a floating-point number.
Answer
Answer
– 10
1.13 × 10
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
15. What are the allowable characters for forming valid a C++ name?
Answer
The allowable characters are letters (upper and lowercase), digits, and underscores.
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
16. Is Window$Cost a valid C++ name?
Answer
No
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
17. In C++ a name that has a special meaning to the C++ compiler is called a
________________.
Answer
keyword
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
18. Write a C++ definition that defines and initializes the int object HeadCount to 25.
Answer
int HeadCount = 25;
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
19. Write a C++ definition that defines and initializes the float object MovingAverage
to 25.0.
Answer
float MovingAverage = 25.0;
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
20. Using the notation <value, type> give the value of the C++ expression 23.0 + 8
Answer
<21.0, double>
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
21. Using the notation <value, type> give the value of the C++ expression 10 / 12.
Answer
<0, int>
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
23. Using the notation <value, type> give the value of the C++ expression 15 / 14.
Answer
<1, int>
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
24. Using the notation <value, type> give the value of the C++ expression 10L / 2.
Answer
<5, long>
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
25. Using the notation <value, type> give the value of the C++ expression 25.5L / 5.
Answer
Answer
<2, int>
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
27. Write a C++ program that converts U.S. dollars to Canadian dollars. Assume a conversion
rate of 1.54 Canadian dollars for each U.S. dollar.
Answer
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << “Enter U.S. dollar amount: “;
float USDollars;
cin >> USDollars;
Answer
<7, long>
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
29. Using the notation <value, type> give the value of the C++ expression 2.3f + 5.2.
Answer
<7.5, double>
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
30. Using the notation <value, type> give the value of the C++ expression 5.1f + 3L.
Answer
<8.1, float>
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
31. Using the notation <value, type> give the value of the C++ expression 3.4L + 3L.
Answer
Answer
<7, long>
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
33. Give the value of the C++ expression 3 / 2 + 5.
Answer
6
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
34. Give the value fo the C++ expression 5 / 1 + 2.
Answer
7
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
35. Give the value of the C++ expression 3 * 2 + 4 * 5.
Answer
26
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
36. Give the value of the C++ expression 4 - 2 + 5 / 3 + 2.
Answer
5
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
37. Give the value of the C++ expression 10 - 2 + 7 % 2 - 1.
Answer
10
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
38. Write a C++ statement that inserts the value of the expression 25 / 4 in the stream cout.
Answer
cout << 25 / 4 << endl;
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
39. What two files must be included in a C++ program to use the iostream library?
Answer
Answer
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Please enter a time (HH:MM:SS): ";
int Hours;
cin >> Hours;
char Separator;
cin >> Separator;
int Minutes;
cin >> Minutes;
int Seconds;
cin >> Seconds;
return 0;
}