-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathExercise 5.3 (Completed)
670 lines (575 loc) · 18.2 KB
/
Exercise 5.3 (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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
1. (Practice) Write a for statement for each of the following cases:
a. Use a counter named i that has an initial value of 1, a final value of 20, and an increment of 1.
for (i = 1; i <= 20; i++)
b. Use a counter named icount that has an initial value of 1, a final value of 20, and an increment of 2.
for (icount = 1; icount <= 20; icount = icount +2)
c. Use a counter named j that has an initial value of 1, a final value of 100, and an increment of 5.
for(j=1; j<= 100; j=j+5)
d. Use a counter named icount that has an initial value of 20, a final value of 1, and an increment of -1.
for(icount = 20; icount >= 1; icount--)
e. Use a counter named icount that has an initial value of 20, a final value of 1, and an increment of -2.
for(icount = 20; icount >= 1; icount = icount - 2)
f. Use a counter named count that has an initial value of 1.0, a final value of 16.2, and an increment of 0.2.
for(count = 1.0; count <= 16.2 ; count = count + 0.2)
g. Use a counter named xcnt that has an initial value of 20.0, a final value of 10.0, and an increment of -0.5.
for(xcnt = 20.0; xcont >= 10.0; xcnt = xcnt - 0.5)
2. (Desk check) Determine the number of times each for loop is executed for the for statements written in Exercise 1.
a. 20
b. 10
c. 20
d. 20
e. 10
f. 77
g. 21
3. (Desk check) Determine the value in total after each of the following loops is executed:
a. total = 0;
for (i = 1; i <= 10; i = i + 1)
total = total + 1;
10
b. total = 1;
for (count = 1; count <= 10; count = count + 1)
total = total * 2;
1024
c. total = 0;
for (i = 10; i <= 15; i = i + 1)
total = total + i;
75
d. total = 50;
for (i = 1; i <=10; i = i + 1)
total = total - i;
-5
e. total = 1;
for (icnt = 1; icnt <= 8; ++icnt)
total = total * icnt;
75
f. total = 1.0;
for (j = 1; j <= 5; ++j)
total = total / 2.0;
.03125
4. (Desk check) Determine the output of the following program:
#include <iostream>
using namespace std;
int main()
{
int i;
for (i = 20; i >= 0; i -= 4)
cout << i;
return 0;
}
20, 16, 12, 8, 4, 0
5. (Modify) Modify Program 5.10 to produce a table of the numbers 0 through 20 in increments
of 2, with their squares and cubes.
#include <iostream>
using namespace std;
int main()
{
const int MAXNUMS = 20;
int num;
cout << endl; // print a blank line
cout << "NUMBER SQUARE CUBE\n"
<< "------ ------ ----\n";
for (num = 0; num <= MAXNUMS; num = num + 2)
cout << setw(3) << num << " "
<< setw(3) << num * num << " "
<< setw(4) << num * num * num << endl;
system("PAUSE");
return 0;
}
6. (Modify) Modify Program 5.10 to produce a table of numbers from 10 to 1, instead of 1 to 10,
as it currently does.
#include <iostream>
using namespace std;
int main()
{
const int MAXNUMS = 0;
int num;
cout << endl; // print a blank line
cout << "NUMBER SQUARE CUBE\n"
<< "------ ------ ----\n";
for (num = 10; num >= MAXNUMS; num--)
cout << setw(3) << num << " "
<< setw(3) << num * num << " "
<< setw(4) << num * num * num << endl;
system("PAUSE");
return 0;
}
7. (Program) a. Write, compile, and run a C++ program that displays a table of 20 temperature
conversions from Fahrenheit to Celsius. The table should start with a Fahrenheit value of
20 degrees and be incremented in values of 4 degrees. Recall that Celsius = (5.0/9.0) ×
(Fahrenheit - 32.0).
#include <iostream>
using namespace std;
int main()
{
int MAXNUMS = 96;
int num;
double Celsius, Fahrenheit;
cout << endl; // print a blank line
cout << "Celsius Fahrenheit \n"
<< "------ ------ \n";
for (Fahrenheit = 20; Fahrenheit <= MAXNUMS; Fahrenheit = Fahrenheit + 4)
{
Celsius = (5.0 / 9.0)*(Fahrenheit - 32.0);
cout << setw(10) << Celsius
<< setw(10) << Fahrenheit << endl;
}
system("PAUSE");
return 0;
}
b. Modify the program written for Exercise 7a to request the number of conversions to be made.
#include <iostream>
using namespace std;
int main()
{
int MAXNUMS;
int num;
double Celsius, Fahrenheit;
cout << endl; // print a blank line
cout << "Enter number of coversions to be made" << endl;
cin >> MAXNUMS;
cout << "Celsius Fahrenheit \n"
<< "------ ------ \n";
for (Fahrenheit = 20; Fahrenheit <= MAXNUMS; Fahrenheit = Fahrenheit + 4)
{
Celsius = (5.0 / 9.0)*(Fahrenheit - 32.0);
cout << setw(10) << Celsius
<< setw(10) << Fahrenheit << endl;
}
system("PAUSE");
return 0;
}
8. (Program) Write, compile, and run a C++ program that converts Fahrenheit to Celsius temperature
in increments of 5 degrees. The initial value of Fahrenheit temperature and the total
conversions to be made should be requested as user input during program execution. Recall
that Celsius = (5.0/9.0) × (Fahrenheit - 32.0).
#include <iostream>
using namespace std;
int main()
{
int MAXNUMS;
int num;
double Celsius, Fahrenheit;
cout << endl; // print a blank line
cout << "Enter Fahrenheit"<< endl;
cin >> Fahrenheit;
cout << "Enter total conversions" << endl;
cin >> MAXNUMS;
cout << "Celsius Fahrenheit \n"
<< "------ ------ \n";
for (Fahrenheit; Fahrenheit <= MAXNUMS; Fahrenheit = Fahrenheit + 5)
{
Celsius = (5.0 / 9.0)*(Fahrenheit - 32.0);
cout << setw(10) << Celsius
<< setw(10) << Fahrenheit << endl;
}
system("PAUSE");
return 0;
}
9. (Program) a. Write, compile, and run a C++ program that accepts five values of gallons, one
at a time, and converts each value entered to its liter equivalent before the next value is
requested. Use a for loop in your program. There are 3.785 liters in 1 gallon of liquid.
#include <iostream>
using namespace std;
int main(){
int MAXNUMS = 5;
double liters, gallons;
cout << "\nThis program wil ask you to enter" << MAXNUMS << "numbers.\n";
for (gallons = 0; gallons <= MAXNUMS; gallons++)
{
cout << "\nEnter a number: ";
cin >> gallons;
liters = 3.785 * gallons;
cout << liters << endl;
cout << gallons << endl;
}
system("PAUSE");
return 0;
}
b. Modify the program written for Exercise 9a to request the number of data items to be
entered and converted first.
#include <iostream>
using namespace std;
int main()
{
int MAXNUMS;
double liters, gallons;
cout << "Enter number of data items to be enter and converted" << endl;
cin >> MAXNUMS;
cout << "\nThis program wil ask you to enter " << MAXNUMS << " numbers.\n";
for (gallons = 0; gallons <= MAXNUMS; gallons++)
{
cout << "\nEnater a number: ";
cin >> gallons;
liters = 3.785 * gallons;
cout << liters << endl;
cout << gallons << endl;
}
system("PAUSE");
return 0;
}
10. (Program) a. An old Arabian legend has it that a fabulously wealthy but unthinking king
agreed to give a beggar 1 cent and double the amount for 64 days. Using this information,
write, compile, and run a C++ program that displays how much the king must pay the beggar
on each day. The output of your program should appear as follows:
Day Amount Owed
--- -----------
1 0.01
2 0.02
3 0.04
. .
. .
. .
64 .
#include <iostream>
using namespace std;
int main()
{
int MAXNUMS = 64;
double amount_owed = .01;
cout << "Day Amount Owed" << endl;
cout << "------- - -----------------" << endl;
for (int day = 1; day<= MAXNUMS; day++)
{
amount_owed = amount_owed * 2;
cout << setw(5)<< day
<< setw(20) << amount_owed << endl;
}
system("PAUSE");
return 0;
}
b. Modify the program you wrote for Exercise 10a to determine on which day the king will
have paid the beggar a total of one million dollars.
#include <iostream>
using namespace std;
int main()
{
double MAXNUMS = 64;
double amount_owed = .01;
double total = 0;
cout << "Day Amount Owed" << endl;
cout << "------- - -----------------" << endl;
for (int day = 1; day<= MAXNUMS; day++)
{
amount_owed = amount_owed * 2;
total = total + amount_owed;
cout << setw(5)<< day
<< setw(20) << amount_owed
<< setw(20) << total <<endl;
if (total >= 1000000){
cout << "I just gave you a million dollars or more on this day " << day << endl;
}
}
system("PAUSE");
return 0;
}
11. (Debug) Is the following program correct? If it is, determine its output. If it’s not, determine
the error and correct it so that the program will run.
#include <iostream>
using namespace std;
int main()
{
for(int i = 1; i < 10; i++)
cout << i << ‘\n’;
for (j = 1; i < 5; i++)
cout << i << endl;
return 0;
}
----------------Corrected--------------------
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i < 10; i++){
cout << i << '\n';
}
for (int j = 1; j < 5; j++){
cout << j << endl;
}
system("PAUSE");
return 0;
}
12. (Program) a. Write, compile, and run a C++ program that calculates and displays the amount
of money available in a bank account that initially has $1000 deposited and earns interest at
the rate of 3% a year. Your program should display the amount available at the end of each year
for a period of 10 years. Use the relationship that the money available at the end of each year
= the amount of money in the account at the start of the year + .03 × the amount available at
the start of the year.
#include <iostream>
using namespace std;
int main()
{
double money = 1000;
for (double year= 1; year <= 10; year++){
money = money + .03 * money;
cout << money<< '\n';
}
system("PAUSE");
return 0;
}
b. Modify the program written for Exercise 12a to prompt the user for the amount of money
initially deposited in the account.
#include <iostream>
using namespace std;
int main()
{
double money;
cout << "Enter your initial money in your account" << endl;
cin >> money;
for (double year= 1; year <= 10; year++){
money = money + .03 * money;
cout << money<< '\n';
}
system("PAUSE");
return 0;
}
c. Modify the program written for Exercise 12a to prompt the user for both the amount of
money initially deposited and the number of years to be displayed.
#include <iostream>
using namespace std;
int main()
{
double money,numyears;
cout << "Enter your initial money in your account" << endl;
cin >> money;
cout << "Enter the number of years too" << endl;
cin >> numyears;
for ( double years= 1; years <= numyears; years++){
money = money + .03 * money;
cout << money<< '\n';
}
system("PAUSE");
return 0;
}
d. Modify the program written for Exercise 12a to prompt the user for the amount of money
initially deposited, the interest rate to be used, and the number of years to be displayed.
#include <iostream>
using namespace std;
int main()
{
double money,numyears,interest;
cout << "Enter your initial money in your account" << endl;
cin >> money;
cout << "Enter the number of years too" << endl;
cin >> numyears;
cout << "Enter the intrest rate last one promise" << endl;
cin >> interest;
for ( double years= 1; years <= numyears; years++){
money = money + interest * money;
cout << money<< '\n';
}
system("PAUSE");
return 0;
}
13. (Program) According to legend, the island of Manhattan was purchased from the native
Indian population in 1626 for $24. Assuming this money was invested in a Dutch bank paying
4% simple interest per year, construct a table showing how much money the native population
would have at the end of each 50-year period, starting in 1626 and ending 400 years later. Use
the relationship that the money available at the end of each 50-year period = the amount of
money in the account at the start of period × the quantity (1 + .04)50.
#include <iostream>
using namespace std;
int main()
{
double money = 24,numyears = 10;
for ( double years= 1626; years <= numyears; years = years + 50){
money = 24 * money *(1 + .04)*50;
cout << money<< '\n';
cout << years << endl;
}
system("PAUSE");
return 0;
}
14. (Program) A well-regarded manufacturer of widgets has been losing 4% of its sales each year.
The company’s annual profit is 10% of sales. This year, the company has had $10 million in
sales and a profit of $1 million. Determine the expected sales and profit for the next 10 years.
Your program should produce a display in the following form:
SALES AND PROFIT PROJECTION
---------------------------
YEAR EXPECTED SALES PROJECTED PROFIT
---- -------------- ----------------
1 $10000000.00 $1000000.00
2 $ 9600000.00 $ 960000.00
3 . .
. . .
. . .
. . .
10 . .
--------------------------------------------------
Totals: $ . $ .
#include <iostream>
using namespace std;
int main()
{
double total1 = 0,total2 = 0;
double numyears = 10, expected = 10000000, profit = 1000000,loses,loses2;
cout << " SALES AND PROFIT PROJECTION" << endl;
cout << " -----------------------------" << endl;
cout << "YEAR EXPECTED SALES PROJECTED PROFIT" << endl;
cout << "-------- -------------------- ---------------------" << endl;
for ( double years= 1; years <= numyears; years++){
loses = years * expected * .04;
loses2 = years * profit *.04;
profit = profit - loses2;
expected = expected - loses;
total1 = total1 + expected ;
total2 = total2 + profit;
cout << years<<
setw(30) << expected <<
setw(30) << profit << endl;
}
cout << "-----------------------------------------------------" << endl;
cout << "Totals: $" << total1 << " $" << total2 << endl;
system("PAUSE");
return 0;
}
15. (Program) Four experiments are performed, and each experiment has six test results. The
results for each experiment are given in the following list. Write, compile, and run a C++ program
using a nested loop to compute and display the average of the test results for each
experiment.
1st experiment results: 23.2 31 16.9 27.5 25.4 28.6
2nd experiment results: 34.8 45.2 27.9 36.8 33.4 39.4
3rd experiment results: 19.4 16.8 10.2 20.8 18.9 13.4
4th experiment results: 36.9 39.5 49.2 45.1 42.7 50.6
#include <iostream>
using namespace std;
int main()
{
double average = 0;
double result = 0;
double solution;
for (int i = 1; i <= 4; i++){
for (int j = 1; j <= 6; j++){
cout << i <<"st "<< " experiment results:";
cin >> result;
average = average + result;
solution = average / 6;
}
cout << solution << endl;
}
system("PAUSE");
return 0;
}
16. (Modify) Modify the program written for Exercise 15 so that the number of test results for
each experiment is entered by the user. Write your program so that a different number of test
results can be entered for each experiment.
#include <iostream>
using namespace std;
int main()
{
double average = 0;
double result = 0;
double solution,num,test;
cout << "Enter number of experiments" << endl;
cin >> num;
cout << "Enter number of different test" << endl;
cin >> test;
for (int i = 1; i <= num; i++){
for (int j = 1; j <= test; j++){
cout << i <<"st "<< " experiment results:";
cin >> result;
average = average + result;
solution = average / 6;
}
cout << solution << endl;
}
system("PAUSE");
return 0;
}
17. (Program) a. A bowling team consists of five players, and each player bowls three games.
Write, compile, and run a C++ program that uses a nested loop to enter each player’s scores
and then computes and displays the average score for each bowler. Assume each bowler has
the following scores:
1st bowler: 286 252 265
2nd bowler: 212 186 215
3rd bowler: 252 232 216
4th bowler: 192 201 235
5th bowler: 186 236 272
#include <iostream>
using namespace std;
int main()
{
double average = 0;
double result = 0;
double solution;
for (int i = 1; i <= 5; i++){
for (int j = 1; j <= 3; j++){
cout << i <<"st "<< " bowler:";
cin >> result;
average = average + result;
solution = (average / 3);
}
cout << solution << endl;
}
system("PAUSE");
return 0;
}
b. (Modify) Modify the program written for Exercise 17a to calculate and display the average
team score. (Hint: Use a second variable to store the total of all players’ scores.)
#include <iostream>
using namespace std;
int main()
{
double average = 0;
double result = 0;
double solution;
double total = 0;
for (int i = 1; i <= 5; i++){
for (int j = 1; j <= 3; j++){
cout << i <<"st "<< " bowler:";
cin >> result;
average = average + result;
solution = (average / 3);
}
total = total + solution;
cout << solution << endl;
cout << total << endl;
}
system("PAUSE");
return 0;
}
c. (Modify) Rewrite the program written for Exercise 17a to eliminate the inner loop. To do
this, you have to input three scores for each bowler rather than enter one at a time. Each
score must be stored in its own variable name before the average is calculated.
#include <iostream>
using namespace std;
int main()
{
double average = 0;
double result, result2,result3;
double solution;
for (int i = 1; i <= 5; i++){
cout << i <<"st "<< " bowler:";
cin >> result >> result2 >> result3;
average = average + result + result2 + result3;
solution = (average / 3);
cout << solution << endl;
}
system("PAUSE");
return 0;
}
18. (Program) Write, compile, and run a C++ program that calculates and displays the yearly
amount available if $1000 is invested in a bank account for 10 years. Your program should
display the amounts available for interest rates from 6% to 12%, inclusive, in 1% increments.
Use a nested loop, with the outer loop having a fixed count of 7 and the inner loop having a
fixed count of 10. The first iteration of the outer loop should use an interest rate of 6% and
display the amount of money available at the end of the first 10 years. In each subsequent pass
through the outer loop, the interest rate should be increased by 1%. Use this relationship:
money available at end of each year = amount of money in account at start of the year + interest
rate × amount available at start of the year.
#include <iostream>
using namespace std;
int main()
{
double money = 1000;
double total = 0;
for (double interest =.06; interest <= .12; interest = interest + .01){
money = money + interest * money;
for (double j = 1; j <= 10; j ++){
total = total + money;
cout << total << endl;
}
}
system("PAUSE");
return 0;
}
//something like that lol