i have to create a program that calculates how many weeks it will take to become a millionaire given an allowance of pennies.

for example...
1st week - 1 cent
2nd week - 2 cents
3rd week - 4 cents
4th week - 8 cents
5th week - 16 cents
so forth, so on...

i sort of need help getting started. so far, i only have a basic equation to begin such program..

(2 ^ n - 1)

in which n is the number pennies of that week.

the total goal is 1,000,000 in dollars, which is 100,000,000 pennies.

i am unsure how to set anything up. so far, i only have...

#include <iostream>
#include <iomanip>
using namespace std;

#include <cmath>

int main()
{
   
   total = ((2 * pow(n)) - 1);
       
    
    
    cout << fixed;
    cout << setprecision(2);    
    
    cout << "In " << weeks << " weeks, I'll be a millionaire." << endl;  
    
    cin.ignore();
    cin.get();
    return 0;
}

can anyone leave me some suggestions how to get going with this program?

Member Avatar for iamthwee

Just thinking aloud so most probably wrong.

Can't you just take logs to find the answer?

Just throw it in a loop, create a temporary variable and initialize it to 1, and as long as the value that variable holds is lower as 1 million (or whatever other value you want to take as indicator of being a millionaire, be sure to use the number of pennies equivalent to this value in dollars here!), you multiply the value in the temporary variable by two.
(You can even use the << operator here)

You'll need to declare two variables before the place you put the loop:
one variable for holding the number of weeks it takes to become a millionaire, and one variable used as 'temporary' variable to let the loop decide whether to run another time or not.

When the loop has finished, you print out the variable holding the number of weeks, and you're finished.

EDIT:: Depending on how you implement the loop, you need to print the value of the variable which holds the number of weeks, plus one.

Member Avatar for iamthwee
#include <cmath>
#include <iostream>

using namespace std; 
int main ()
{
   cout << ( log ( 100000000.0 ) / log ( 2.0 ) ) + 1;
   getchar();
}

output

27.5754

?

commented: You should have waited for the OP to make a contribution based on your previous (rather good) reply, and not spoon-feed an answer almost immediately. -7

Logarithms should work as well because you can just derive it to search a power of two.
But, instead of the +1, why not round it?
BTW, iamthwee, you forgot a zero at the end of the number, that makes your output incorrect, it has to be one billion.
(eight zeroes instead of nine)

I would fix it to:

#include <cmath>
#include <iostream>

using namespace std; 
int main ()
{
   cout << ceil( log ( 1000000000.0 ) / log ( 2.0 ) );
   getchar();
}

I guess the output (30) is correct now.
(Solved it by hand using Windows Calculator :P)

commented: Good catch on spotting the billion err +22
Member Avatar for iamthwee

> BTW, iamthwee, you forgot a zero at the end of the number

yeah a typo, I still think you need the +1 which makes the answer
30.89, which rounds to 31?

For example in his example:

4th week - 8 cents cout << ( log ( 8.0 ) / log ( 2.0 ) ) + 1; Without the +1 it doesn't evaluate to 4 weeks. However, having said all that it seems pointless to give a programming assignment which can be evaluated using a scientific calculator.

So ignore the above!

commented: Oops! Yes, the [B]+1[/B] is needed. Sorry, my mistake :D +17

>I still think you need the +1 which makes the answer
30.89, which rounds to 31?

Uh? My output is 30 not 31, when you round it, you don't have to add 1 anymore.

2^31 = 2147483648
[B]2^30 = 1073741824[/B]
2^29 = 536870912

According to this ^^ I would say that 30 is the correct answer.

EDIT:: Yes, you're right that rounding may not be the best way for doing this, adding one and casting to an integer would be better.
It was just a coincidence that ceil worked here :P

thank you guys for all the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.