Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

We have all heard of the individual being paid one cent on the first day and sub

ID: 3622336 • Letter: W

Question

We have all heard of the individual being paid one cent on the first day and subsequent doubling of the value the following days. Construct an algorithm that calculates the amount earned given the above criteria. Assume the individual accumulates earnings. Write a C++ program that accepts the number of days individual is to receive payment and then output the accumulated total. You will be surprised how money grows. I just cant figure out how to double it to make it work properly. please help if u can thanks

Explanation / Answer

//A recursive solution is simplest:

long double earnings(long double currentEarnings, long double rate, int days)
{
if (days<=0) return currentEarnings;
else return earnings( (currentEarnings + rate), rate * 2, --days);

}

//example call for starting with one penny for 30 days

#include <iostream>

int main()

{

long double e;

e = earnings(0, 0.01, 30);

cout << e;

}

//If you use this function with a very large value for days, it may exceed the limits for long double. If your professor is bitchy about exception handling, you will want to add a check using the numeric_limits library.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote