Loops and Penny Calculation

Jan 31, 2011 at 8:50pm
I have a project due friday.
I have to type a c++ program that calculate how much a person would earn over a period of time if his or her salary is one penny the first day and two pennies the second day, and continues to double each day. i have typed this program but i cannot get it to work. do i need to fix my loop? is the loop supposed to be for or while?

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
//This program will calculate how much a person 
//would earn over a period of time if his or her salary
//is one penny the first day and two pennies the second 
//day, and continues to double each day

//Amehd Gutierrez

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

int main()
{
	int days;				//number of days
	double pennies = 0.01;	//pennies = $0.01
	double total = 0.0;		//Accumalator, initilized with $0.01

	//Get the number of days
	cout << "For how many days will the pay double? ";
	cin >> days;

	cout << endl << "Day             Total Pay \n";
	cout << "-------------------------------- \n";
	
	//Get the double of 0.01 for each day and accumalate the total.
	for ( int count = 1; count <=days; count++)
	{
		
		cout << count << "\t\t$\t" << (pow(2, pennies)) << endl;
	
	}

	//Displays the total 
	cout << setprecision(2) << fixed << showpoint;
	cout << "-------------------------------- \n";
	cout << "Total           $\t" << total << endl;
	
	return 0;
}
Jan 31, 2011 at 9:19pm
At which point in that code are you actually changing the value of total?
Jan 31, 2011 at 9:22pm
see thats the problem. i dont know where to go from there. i want the program to add up. so for example i put 10 days...... day1 .01 day 2 .02 day 3 .04. and then get the total
Jan 31, 2011 at 9:25pm
You could simply work out (or find) a formula to calculate the sum of a geometric progression, but the task was probably meant to make you use loops.

for...;...;...) part is OK. The problem is what you put in it. Now you just print 20.01 many times. What you need to do is to add something to total on every cycle. That something should double after every cycle. That something is pennies.
Jan 31, 2011 at 9:27pm
double? as in * 2 or ^2
Jan 31, 2011 at 9:59pm
I sounds like 2n. It's kind of funny that they only gave 2 terms, though.

Here, find the pattern:
1, 2, etc.

That kind of makes me chuckle.
Feb 1, 2011 at 1:59pm
sure you could keep adding 2n for n := 0 to days-1, but you need to understand the faster way too.

this is a geometric progression : bn = bn-1*2
let's say the task is to fill an array with 1, 2, 4...
the solution clearly (from the formula above) would be
1
2
3
arr[0] = 1;
for(int i = 1; i < array_size; i++)
   arr[i] = arr[i-1]*2;


but you don't need to store them anywhere. you don't need that array. still, new_a = last_a*2. then swap them and calculate another new_a. thanks to how computers work, you can write a = a*2; (or a *= 2;). then you just have add up n of those.
Feb 1, 2011 at 11:22pm
Or, you could do some more math, and find this solution:
1
2
3
4
5
6
7
8
#include "stdio.h"
#include "math.h"
int main(){
	double days;
	printf("Number of days: ");
	scanf("%lf",&days);
	printf("You will be paid $%.2lf",(pow(2,days)-1)/100);
}
Topic archived. No new replies allowed.