C++ Primer Plus (Six Edition) Chapter 3, Review
2016-09-10 08:10
363 查看
Q1. Why does C++ have more than one integer type?
A: In order to give users the option of choosing the integer type that best meets a program's particular requirements and save system memory also for which
may speed up the calculation.
Q2. Declare variables matching the following descriptions:
a. A short integer with the value 80
b. An unsigned int integer with the value 42,110
c. An integer with the value 3,000,000,000
A: a. short int a = 80;
b. unsigned int b = 42110;
c. long long c = 3,000,000,000;
Note: Usually you should not make int be large enough to hold 3,000,000,000.
Q3. What safeguards does C++ provide to keep you from exceeding the limits of an integer type?
A: Sateguards are not provided, still we can use the climits header file to determine what the limits are.
Q4. What is the distinction between 31L and 33?
A: The constant 33L is type long, while 33 is type int.
Q5. Consider the two C++ statements that follow:
char grade = 65;
char grade = 'A';
Are they equivalent?
A: No, they only have the same effect on some systems, and ' A ' is a type char, whereas 65 is a type int.
Q6. How could you use C++ to find out which character the code 88 represents? Come up with at least two ways.
A: Here are for ways:
char c = 88;
cout << c << endl;
cout.put(char(88));
cout << char(88) << endl;
cout << (char)88 << endl;
Q7. Assigning a long value to a float can result in a rounding error. What about assigning long to double? long long to double?
A: It depends on how large the two types are. If long is 4 bytes, there's no loss, for the largest long value would be about 2 billion, which is 10 digits.
And 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. Also note that long long is an integer type, while double is not.
Q8. Evaluate the following expressions as C++ would:
a. 8 * 9 + 2
b. 6 * 3 / 4
c. 3 / 4 * 6
d. 6.0 * 3 / 4
e. 15 % 4
A: They successively yield 74, 4, 0, 4.5, 3.
Q9. Suppose x1 and x2 are two type double variables that you want to add as integers and assign to an integer variable. Construct a C++ statement for doing so. What if
you want to add them as type double and then convert to int?
A: 1. int pos = (int) x1 + (int) x2; // or int pos = int (x1) + int (x2);
2. int pos = (int) (x1 + x2); // or int pos = int (x1 + x2);
Q10. What is the variable type for each of the following declarations?
a. auto cars = 15;
b. auto iou = 150.37f;
c. auto level = ' B ';
d. auto crat = U ' /U00002155 ' ;
e. auto fract = 8.25f / 2.5;
A: They successively are type int, float, char, char32_t and double.
A: In order to give users the option of choosing the integer type that best meets a program's particular requirements and save system memory also for which
may speed up the calculation.
Q2. Declare variables matching the following descriptions:
a. A short integer with the value 80
b. An unsigned int integer with the value 42,110
c. An integer with the value 3,000,000,000
A: a. short int a = 80;
b. unsigned int b = 42110;
c. long long c = 3,000,000,000;
Note: Usually you should not make int be large enough to hold 3,000,000,000.
Q3. What safeguards does C++ provide to keep you from exceeding the limits of an integer type?
A: Sateguards are not provided, still we can use the climits header file to determine what the limits are.
Q4. What is the distinction between 31L and 33?
A: The constant 33L is type long, while 33 is type int.
Q5. Consider the two C++ statements that follow:
char grade = 65;
char grade = 'A';
Are they equivalent?
A: No, they only have the same effect on some systems, and ' A ' is a type char, whereas 65 is a type int.
Q6. How could you use C++ to find out which character the code 88 represents? Come up with at least two ways.
A: Here are for ways:
char c = 88;
cout << c << endl;
cout.put(char(88));
cout << char(88) << endl;
cout << (char)88 << endl;
Q7. Assigning a long value to a float can result in a rounding error. What about assigning long to double? long long to double?
A: It depends on how large the two types are. If long is 4 bytes, there's no loss, for the largest long value would be about 2 billion, which is 10 digits.
And 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. Also note that long long is an integer type, while double is not.
Q8. Evaluate the following expressions as C++ would:
a. 8 * 9 + 2
b. 6 * 3 / 4
c. 3 / 4 * 6
d. 6.0 * 3 / 4
e. 15 % 4
A: They successively yield 74, 4, 0, 4.5, 3.
Q9. Suppose x1 and x2 are two type double variables that you want to add as integers and assign to an integer variable. Construct a C++ statement for doing so. What if
you want to add them as type double and then convert to int?
A: 1. int pos = (int) x1 + (int) x2; // or int pos = int (x1) + int (x2);
2. int pos = (int) (x1 + x2); // or int pos = int (x1 + x2);
Q10. What is the variable type for each of the following declarations?
a. auto cars = 15;
b. auto iou = 150.37f;
c. auto level = ' B ';
d. auto crat = U ' /U00002155 ' ;
e. auto fract = 8.25f / 2.5;
A: They successively are type int, float, char, char32_t and double.
相关文章推荐
- C++ Primer Plus (Six Edition) Chapter 4, Review
- C++ Primer Plus (Six Edition) Chapter 2, Review
- C++ Primer Plus (Six Edition) Chapter 4, Review
- C++ Primer Plus (Six Edition) Chapter 3, Programming Exercises
- C++ Primer Plus (Six Edition) Chapter 2, Programming Exercises
- C++ Primer Plus 第六版_编程练习(4)(Chapter_five 1-5)
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第五章(chapter 5) 6-10
- C++ Primer Plus 第六版_编程练习(5)(Chapter_five 6-10)
- c++ primer plus笔记 [chapter 3]
- C++ Primer Plus Chapter3
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第八章(chapter 8) 1-4
- C++ Primer Plus, Chapter 14, excercise
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第六章(chapter 6) 5-9
- 《C++ Primer Plus(第6版)》编程练习代码 Chapter 2
- C++ primer plus(sixth edition) 编程练习答案(answers for programing exercises)第七章(chapter 7) 6-10
- C++ Primer Plus, Chapter 13, excercise
- C++ Primer Plus Chapter 2 2.6复习题答案
- c++ primer plus(chapter 7)const与函数地址
- c++字符串数组与字符串指针 c++ primer plus chapter 7 复习题8 的一点小问题
- C++ Primer Plus (Chapter 9)