how many weeks to become a millionaire?

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...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#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?
It involves base 2 logarithms.
Last edited on
Here is an algorithm:


initialize total_amount_earned to 0.
initialize week_number to 1.
while the total_amount_earned is less than $1,000,000 do:
add to total_amount_earned the amount earned in week "week_number".
increment week_number.


Now when the loop terminates, what will the value of total_amount_earned and week_number be?
For example, total_amount_earned = 1, but week_number = 2, right? (because we increment
week_number). So you might need to output an "adjusted" week_number.

[EDIT: This is just one way to do it. There are other ways as well that don't require loops.]
Last edited on
Hehe can't we just do:

std::cout<< "Number of weeks:"<<10;

Sure it would give you a bad grade, but also the satisfaction you annoyed your teacher, who gave you such a lovely problem... ;)
Last edited on
10 weeks to become a millionaire, awesome. Though you could figure out whatever binary number(consisting of 1 followed by all zeroes) is closest to 100mil, and then the number of bits in that number is your answer.

note this post was edited.
Last edited on
Based on the allowance that you posted..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <winnt.h>
#include <sal.h>
#include <stdio.h>

#define PENNIES_NEEDED 100000000

int __cdecl main(__in int argc, __in_ecount_z(argc) char* argv[], __in_z char* envp[])
{
	UNREFERENCED_PARAMETER(argc);
	UNREFERENCED_PARAMETER(argv);
	UNREFERENCED_PARAMETER(envp);

	UINT	i = 0, j = 1;

	for(; j < PENNIES_NEEDED; i++)
		j *= 2;

	printf("it will take %i weeks for you to become a millionaire", i);
	getchar();
	return EXIT_SUCCESS;
}


:) You can use cout instead of printf, I'm just more of a C person.
Christ.
Please tell me how you learnt C so I can discourage people from taking that path, Kiana.
finally got it to work. thanks for all the suggestions. i did change my equation to having 2 logs within it. im finally getting the output i wanted.

THANKS a million for taking the time to help me out.
Topic archived. No new replies allowed.