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?
//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>
usingnamespace 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;
}
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
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.
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.
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);
}