4.14 Ch 4 Program: Arabian Legend Students: This content is controlled by your i
ID: 3863850 • Letter: 4
Question
4.14 Ch 4 Program: Arabian Legend
Students:
This content is controlled by your instructor, and is not zyBooks content. Direct questions or concerns about this content to your instructor. If you have any technical issues with the zyLab submission system, use the "Trouble with lab?" button at the bottom of the lab.
An old Arabian legend has it that a fabulously wealthy but unthinking king agreed to pay a beggar 1 cent and double the amount everyday for 64 days.
1) Using this information write a C++ program that uses a for loop and displays how much the king must pay the beggar on each day. The output of your program should appear as follows: (3 points)
Attention: If you submit your program to see if you have completed this part before moving onto Part 2, then only pay attention to the first "Compare Output" section. The second, third, and fourth compare outputs are for the second part and rely on user input.
2) Modify the application to prompt the user for the amount the king paid the beggar on Day 1 and the # of Days the King agreed to pay the beggar. The output of your program should appear as follows: (3 points)
Explanation / Answer
// C++ code 1
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
int main()
{
int MAXIMUMDAYS = 64;
int currentDay = 1, millionDay = 0;
double amountOwed = 0.0, finalTotal = 0.0;
cout << "An Old Arabian Legend " << endl << endl;
cout << setw(5) << "Day" << setw(25) << "Amount Owed" << endl;
cout << setw(5) << "---" << setw(25) << "-----------" << endl;
for (currentDay = 1; currentDay <= MAXIMUMDAYS; currentDay++)
{
amountOwed = pow(2.0, currentDay - 1) / 100.0;
cout << setw(5) << currentDay << setw(25) << fixed << setprecision(2) << amountOwed;
finalTotal += amountOwed;
if (finalTotal >= 1000000.0 && !millionDay)
{
millionDay = currentDay;
}
cout << endl;
}
cout << endl;
cout << "The King paid the beggar a total of one million dollars on day "<< millionDay << endl << endl;
cout << "The King paid the beggar a grand sum of $"<< finalTotal << "!" << endl;
return 0;
}
/*
output:
An Old Arabian Legend
Day Amount Owed
--- -----------
1 0.01
2 0.02
3 0.04
4 0.08
5 0.16
6 0.32
7 0.64
8 1.28
9 2.56
10 5.12
11 10.24
12 20.48
13 40.96
14 81.92
15 163.84
16 327.68
17 655.36
18 1310.72
19 2621.44
20 5242.88
21 10485.76
22 20971.52
23 41943.04
24 83886.08
25 167772.16
26 335544.32
27 671088.64
28 1342177.28
29 2684354.56
30 5368709.12
31 10737418.24
32 21474836.48
33 42949672.96
34 85899345.92
35 171798691.84
36 343597383.68
37 687194767.36
38 1374389534.72
39 2748779069.44
40 5497558138.88
41 10995116277.76
42 21990232555.52
43 43980465111.04
44 87960930222.08
45 175921860444.16
46 351843720888.32
47 703687441776.64
48 1407374883553.28
49 2814749767106.56
50 5629499534213.12
51 11258999068426.24
52 22517998136852.48
53 45035996273704.96
54 90071992547409.92
55 180143985094819.84
56 360287970189639.69
57 720575940379279.38
58 1441151880758558.75
59 2882303761517117.50
60 5764607523034235.00
61 11529215046068470.00
62 23058430092136940.00
63 46116860184273880.00
64 92233720368547760.00
The King paid the beggar a total of one million dollars on day 27
The King paid the beggar a grand sum of $184467440737095488.00!
*/
// C++ code 2
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
int main()
{
int currentDay = 1, numberOfdays;
double amountOwed = 0.0, finalTotal = 0.0;
cout << "An Old Arabian Legend " << endl << endl;
double amountDay1;
cout << "Please enter the amount the King paid on Day 1: ";
cin >> amountDay1;
cout << "Please enter the number of days the King was required to pay the beggar: ";
cin >> numberOfdays;
cout << setw(5) << "Day" << setw(25) << "Amount Owed" << endl;
cout << setw(5) << "---" << setw(25) << "-----------" << endl;
for (currentDay = 1; currentDay <= numberOfdays; currentDay++)
{
amountOwed = pow(2.0, currentDay - 1)*amountDay1;
cout << setw(5) << currentDay << setw(25) << fixed << setprecision(2) << amountOwed;
cout << endl;
}
return 0;
}
/*
output:
An Old Arabian Legend
Please enter the amount the King paid on Day 1: 0.05
Please enter the number of days the King was required to pay the beggar: 5
Day Amount Owed
--- -----------
1 0.05
2 0.10
3 0.20
4 0.40
5 0.80
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.