-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 3.2 (Completed)
277 lines (244 loc) · 9.58 KB
/
Exercise 3.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
1. (Debug) Determine the errors in the following statements:
a. cout << “\n << “ 15)
cout << "\n" << 15; // Quotes misplaced, substitute
// semicolon for parenthesis
b. cout << “setw(4)” << 33;
cout << setw(4) << 33; // No quotes
c. cout << “setprecision(5)” << 526.768;
cout << setprecision(5) << 526.768 // No quotes
d. “Hello World!” >> cout;
cout << "Hellow World!"; // Statement reversed
e. cout << 47 << setw(6);
cout << setw(6) << 47; // Statement out of order
f. cout << set(10) << 526.768 << setprecision(2);
cout << setw(10) << setprecision(2) << 526.768; // set should be setw, statement out of order
2. (Desk check) Determine and write out the display produced by the following statements:
a. cout << “|” << 5 <<”|”;
|5|
b. cout << “|” << setw(4) << 5 << “|”;
| 5|
c. cout << “|” << setw(4) << 56829 << “|”;
|56829|
d. cout << “|” << setw(5) << setiosflags(ios::fixed)
<< setprecision(2) << 5.26 << “|”;
| 5.26|
e. cout << “|” << setw(5) << setiosflags(ios::fixed)
<< setprecision(2) << 5.267 << “|”;
| 5.27|
f. cout << “|” << setw(5) << setiosflags(ios::fixed)
<< setprecision(2) << 53.264 << “|”;
|53.26|
g. cout << “|” << setw(5) << setiosflags(ios::fixed)
<< setprecision(2) << 534.264 << “|”;
|534.26|
h. cout << “|” << setw(5) << setiosflags(ios::fixed)
<< setprecision(2) << 534. << “|”;
|534.00|
3. (Desk check) Write out the display produced by the following statements:
a. cout << “The number is “ << setw(6) << setiosflags(ios::fixed)
<< setprecision(2) << 26.27 << endl;
cout << “The number is “ << setw(6) << setiosflags(ios::fixed)
<< setprecision(2) << 682.3 << endl;
cout << “The number is “ << setw(6) << setiosflags(ios::fixed)
<< setprecision(2) << 1.968 << endl;
The number is 26.27
The number is 682.30
The number is 1.97
b. cout << setw(6) << setiosflags(ios::fixed)
<< setprecision(2) << 26.27 << endl;
cout << setw(6) << setiosflags(ios::fixed)
<< setprecision(2) << 682.3 << endl;
cout << setw(6) << setiosflags(ios::fixed)
<< setprecision(2) << 1.968 << endl;
cout << “------\n”;
cout << setw(6) << setiosflags(ios::fixed)
<< setprecision(2)
<< 26.27 + 682.3 + 1.968 << endl;
26.27
682.30
1.97
------
710.54
c. cout << setw(5) << setiosflags(ios::fixed)
<< setprecision(2) << 26.27 << endl;
cout << setw(5) << setiosflags(ios::fixed)
<< setprecision(2) << 682.3 << endl;
cout << setw(5) << setiosflags(ios::fixed)
<< setprecision(2) << 1.968 << endl;
cout << “-----\n”;
cout << setw(5) << setiosflags(ios::fixed)
<< setprecision(2)
<< 26.27 + 682.3 + 1.968 << endl;
26.27
682.30
1.97
------
701.54
d. cout << setw(5) << setiosflags(ios::fixed)
<< setprecision(2) << 36.164 << endl;
cout << setw(5) << setiosflags(ios::fixed)
<< setprecision(2) << 10.003 << endl;
cout << “-----” << endl;
36.16
10.00
-----
4. (Desk check) The following chart lists the equivalent octal and hexadecimal representations
for the decimal numbers 1 through 15:
Decimal: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Octal: 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17
Hexadecimal: 1 2 3 4 5 6 7 8 9 a b c d e f
Using this chart, determine the output of the following program:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << “\nThe value of 14 in octal is “ << oct << 14
<< “\nThe value of 14 in hexadecimal is “ << hex << 14
<< “\nThe value of 0xA in decimal is “ << dec << 0xA
<< “\nThe value of 0xA in octal is “ << oct << 0xA
<< endl;
return 0;
}
The value of 14 in octal is 16
The value of 14 in headecimal is e
The value of 0xA in decimal is 10
The value of 0xA in octal is 12
5. (Program) Write a C++ program to calculate and display the value of the slope of the line
connecting two points with the coordinates (3,7) and (8,12). Use the fact that the slope
between two points at the coordinates (x1,y1) and (x2,y2) is slope = (y2 - y1) / (x2 - x1). Your
program should produce this display:
The value of the slope is xxx.xx
The xxx.xx denotes placing the calculated value in a field wide enough for three places to
the left of the decimal point and two places to the right of it.
#include <iostream>
using namespace std;
int main(){
double x1,y1,x2,y2,slope;
x1 = 3;
x2 = 8;
y1 = 7;
y2 = 12;
slope = (y2-y1) / (x2 - x1);
cout << "The value of the slope is" <<setw(5) <<setiosflags(ios::fixed)<<setprecision(2) <<slope <<endl;
return 0;
}
6. (Program) Write a C++ program to calculate and display the midpoint coordinates of the line
connecting the two points with coordinates of (3,7) and (8,12). Use the fact that the midpoint
coordinates between two points with the coordinates (x1,y1) and (x2,y2) are ((x2 + x1) / 2,
(y2 + y1) / 2). Your program should produce this display:
The x coordinate of the midpoint is xxx.xx
The y coordinate of the midpoint is xxx.xx
The xxx.xx denotes placing the calculated value in a field wide enough for three places to
the left of the decimal point and two places to the right of it.
Verify your program using the following test data:
Test data set 1: Point 1 = (0,0) and Point 2 = (16,0)
Test data set 2: Point 1 = (0,0) and Point 2 = (0,16)
Test data set 3: Point 1 = (0,0) and Point 2 = (-16,0)
Test data set 4: Point 1 = (0,0) and Point 2 = (0,-16)
Test data set 5: Point 1 = (-5,-5) and Point 2 = (5,5)
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double x1, x2, y1, y2, midpoint_x, midpoint_y;
cout << "Enter x1:"<<endl;
cin >> x1;
cout << "Enter y1:"<<endl;
cin >> y1;
cout << "Enter x2:"<<endl;
cin >> x2;
cout << "Enter y2:"<<endl;
cin >> y2;
midpoint_x = (x2 + x1) / 2;
midpoint_y = (y2 + y1) / 2;
cout << "The x coordinate of the midpoint is" << setw(5) << setiosflags(ios::fixed) << setprecision(2) << midpoint_x << endl;
cout << "The y coordinate of the midpoint is" << setw(5) << setiosflags(ios::fixed) << setprecision(2) << midpoint_y << endl;
system("PAUSE");
return 0;
}
When you have completed your verification, use your program to complete the following table.
Point 1 Point 2 Midpoint
------- ------- --------
(4,6) (16,18) (10,12)
(22,3) (8,12) (15, 7.50)
(-10,8) (14,4) (2,6)
(-12,2) (14,3.1) (1.00,2.55)
(3.1,-6) (20,16) (11.55,5.00)
(3.1,-6) (-16,-18) (-6.45,-12.00)
7. (Program) The change remaining after an amount is used to pay a restaurant bill of amount
check can be calculated by using the following C++ statements:
// determine the number of pennies in the change
change = (paid - check) * 100;
// determine the number of dollars in the change
dollars = (int) (change/100);
a. Using the previous statements as a starting point, write a C++ program that calculates the
number of dollar bills, quarters, dimes, nickels, and pennies in the change when $10 is used
to pay a bill of $6.07.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double change,paid, check;
paid = 10;
check = 6.07;
change = (paid - check) * 100; // (10-6.07) *100 = 393 cents
int dollars = (change / 100); // 3 dolars
int quart = (change / 25); // 15 quarters
int dim = (change / 10); // 39 dimes
int nick = (change / 5); // 78 nickles
int pen = (change / 1); // 393 pennies
cout << "Total change "<< change << endl;
cout << "Total dollars " << dollars << endl;
cout << "Total quarters " << quart << endl;
cout << "Total dimes " << dim << endl;
cout << "Total nickles " << nick <<endl;
cout << "Total pennies " << pen << endl;
system("PAUSE");
return 0;
}
b. Without compiling or running your program, check the effect, by hand, of each statement in
the program and determine what’s stored in each variable as each statement is encountered.
#include <iostream>
using namespace std;
int main(){
double change, dollars, paid, check ;
paid = 10;
check = 6.07;
change = (paid - check) * 100; // (10-6.07) *100 = 393 cents
int dollars = (change/100); // 3 dolars
int quart = (change/25); // 15 quarters
int dim = (change/10); // 39 dimes
int nick = (change/5); // 78 nickles
int pen = (change/1); // 393 pennies
return 0;
}
c. When you have verified that your algorithm works correctly, compile and run your program.
Verify that the result produced by your program is correct, and then use your program to
determine the change when a check of $12.36 is paid with a 20-dollar bill.
total change = 764
total dollars = 7
total quarters = 30
total dimes = 76
total nickles = 152
total pennies = 764
8. (Program) Write a C++ program to calculate and display the maximum bending moment, M,
of a beam that’s supported on both ends (see Figure 3.6). The formula is M = XW (L - X) / L,
where X is the distance from the end of the beam that a weight, W, is placed, and L is the
length of the beam. Your program should produce this display:
The maximum bending moment is xxxx.xxxx
The xxxx.xxxx denotes placing the calculated value in a field wide enough for four places to
both the right and left of the decimal point.
*See texbook for Figure*
#include <iostream>
using namespace std;
int main(){
int M, X = 10, W =30 , L = 20;
M= X * W *( L - X ) / L;
cout << "The maximum bending moment is" << setw(6) << setiosflags(ios::fixed) << setprecision(3)<< M << endl;
return 0;
}