-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 5.2 (Completed)
487 lines (420 loc) · 13.4 KB
/
Exercise 5.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
1. (Modify) Rewrite Program 5.6 to compute the total of eight numbers.
#include <iostream>
using namespace std;
int main()
{
const int MAXNUMS = 8;
int count;
double num, total;
cout << “\nThis program will ask you to enter “
<< MAXNUMS << “ numbers.\n”;
count = 1;
total = 0;
while (count <= MAXNUMS)
{
cout << “\nEnter a number: “;
cin >> num;
total = total + num;
cout << “The total is now “ << total;
count++;
}
cout << “\n\nThe final total is “ << total << endl;
return 0;
}
2. (Modify) Rewrite Program 5.6 to display this prompt:
Please type in the total number of data values to be added:
In response to this prompt, the program should accept a user-entered number, and then use it
to control the number of times the while loop is executed. So if the user enters 5 in response
to the prompt, the program should request the input of five numbers and display the total after
five numbers have been entered.
#include <iostream>
using namespace std;
int main()
{
int MAXNUMS;
int count;
double num, total;
cout << "Please type in the total number of dat values to be added : ";
cin >> MAXNUMS;
cout << "\nThis program will ask you to enter "
<< MAXNUMS << " numbers.\n";
count = 1;
total = 0;
while (count <= MAXNUMS)
{
cout << "\nEnter a number : ";
cin >> num;
total = total + num;
cout << "The total is now " << total;
count++;
}
cout << "\n\nThe final total is "<< total << endl;
system("PAUSE");
return 0;
}
3. (Modify) Rewrite Program 5.7 to compute the average of 10 numbers.
#include <iomanip>
#include <math.h> /* pow & sqrt */
#include <iostream>
using namespace std;
int main()
{
const int MAXNUMS = 10;
int count;
double num, total, average;
cout << "\nThis program will ask you to enter "
<< MAXNUMS << "numbers.\n\n";
count = 1;
total = 0;
while (count <= MAXNUMS)
{
cout << "Enter a number : ";
cin >> num;
total = total + num;
count++;
}
count--;
average = total / count;
cout << "\nThe average of the numbers is "
<< average << endl;
system("PAUSE");
return 0;
}
4. (Modify) Rewrite Program 5.7 to display the following prompt:
Please type in the total number of data values to be averaged:
In response to this prompt, the program should accept a user-entered number, and then use it
to control the number of times the while loop is executed. So if the user enters 6 in response
to the prompt, the program should request an input of six numbers and display the average of
the next six numbers entered.
#include <iostream>
using namespace std;
int main()
{
int MAXNUMS;
int count;
double num, total, average;
cout << "Pleae type in the totla number of data values to be averaged:" << endl;
cin >> MAXNUMS;
cout << "\nThis program will ask you to enter "
<< MAXNUMS << "numbers.\n\n";
count = 1;
total = 0;
while (count <= MAXNUMS)
{
cout << "Enter a number : ";
cin >> num;
total = total + num;
count++;
}
count--;
average = total / count;
cout << "\nThe average of the numbers is "
<< average << endl;
system("PAUSE");
return 0;
}
5. (Debug) By mistake, a programmer puts the statement average = total / count; in the
while loop immediately after the statement total = total + num; in Program 5.7. As a
result, the while loop becomes the following:
while (count <= MAXNUMS)
{
cout << “Enter a number: “;
cin >> num;
total = total + num;
average = total / count;
count++;
}
a. Will the program yield the correct result with this while loop?
Yes, the program yields the correct results because the last time through the loop, it takes
the average of the final total.
b. From a programming perspective, which while loop is better to use, and why?
Having the average calculate outside the loop is better because the program does the calculation
once instead of each time the loop executes.
6. (Program) a. Write a C++ program to convert meters to feet. The program should request the
starting meter value, the number of conversions to be made, and the increment between metric
values. The display should have appropriate headings and list the meters and corresponding
feet value. If the number of iterations is greater than 10, have your program substitute a
default increment of 10. Use the relationship that 1 meter = 3.281 feet.
#include <iostream>
using namespace std;
int main()
{
int Startmeters;
int Maxnumber;
int increment;
double feet,meters;
cout << "Enter starting meter value" << endl;
cin >> Startmeters;
cout << "Enter the number of conversions to be made" << endl;
cin >> Maxnumber;
cout << "Enter the increment between metric values" << endl;
cin >> increment;
if (increment > 10){
increment = 10;
}
meters = Startmeters;
while (meters <= Maxnumber){
feet = meters * 3.2808399;
cout << meters << endl;
cout << feet << endl;
meters = meters + increment;
}
system("PAUSE");
return 0;
}
b. Run the program written in Exercise 6a on a computer. Verify that your program begins at
the correct starting meter value and contains the exact number of conversions specified in
your input data.
Done!!
7. (Modify) a. Modify the program written in Exercise 6a to request the starting meter value,
the ending meter value, and the increment. Instead of the condition checking for a fixed
count, it checks for the ending meter value. If the number of iterations is greater than 20, have
your program substitute a default increment of (ending value - starting value) / 19.
#include <iostream>
using namespace std;
int main()
{
int Startmeters;
int Maxnumber;
int increment;
double feet,meters;
cout << "Enter starting meter value" << endl;
cin >> Startmeters;
cout << "Enter the number of conversions to be made" << endl;
cin >> Maxnumber;
cout << "Enter the increment between metric values" << endl;
cin >> increment;
if ((Maxnumber - Startmeters) > 20)
{
increment = ((Maxnumber - Startmeters) / 19);
}
meters = Startmeters;
while (meters <= Maxnumber){
feet = meters * 3.2808399;
cout << meters << endl;
cout << feet << endl;
meters = meters + increment;
}
system("PAUSE");
return 0;
}
b. Run the program written in Exercise 7a on a computer. Verify that your output starts at the
correct beginning value and ends at the correct ending value.
Done!!
8. (Program) a. Write a C++ program to convert Celsius degrees to Fahrenheit. The program
should request the starting Celsius value, the number of conversions to be made, and the
increment between Celsius values. The display should have appropriate headings and list the
Celsius value and the corresponding Fahrenheit value. Use the relationship that Fahrenheit =
(9.0 / 5.0) * Celsius + 32.0.
#include <iostream>
using namespace std;
int main()
{
int StartCelsius;
int Maxnumber;
double increment;
double Celsius, Fahrenheit;
cout << "Enter starting Celsius value" << endl;
cin >> StartCelsius;
cout << "Enter the number of conversions to be made" << endl;
cin >> Maxnumber;
cout << "Enter the increment between metric values" << endl;
cin >> increment;
Celsius = StartCelsius;
while (Celsius <= Maxnumber){
Fahrenheit = ((9.0 / 5.0) * Celsius + 32.0);
cout << Celsius << endl;
cout << Fahrenheit << endl;
Celsius = Celsius + increment;
}
system("PAUSE");
return 0;
}
b. Compile and run the program written in Exercise 8a on a computer. Verify that your program
begins at the correct starting Celsius value and contains the exact number of conversions
specified in your input data.
Done!!
9. (Program) An arithmetic series is defined by the following:
a + (a + d ) + (a + 2d ) + (a + 3d ) + ˙˙˙ + [(a + (n - 1)d )]
a is the first term.
d is the “common difference.”
n is the number of terms to be added.
Using this information, write a C++ program that uses a while loop to display each term and
determine the sum of the arithmetic series having a = 1, d = 3, and n = 100. Make sure your
program displays the value it has calculated.
#include <iostream>
using namespace std;
int main()
{
int Startnum = 1;
int Maxnumber = 100;
double increment = 1;
double a=1,d=3,n,Solution;
n = Startnum;
while (n <= Maxnumber){
Solution = (a + (n - 1)*d);
cout << a << endl;
cout << d << endl;
cout << n << endl;
cout << Solution << endl;
n = n + increment;
}
system("PAUSE");
return 0;
}
10. (Program) A geometric series is defined by the following:
a + ar + ar 2 + ar 3 + ˙˙˙ + ar n - 1
a is the first term.
r is the “common ratio.”
n is the number of terms in the series.
Using this information, write a C++ program that uses a while loop to display each term and
determine the sum of a geometric series having a = 1, r = .5, and n = 10. Make sure your program
displays the value it has calculated.
#include <iostream>
using namespace std;
int main()
{
int Startnum = 1;
int Maxnumber = 10;
double increment = 1;
double a=1,r=.5,n,Solution;
n = Startnum;
while (n <= Maxnumber){
Solution = pow((a*r ) ,(n - 1));
cout << a << endl;
cout << r << endl;
cout << n << endl;
cout << Solution << endl;
n = n + increment;
}
system("PAUSE");
return 0;
}
11. (Program) a. The data in the following chart was collected on a recent automobile trip:
Mileage Gallons
22,495 Full tank
22,841 12.2
23,185 11.3
23,400 10.5
23,772 11.0
24,055 12.2
24,434 14.7
24,804 14.3
25,276 15.2
Write, compile, and run a C++ program that accepts a mileage and gallons value and calculates
the miles per gallon (mpg) for that segment of the trip. The mpg is calculated as the difference
in mileage between fill-ups divided by the number of gallons of gasoline used in the fill-up.
#include <iostream>
using namespace std;
int main()
{
double startmil = 22495;
double endmil = 25276;
double increment = 200;
double Mileage,fill_up = 15,mpg;
Mileage = startmil;
while (Mileage <= endmil){
mpg = ((endmil - startmil) / (fill_up));
cout << fill_up << endl;
cout << Mileage << endl;
cout << mpg << endl;
Mileage = Mileage + increment;
}
system("PAUSE");
return 0;
}
b. Modify the program written for Exercise 11a to also compute and display the cumulative
mpg after each fill-up. The cumulative mpg is calculated as the difference between mileage
at each fill-up and mileage at the start of the trip divided by the sum of gallons used to that
point in the trip.
#include <iostream>
using namespace std;
int main()
{
double startmil = 22495;
double endmil = 25276;
double increment = 200;
double Mileage,fill_up = 15,mpg,cumulativemil,currentgal = 2.5;
Mileage = startmil;
while (Mileage <= endmil){
currentgal = currentgal + fill_up;
cumulativemil = (endmil - Mileage) / (currentgal);
mpg = ((endmil - Mileage) / (fill_up));
cout << Mileage << endl;
cout << cumulativemil;
cout << Mileage << endl;
cout << mpg << endl;
Mileage = Mileage + increment;
}
system("PAUSE");
return 0;
}
12. (Program) a. A bookstore summarizes its monthly transactions by keeping the following
information for each book in stock:
Book identification number
Inventory balance at the beginning of the month
Number of copies received during the month
Number of copies sold during the month
Write a C++ program that accepts this data for each book and then displays the book identification
number and an updated book inventory balance, using this relationship:
New balance = Inventory balance at the beginning of the month
+ Number of copies received during the month
- Number of copies sold during the month
Your program should use a while statement with a fixed-count condition so that information
on only three books is requested.
#include <iostream>
using namespace std;
int main()
{
double start = 1;
double end = 3;
double increment = 1,newBal,invBal,numCopRec,numCopsold,id;
cout << "Inventory balance at the beginning of the month" << endl;
cin >> invBal;
cout << "Book identification number" << endl;
cin >> id;
cout << "Number of copies received during the month" << endl;
cin >> numCopRec;
cout << "Number of copies sold diring the month" << endl;
cin >> numCopsold;
id = start;
while (id <= 3){
invBal = (invBal + numCopRec - numCopsold);
cout << id << endl;
cout << invBal << endl;
id = id + increment;
}
system("PAUSE");
return 0;
}
b. Compile and run the program written in Exercise 12a. Review the display your program
produces and verify that the output is correct.
Done!!
13. (Modify) Modify the program you wrote for Exercise 12 to keep requesting and displaying
results until a sentinel value of 999 is entered. Compile and run your program. Review the
display your program produces and verify that the output is correct.
#include <iostream>
using namespace std;
int main()
{
double start = 1;
double end = 999;
double increment = 1,newBal,invBal,numCopRec,numCopsold,id;
cout << "Inventory balance at the beginning of the month" << endl;
cin >> invBal;
cout << "Book identification number" << endl;
cin >> id;
cout << "Number of copies received during the month" << endl;
cin >> numCopRec;
cout << "Number of copies sold diring the month" << endl;
cin >> numCopsold;
id = start;
while (id <= 999){
invBal = (invBal + numCopRec - numCopsold);
cout << id << endl;
cout << invBal << endl;
id = id + increment;
}
system("PAUSE");
return 0;
}