-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 2.2 (Completed)
116 lines (115 loc) · 4.38 KB
/
Exercise 2.2 (Completed)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
1. (Practice) For the following correct algebraic expressions and corresponding incorrect C++
expressions, find the errors and write corrected C++ expressions:
Algebra C++ Expression
a. (2)(3) + (4)(5) (2 * 3) + (4 * 5)
b. 6 + 18/2 (6 + 18)/ 2
c. 4.5/12.2 - 3.1 4.5 / (12.2 - 3.1)
d. 4.6(3.0 + 14.9) 4.6 * (3.0 + 14.9)
e. (12.1 + 18.9)(15.3 - 3.8) (12.1 + 18.9) * (15.3 - 3.8)
2. (Practice) Determine the values of the following integer expressions:
a. 3 + 4 * 6 = 27 f. 20 - 2 / (6 + 3) = 20
b. 3 * 4 / 6 + 6 = 8 g. (20 - 2) / 6 + 3 = 6
c. 2 * 3 / 12 * 8 / 4 = 1 h. (20 - 2) / (6 + 3) = 2
d. 10 * (1 + 7 * 3) = 220 i. 50 % 20 = 10
e. 20 - 2 / 6 + 3 = 23 j. (10 + 3) % 4 = 1
3. (Practice) Determine the value of the following floating-point expressions:
a. 3.0 + 4.0 * 6.0 = 27.0
b. 3.0 * 4.0 / 6.0 + 6.0 = 8.0
c. 2.0 * 3.0 / 12.0 * 8.0 / 4.0 = 1.0
d. 10.0 * (1.0 + 7.0 * 3.0) = 220.0
e. 20.0 - 2.0 / 6.0 + 3.0 = 23.0
f. 20.0 - 2.0 / (6.0 + 3.0) = 20.0
g. (20.0 - 2.0) / 6.0 + 3.0 = 6.0
h. (20.0 - 2.0) / (6.0 + 3.0) = 2.0
4. (Practice) Evaluate the following mixed-mode expressions and list the data type of the result.
In evaluating the expressions, be aware of the data types of all intermediate calculations.
a. 10.0 + 15 / 2 + 4.3 = 21.8 float point
b. 10.0 + 15.0 / 2 + 4.3 = 21.8 floating point
c. 3.0 * 4 / 6 + 6 = 8.0 floating point
d. 3 * 4.0 / 6 + 6 = 8.0 floating point
e. 20.0 - 2 / 6 + 3 = 22.6666667 floating point
f. 10 + 17 * 3 + 4 = 65 integer
g. 10 + 17 / 3.0 + 4 = 19.6666667 floating point
h. 3.0 * (4 % 6) + 6 = 18.0 floating point
i. 10 + 17 % 3 + 4 = 16 integer
5. (Practice) Assume that amount stores the integer value 1, m stores the integer value 50, n stores
the integer value 10, and p stores the integer value 5. Evaluate the following expressions:
a. n / p + 3 = 10 / 5 + 3 = 5
b. m / p + n - 10 * amount = 50/ 5 + 10 -10 * 1 = 10
c. m - 3 * n + 4 * amount = 50 - 3 * 10 + 4 * 1 = 24
d. amount / 5 = 1/5 = .2
e. 18 / p = 18/5 = 3.6
f. -p * n = =-5 * 10 = -50
g. -m / 20 = -50/20 = -2.5
h. (m + n) / (p + amount) = (50 + 10)/(5+1) = 10
i. m + n / p + amount = 50 + 10 / 5 + 1 = 53
6. (Practice) Repeat Exercise 5, assuming that amount stores the value 1.0, m stores the value
50.0, n stores the value 10.0, and p stores the value 5.0.
a. 5.0
b. 10.0
c. 24.0
d. .2
e.3.6
f.-50
g.-2.5
h. 10.0
i. 53.0
7. (Practice) Enter, compile, and run Program 2.2.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
cout << "15.0 plus 2.0 equals " << (15.0 + 2.0) << endl
<< "15.0 minus 2.0 equals " << (15.0 - 2.0) << endl
<< "15.0 times 2.0 equals " << (15.0 * 2.0) << endl
<< "15.0 divided by 2.0 equals " << (15.0 / 2.0) << endl;
system("PAUSE");
return 0;
}
8. (Desk check) Determine the output of the following program:
#include <iostream>
using namespace std;
int main() // a program illustrating integer truncation
{
cout << “answer1 is the integer “ << 9/4;
cout << “\nanswer2 is the integer “ << 17/3;
return 0;
}
answer1 is the integer 2
answer2 is the integer 5
9. (Desk check) Determine the output of the following program:
#include <iostream>
using namespace std;
int main() // a program illustrating the % operator
{
cout << “The remainder of 9 divided by 4 is “ << 9 % 4;
cout << “\nThe remainder of 17 divided by 3 is “ << 17 % 3;
return 0;
}
The remainder of 9 divided by 4 is 1
The remainder of 17 divided by 3 is 2
10. (Program) Write a C++ program that displays the results of the expressions 3.0 * 5.0,
7.1 * 8.3 - 2.2, and 3.2 / (6.1 * 5). Calculate the value of these expressions manually
to verify that the displayed values are correct.
-------------------------------------------------------------
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
cout << "The results of the hidden code " << 3.0 * 5.0<<", "<< 7.1 * 8.3 - 2.2<<" and "<< 3.2/(6.1*5)<< endl;
system("PAUSE");
return 0;
}
11. (Program) Write a C++ program that displays the results of the expressions 15 / 4, 15 % 4,
and 5 * 3 - (6 * 4). Calculate the value of these expressions manually to verify that the
displayed values are correct.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
cout << "The results of the hidden code " << 15/4<<", "<<15%4<<" and "<< 5*3 - (6*4)<< endl;
system("PAUSE");
return 0;