FOR loop issues

Hello all, I need some slight assistance on understanding why and how my program is one answer short of the right one I need. I know that may not make much sense so I'll just show you what I am talking about.

I have a program that needs to calculate the amount of green crud that has grown over 88 days, Using the Fibonacci Sequence. I have to write these in WHILE, FOR, and DO-WHILE.

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

int main()
{
    ofstream out("Green Crud WHILE.out");
    int days=88, fi=0, f1=10, f2=0, day=0;

    out << "\tGreen Crud Program (WHILE)\n\n"
        << setw(5)  << " Crud(lbs) "
        << setw(15) << " Number of Days     \n";

    while(day<=days)
    {
        fi = f1 + f2;
        f2=f1;
        f1=fi;
        day+=5;
        out << " " << f1 << "\t\t\t\t"
            << day << endl;
    }

    out.close();
    return 0;
}

OUTPUT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	Green Crud Program (WHILE)

 Crud(lbs)  Number of Days     
 10				5
 20				10
 30				15
 50				20
 80				25
 130			30
 210			35
 340			40
 550			45
 890			50
 1440			55
 2330			60
 3770			65
 6100			70
 9870			75
 15970			80
 25840			85
 41810			90


FOR
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>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{
    ofstream out("Green Crud FOR.out");
    int days=88, fi, f1=10, f2=0;

    out << "\tGreen Crud Program (FOR)\n\n"
        << setw(5)  << " Crud(lbs) "
        << setw(15) << " Number of Days     \n";

     for(int day=0; day<=days; day+=5)
     {
         fi = f1 + f2;
         f2=f1;
         f1=fi;
         out << " " << f1 << "\t\t\t\t"
             << day << endl;
    }

    out.close();
    return 0;
}

OUTPUT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
	Green Crud Program (FOR)

 Crud(lbs)  Number of Days     
 10				0
 20				5
 30				10
 50				15
 80				20
 130				25
 210				30
 340				35
 550				40
 890				45
 1440				50
 2330				55
 3770				60
 6100				65
 9870				70
 15970				75
 25840				80
 41810				85


To reiterate, I need understanding on why, in the FOR loop, the day=0 but why does the for start at 0 and the while doesn't? If it starts at 0 then 10 needs to be repeated for 0 and 5, then 20 for 10.
When does each loop update the 'day'?
Before or after the out << ...?
IN the for loop it updates within the parameters. line 15. and in the while loop its on line 20. I don't understand why I have the same information within each program, and yet I get different outputs.

After some thought I think the output needs to be

1
2
3
4
5
10     0
10     5
20    10
30    15
etc


But I'm unsure how to do this
Last edited on
You just need to move the day+=5; to after the out<<.
A for loop has initalization, condition, increment, and body.
The increment is executed after the body on each iteration.

If you show value before you change it, then you see the original value.
If you show value after you change it, then you see the modified value.
Hello, please help me with my c++ homework here is the task :

"Four digit numbers which first digit is greater than the sum of the third and the fourth digits"
@Aysulu31,
Please write out 1650 times: "Thou shalt not hijack another forum poster's thread ... with homework questions ... which you have made no attempt at".
Last edited on
@Aysulu, you can come out of the naughty box.

BTW a budding programmer would have written a short program to pay the hijack fine.

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
#include <iostream>

int main()
{
    int array[4]{0};
    int number{0};
    int temp{0};
    
    for(int i = 1000; i < 10000; i++)
    {
        number = i;
        std::cout << number << ' ';
        
        for(int j = 3; j >= 0; j--)
        {
            temp = number % 10;
            array[j] = temp;
            number /= 10;
        }
        
        if( array[0] > array[2] + array[3])
            std::cout << "Bingo!";
        
        std::cout << '\n';
    }
    
    return 0;
}
Registered users can post here. Sign in or register to post.