Using loop to increase numbers.

Well I'm trying to use loop to increase a set of numbers over time.
Example:
Day 1 $0.01
Day 2 $0.03
Day 3 $0.09
Day 4 $0.27
Day 5 $0.81
(Keeps on going until)
Day 15 $47829.69

So far I input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "iostream.h"
#include "iomanip.h"
#include "math.h"

int main ()
{
	// Variable declarations
	double total, mul;

	// Output
	cout << "Mr. Francesa's Donations!" << endl << endl;
	cout << "DAY #                     TOTAL SAVED" << endl;


	for (int counter = 1; counter <= 15; ++counter)
		mul = counter / 100;
		cout << " " << counter << "                     $"<< mul << endl;

	
	return 0;
}




The results will show Day 16 and $0.
Sorry that this is not much but Like I'm stuck and just don't know what to do anymore.
Also this is my first time posting here.

Edit - Input the whole code.
Last edited on
You've haven't explained the algorithm for increasing the numbers.

You also have not shown the definition of mul. If it's an int, you're going to be doing integer division and the result will always be zero.

Not clear if you want to display the total only at the end, or as you iterate through the loop.
If you want to display it on each iteration, your calculation and your cout need to be in {}.

If mul is supposed to be cumulative, then a simple assignment which does not include mul will not accumulate.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
I've included the whole code and fixed it so the code was much easier to read thanks.
Thank you for adding code tags.

You still haven't explained the algorithm for increasing the numbers.

Line 16: You're doing integer division. counter / 100 will always be zero.

Line 12 and the indentation of lines 16-17 impliy you want to print the total for each iteration of the loop. lines 16-17 need to be inside {}. Without the {}, only line 16 executes inside the loop.

Line 16 does not compute a cumulative total. Don't forget to initialize your cumulative amount to 0 before adding to it inside each iteration.


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
#include "iostream.h"
#include "iomanip.h"
#include "math.h"

int main ()
{
	// Variable declarations
	double total, mul;

	// Output
	cout << "Mr. Francesa's Donations!" << endl << endl;
	cout << "DAY #                     TOTAL SAVED" << endl;


	for (int counter = 1; counter <= 15; ++counter)
	{	
		mul = counter * 0.01;
		cout << setiosflags(ios :: fixed | ios :: showpoint);
		cout << setprecision(2);
		cout << " " << counter << "                     $"<< mul << endl;
	}

	
	return 0;
}


I need to add multiplication to the mix.
This what the assignment said -

"Mr. Francesa came up with a way of saving money to donate to his
favorite charity. He wants to start with a penny on the first day and
double the amount he gives for each subsequent day. Write a program to
determine how much he will have donated after 15 days. Use a for loop
to count the number of days and display the total amount saved after each
day."

I'm thinking it's either multiplication or power but I'm having trouble and don't know what to add to make it happen.

The results is going to be-

Day 1 $0.01
Day 2 $0.03
Day 3 $0.09
Day 4 $0.27
Day 5 $0.81
(Keeps on going until)
Day 15 $47829.69

Like do you multiply by 3 or power by 3. This is where I'm stuck at.

I don't agree with your expected results. I added a column to show how much he donates each day. You can see that it doubles each day per the instructions. The third column shows the cumulative donations.
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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main ()
{
	// Variable declarations
	double mul, total;

	// Output
	cout << "Mr. Francesa's Donations!" << endl << endl;
	cout << "DAY #                     TOTAL SAVED" << endl;

    mul = .01;
    total = 0;
	for (int counter = 1; counter <= 15; ++counter)
	{   total += mul;
	    cout << setiosflags(ios :: fixed | ios :: showpoint);
		cout << setprecision(2);
		cout << " " << counter << "                     $"<< mul << " " << total << endl;
		mul = mul * 2;  //  For the next day
	}

    system ("pause");	
	return 0;
}
Mr. Francesa's Donations!

DAY #                     TOTAL SAVED
 1                     $    0.01 $    0.01
 2                     $    0.02 $    0.03
 3                     $    0.04 $    0.07
 4                     $    0.08 $    0.15
 5                     $    0.16 $    0.31
 6                     $    0.32 $    0.63
 7                     $    0.64 $    1.27
 8                     $    1.28 $    2.55
 9                     $    2.56 $    5.11
10                     $    5.12 $   10.23
11                     $   10.24 $   20.47
12                     $   20.48 $   40.95
13                     $   40.96 $   81.91
14                     $   81.92 $  163.83
15                     $  163.84 $  327.67
Press any key to continue . . .
So I got it


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
#include "iostream.h"
#include "iomanip.h"
#include "math.h"

int main ()
{
	// Variable declarations
	double total, mul;

	// Output
	cout << "Mr. Francesa's Donations!" << endl << endl;
	cout << "DAY #                     TOTAL SAVED" << endl;

	mul = .01;
    total = 0;
	for (int counter = 1; counter <= 15; ++counter)
	{   total += mul;
	    cout << setiosflags(ios :: fixed | ios :: showpoint);
		cout << setprecision(2);
		cout << " " << counter << "                     $" << mul << " " << endl;
		mul = mul * 3;
	}


	
	return 0;
}


Mr. Francesa's Donations!

DAY #                     TOTAL SAVED
 1                     $0.01
 2                     $0.03
 3                     $0.09
 4                     $0.27
 5                     $0.81
 6                     $2.43
 7                     $7.29
 8                     $21.87
 9                     $65.61
 10                     $196.83
 11                     $590.49
 12                     $1771.47
 13                     $5314.41
 14                     $15943.23
 15                     $47829.69
Press any key to continue


I used your way and played around with it, but I'm still confused in some parts.

Line 17 : total += mul; \

Line 21 : When I input that before using your method, the output would just stay $0.01 for the output. I put mul = mul * 2; and this will show up.

Mr. Francesa's Donations!

DAY #                     TOTAL SAVED
 1                     $0.01
 2                     $0.01
 3                     $0.01
 4                     $0.01
 5                     $0.01
 6                     $0.01
 7                     $0.01
 8                     $0.01
 9                     $0.01
 10                     $0.01
 11                     $0.01
 12                     $0.01
 13                     $0.01
 14                     $0.01
 15                     $0.01
Press any key to continue


If possible can you explain this.
Last edited on
@xBroken115

I don't know why you insist that a given day's savings is the previous day's savings times three. That's not what the instructions say.
The instructions say that he starts by donating one penny on the first day, and on every subsequent day he doubles the amount he donates.

On day# 1, he donates $0.01. His current, accumulated savings is $0.01.
On day# 2, he donates $0.02. His current, accumulated savings is $0.03 (0.01 + 0.02).
On day# 3, he donates $0.04. His current, accumulated savings is $0.07 (0.03 + 0.04).
On day# 4, he donates $0.08. His current, accumulated savings is $0.15 (0.07 + 0.08).
On day# 5, he donates $0.16. His current, accumulated savings is $0.31 (0.15 + 0.16).
And so on...

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

int main() {

	const int num_days = 15;

	double donation_per_day = 0.01;
	double total_savings = 0.0;

	for (int i = 0; i < num_days; ++i) {

		total_savings += donation_per_day;

		std::cout << "Savings for Day# " << i + 1 << ":\t$" << total_savings << std::endl;

		donation_per_day *= 2;

	}

	return 0;
}
Last edited on
Topic archived. No new replies allowed.