Do While Loop unknown behavior

Hi guys, I have two questions:
I'm trying to run this code in Codeblocks, and whenever I try to modify the length of the initial array to 100, it stops after three loops, and when I modify it to 50 it stops after 13 loops.
Is my compiler broken, how should I handle it?

Alsoinitial[i] += ((initial[i - 1]* 0.05) + initial[i - 1]);
shouldn't it equal to 0, on the first loop, why is it evaluated to 100?

Thanks in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main()
{
     float original = 100;

    double interestDaphne ;
    double initial[100];
    int i = 2;
                    initial[3] = 100;
                    interestDaphne = 100;
    do
    {
            i++;
            interestDaphne += (0.10 * original);
            initial[2] = 0;
            initial[i] += ((initial[i - 1]* 0.05) + initial[i - 1]);
            cout << initial[i] << "   " << interestDaphne<<  "   "  << endl;

    }
    while(initial[i] < interestDaphne);
}
Also initial[i] += ((initial[i - 1]* 0.05) + initial[i - 1]);
shouldn't it equal to 0, on the first loop, why is it evaluated to 100?

100 + 0 = 100
As written (with +=) you are, in line 18, ....
taking the current value of initial[3] (which is 100) and adding to it the RHS (which is 0).
(This is basically what @Peter87 is saying.)

If you wanted it to be 0 you would use = not +=. Your code is equivalent at present to
initial[i] = initial[i] + ((initial[i - 1]* 0.05) + initial[i - 1]);

Try walking through each line of code and seeing how variables change.
Topic archived. No new replies allowed.