Does anyone know how to print this

This is the output:

Target money: $20
Day 0 you save $0 so you have $0
Day 1 you save $1 so you have $1
Day 2 you save $2 so you have $3
Day 3 you save $3 so you have $6
Day 4 you save $4 so you have $10
Day 5 you save $5 so you have $15
Day 6 you save $6 so you have $21



The amount of money you save is the same as the day number. When I enter the target money, I need the program lists out the saving progress from day 0 until reaching the saving target.

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
#include <iostream>
using namespace std;

int main()
{
   
    int tm;

    cout << "Target money: ";
    cin >> tm;

     

        // first loop
        for (int i = 0; i >=0 ; i++) 
        {
            // second loop
            for (int j = i; j >=0; j++)
            {
                int k;
                for (k = j+i ; k >= j + i; k++)

                if (k >= tm)
                {
                    break;
                }
                cout << "Day " << i << " you save $" << j << " so you have $" << k << endl;
            }
        }

        return 0;

}


I tried this but I think I do it so bad, so I didn't post it.
Last edited on
Hello ayanokoji,

Does anyone know how to print this? Yes, so what have you done?

keskiverto once wrote:

Please note that this is not a homework site. We won't do your homework for you. The purpose of homework is that you learn by doing. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.

We didn't see your attempts to solve this problem yourself and so we cannot correct mistakes you didn't make and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again.



Andy
Hello ayanokoji,

Even if it is bad or does not work if you do not post the code no one can tell you what is wrong.

Line 15: This is an endless for loop because "i" will always be < "i + 1".

In all the for loops be very careful about using "<=". This tends to do 1 more loop than you expect.

Just so you know line 25 only breaks out of the inner most for loop and not the other 2.

If I understand line 23 this is when you equal or exceed the target money and need to leave all the for loops to finish up.

As soon as I get this loaded up and tested I will let you know if I find anything else.

Andy
Hello ayanokoji,

Line 15 is still an endless for loop. Think about what you have and you will see that "i" will always be ">=" to (0) zero. Also it does not work because you never get back to it to change the value of "i" when it needs to be changed because the 2nd for loop is also an endless loop.

The 3rd for loop does not make any sense. It seems to be over kill for what should just be an if statement.

I just ran the code for 3 lines, but this was my output:

Target money: 20
Day 0 you save $0 so you have $20
Day 0 you save $1 so you have $20
Day 0 you save $2 so you have $20


This shows me that the "Day" never changes and the amount of money you have should start at (0) zero not 20.

I have not worked on it yet, but I am thinking that 1 for loop should do the trick.

Andy
my lastest version:
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
#include <iostream>
using namespace std;

int main()
{
   
    int tm;

    cout << "Target money: ";
    cin >> tm;

     

      
        for (int i = 0; i < i+1 ; i++) 
        {
           
            for (int k = 0; k >= k + i; k++)
            {

                if (k >= tm)
                {
                    break;
                }
                cout << "Day " << i+k << " you save $" << i+k << " so you have $" << k+i << endl;
            }
        }

        return 0;

}


output
Target money: 20
Day 0 you save $0 so you have $0
Day 1 you save $1 so you have $1
Day 2 you save $2 so you have $2
Day 3 you save $3 so you have $3
Day 4 you save $4 so you have $4
Day 5 you save $5 so you have $5
Day 6 you save $6 so you have $6
Day 7 you save $7 so you have $7
Day 8 you save $8 so you have $8
Day 9 you save $9 so you have $9
Day 10 you save $10 so you have $10
Day 11 you save $11 so you have $11
Day 12 you save $12 so you have $12
Day 13 you save $13 so you have $13
Day 14 you save $14 so you have $14
Day 15 you save $15 so you have $15
Day 16 you save $16 so you have $16
Day 17 you save $17 so you have $17
Day 18 you save $18 so you have $18
Day 19 you save $19 so you have $19
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Example program
#include <iostream>

int main()
{
    int target = 20;
    std::cout << "Target money: $" << target << '\n';

    for (int n = 0; ; n++)
    {
        int total = (n*(n+1)/2);
        std::cout << "Day " << n << " you save $" << n << " so you have $" << total << '\n';
        if (total >= target)
        {
            break;   
        }
    }
}

Target money: $20
Day 0 you save $0 so you have $0
Day 1 you save $1 so you have $1
Day 2 you save $2 so you have $3
Day 3 you save $3 so you have $6
Day 4 you save $4 so you have $10
Day 5 you save $5 so you have $15
Day 6 you save $6 so you have $21
Last edited on
Thank Ganado,
why total = (n*(n+1)/2)
Last edited on
That's just a shortcut for the sequence 0, 1, 3, 6, 10, 15, 21, ...
0
0 + 1
0 + 1 + 2
0 + 1 + 2 + 3 ...

Triangular numbers
https://en.wikipedia.org/wiki/Triangular_number
http://oeis.org/A000217

You could do total = total + n; with total initialized outside the loop for the equivalent behavior.
Last edited on
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
   double target;
   cout << "What is your target? ";   cin >> target;
   cout << "You require " << ceil( sqrt( 2 * target + 0.25 ) - 0.5 ) << " days\n";
}


What is your target? 20
You require 6 days
@Ganado, thx a lot I get it now.

@Handy Andy, thx for your all suggestion.

@lastchance, it is awesome, I will try it.
Heh, lastchance, I was just about to do the same thing, taking the inverse to calculate the # of days since the loop is wholly unnecessary.
Hello ayanokoji,

Seeing all these nice examples makes me wish I stayed with math more than I did.

Anyhow here is something different.
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
#include <iostream>

using namespace std;  // <--- Best not to use.

int main()
{
    constexpr int TOTALDAYS{ 10 };

    int targetMoney{}, totalSaved{};

    //cout << "Target money: ";
    cout << "Enter your savings target: ";
    cin >> targetMoney;

    for (int day = 0; day < TOTALDAYS; day++)
    {
        totalSaved += day;

        cout << "Day " << day << " you save $" << day << " so you have $" << totalSaved << '\n';

        if (totalSaved >= targetMoney)
        {
            std::cout << "\n In " << day << (day > 1 ? " days" : " day") << " you have reached your target.\n";

            break;
        }
    }

    return 0;  // <--- Not required, but makes a good break point.
}


Andy
Without using the maths:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main()
{
	int targetMoney {}, days {};

	cout << "Enter your savings target: ";
	cin >> targetMoney;

	for (int totalSaved {}; totalSaved < targetMoney + days; totalSaved += ++days)
		cout << "Day " << days << " you save $" << days << " so you have $" << totalSaved << '\n';

	std::cout << "\nIn " << days - 1 << " days you have reached your target.\n";
}



Enter your savings target: 100
Day 0 you save $0 so you have $0
Day 1 you save $1 so you have $1
Day 2 you save $2 so you have $3
Day 3 you save $3 so you have $6
Day 4 you save $4 so you have $10
Day 5 you save $5 so you have $15
Day 6 you save $6 so you have $21
Day 7 you save $7 so you have $28
Day 8 you save $8 so you have $36
Day 9 you save $9 so you have $45
Day 10 you save $10 so you have $55
Day 11 you save $11 so you have $66
Day 12 you save $12 so you have $78
Day 13 you save $13 so you have $91
Day 14 you save $14 so you have $105

In 14 days you have reached your target.

Topic archived. No new replies allowed.