0% found this document useful (0 votes)
98 views36 pages

C++ Program Design/3e Answers To Self-Check Exercises: Answer

This document contains answers to self-check exercises from Chapter 2 of the book "C++ Program Design/3e". It provides answers to 34 questions about C++ concepts and syntax, such as: - The endl manipulator inserts a new line in an output stream - A program should return a non-zero value from main() if it encounters an error - The ASCII character set uses 7 bits to encode each character - A float object is used to represent real numbers in C++

Uploaded by

Ledia Pelivani
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views36 pages

C++ Program Design/3e Answers To Self-Check Exercises: Answer

This document contains answers to self-check exercises from Chapter 2 of the book "C++ Program Design/3e". It provides answers to 34 questions about C++ concepts and syntax, such as: - The endl manipulator inserts a new line in an output stream - A program should return a non-zero value from main() if it encounters an error - The ASCII character set uses 7 bits to encode each character - A float object is used to represent real numbers in C++

Uploaded by

Ledia Pelivani
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

C++ Program Design/3e

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

Function main() should return a nonzero value.


C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
4. Write a C++ program that computes a tip on a meal purchase. Since you are generous, use
17 percent as the tipping rate.

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

mantissa and exponent


C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
14. Give the standard scientific notation for the C++ floating-point constant 1.13E-10.

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

<5.1, long double>


C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
26. Using the notation <value, type> give the value of the C++ expression 5 / 2.

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;

// Convert to Canadian dollars and output


float CanadianDollars = USDollars * 1.54;
cout << “$” << USDollars << “ U.S. dollars is $“
<< CanadianDollars << “ Canadian dollars.” << endl;
return 0;
}
C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
28. Using the notation <value, type> give the value of the C++ expression 2 + 5L.

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

<6.4, long double>


C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
32. Using the notation <value, type> give the value of the C++ expression 2 + 5L.

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

iostream and string


C++ Program Design/3e
Chapter 2
Answers to Self-Check Exercises
40. Write a C++ program that prompts for and accepts a time and computes the number of
seconds elapsed since midnight. The time should be entered in the format HH:MM:SS.

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;

cin >> Separator;

int Seconds;
cin >> Seconds;

int ElapsedTime = (Hours * 60 * 60) + (Minutes * 60) + Seconds;


cout << "Elapsed time since midnight is "
<< ElapsedTime << " seconds." << endl;

return 0;
}

You might also like