Unexpected Output with While loop

I am in a C++ class and when compiling a solution to an assignment, the expected outcome is 814 wolves, however, I only get 806. The code is a simulation between a predator-prey relation for wolves and rabbits. Below I attached the code, more details will be provided upon commenting on this post.
//C++
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
int RabitCalc();
int WolfCalc();
int main() {

int WolfNumber;
int RabitNumber = 10000;
int RabitNumberTom = 0;
int WolfNumberTom = 0;
int days = 0;
cout << " This simulation starts with 10,000 rabbits... ";
cout << "\n";
ifstream myfile;
myfile.open("input.txt");
//myfile >> numofsimulations >> wolfnumber1 >> wolfnumber2 >> wowlfnumber3
do {
cout << " How many wolves do you want to have? ";
cin >> WolfNumber;
}

while (WolfNumber <= 50);

cout << "Day | Rabbits | Wolves" << endl;
while (days < 1001 & RabitNumber < 250001 & WolfNumber > 49) {
RabitNumberTom = (1 + 0.01)*RabitNumber - 0.00001*RabitNumber*WolfNumber;

WolfNumberTom = (1 - 0.005)*WolfNumber+ 0.00001* 0.01 *RabitNumber*WolfNumber;
WolfNumber = WolfNumberTom;
RabitNumber = RabitNumberTom;

if (days % 50 == 0) {
cout << days << " | " << RabitNumberTom << " | " << WolfNumberTom << endl;
}

days = days++;


}
if (days >= 1000) {
cout << " Congratulations! You have successfully ran this simulation. You ended with " << RabitNumberTom << " rabbits and " << WolfNumber << " wolves. ";
}
else if (WolfNumberTom < 50) {
cout << " Unfortunately, there are only " << WolfNumberTom << " wolves, which is less than or equal to 50. ";

}
else if (RabitNumberTom >= 250000) {
cout << " Unfortunately, there are " << RabitNumberTom << " rabbits, which is greater than or equal to 250,000 ";

}
system("pause");
return 0;
}

//C++
Thanks!
You need to say how many wolves you start with.
Is the number of rabbits you end up with correct?
Can you show (or link to) the original equations you are basing this on?

BTW, the logical (or "boolean") AND is && (not &, which is the "bitwise" AND).

And rabbit has two b's.

Also, you should use code tags to preserve your code's indentation.

[code]
put your code between the "tags" like this
[/code]
Last edited on
tpb,


Thanks for your quick response, and for the tip about code tags. This is my first post, so bear with me. I start with 1000 wolves and the number of rabbits is also incorrect. Sorry for not including that in the first place. Thanks
I get the same result as you (806 for wolves, 3901 rabbits).
Here's your code fixed up a little, but no real change. I hardcoded the 1000 wolves and commented out the input for testing.

806 is pretty close to 814 so it's hard to say what's wrong. I thought it might have something to do with rounding and tried a couple of things, but nothing worked.

If you can show the original equations that you're basing this on, it might help. And are you absolutely sure the answer should be 814? And how many rabbits?

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main() {
    int WolfNumber  =  1000;
    int RabitNumber = 10000;
    int RabitNumberTom  = 0;
    int WolfNumberTom   = 0;
    int days = 0;

    cout << " This simulation starts with 10,000 rabbits...\n";

//    ifstream myfile("input.txt");
//    myfile >> numofsimulations >> wolfnumber1
//           >> wolfnumber2 >> wowlfnumber3
/*
    do {
        cout << " How many wolves do you want to have? ";
        cin >> WolfNumber;
    } while (WolfNumber <= 50);
*/
    cout << "Day  | Rabbits | Wolves\n";

    while (days < 1001 && RabitNumber < 250001 && WolfNumber > 49) {

        RabitNumberTom = 1.01 * RabitNumber
                       - 0.00001 * RabitNumber * WolfNumber;
        WolfNumberTom = 0.995 * WolfNumber
                      + 0.0000001 * RabitNumber * WolfNumber;

        WolfNumber = WolfNumberTom;
        RabitNumber = RabitNumberTom;

        if (days % 50 == 0) {
            cout << setw(4) << days << " | "
                 << setw(7) << RabitNumber << " | "
                 << setw(6) << WolfNumber << '\n';
        }

        days++;
    }

    if (days >= 1000) {
        cout << "Congratulations! You have successfully ran this simulation.\n"
             << "You ended with " << RabitNumber
             << " rabbits and " << WolfNumber << " wolves.\n";
    }
    else if (WolfNumber < 50) {
        cout << " Unfortunately, there are only " << WolfNumber
             << " wolves, which is less than or equal to 50.\n";
    }
    else {
        cout << " Unfortunately, there are " << RabitNumber
             << " rabbits, which is greater than or equal to 250,000\n";
    }

    return 0;
}

You are out by (about) 2 days.

God spent the first day creating rabbits and wolves (so initialise days=1).
Move the days++ to the start of the loop.

God spent the next six days debugging his program.
Also logical bug. Should be days++; and not days = days++;

1
2
3
4
5
6
7
8
9
10
#include <iostream>

int main()
{
    int days = 0;
    days = days++;
    std::cout << days << '\n';

    return 0;
}

0


Why? iirc this is actually unspecified behavior and dangerous to attempt assigning a post-increment var to itself.
Variable 'days' is assigned to value of 'days', and only afterwards the right-hand side (a temporary copy of days?), increments.

Perhaps most implementations set a rule that if left side contains same variable as right-side, in the case of post-increment the assignment occurs (and increment ignored?). I'm not entirely sure.
Last edited on
Also, why is the expected outcome 814?
Can you put the growth/death rate into words so that we can confirm that your code reflects this?

Btw, to make parallel assignment, you can use std::tie from the <tuple> header:
1
2
3
std::tie(rabbits,wolves) = 
            std::make_tuple(1.01*rabbits - 0.00001*rabbits*wolves,
                            0.995*wolves + 0.0000001*rabbits*wolves);
I've changed while loop to do-while loop and start with days = 1.
I get 814, see code below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    days = 1;
    cout << "Day  | Rabbits | Wolves\n";
    {
            cout << setw(4) << days << " | "
                 << setw(7) << RabitNumber << " | "
                 << setw(6) << WolfNumber << '\n';
    }
    do {	    
        RabitNumberTom = (1 + 0.01) * RabitNumber
                       - 0.00001 * RabitNumber * WolfNumber;
        WolfNumberTom = (1 - 0.005) * WolfNumber
                      + 0.00001 * 0.01 * RabitNumber * WolfNumber;

        WolfNumber = WolfNumberTom;
        RabitNumber = RabitNumberTom;
		days++;
		
        if (days % 50 == 0) {
            cout << setw(4) << days << " | "
                 << setw(7) << RabitNumber << " | "
                 << setw(6) << WolfNumber << '\n';
        }

    } while (days < 1000 && RabitNumber < 250001 && WolfNumber > 49);


Result:

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
 This simulation starts with 10,000 rabbits...
Day  | Rabbits | Wolves
   1 |   10000 |   1000
  50 |   10459 |    804
 100 |   11996 |    643
 150 |   14818 |    511
 200 |   19347 |    411
 250 |   26483 |    336
 300 |   37292 |    286
 350 |   53827 |    245
 400 |   78370 |    245
 450 |  113629 |    275
 500 |  159511 |    386
 550 |  202912 |    709
 600 |  197705 |   1509
 650 |  116046 |   2590
 700 |   45790 |   2945
 750 |   18226 |   2637
 800 |    8946 |   2166
 850 |    5537 |   1727
 900 |    4205 |   1354
 950 |    3780 |   1052
1000 |    3887 |    814
Congratulations! You have successfully ran this simulation.
You ended with 3887 rabbits and 814 wolves.
@homy18 -- your output is technically off by a day. You're incrementing the days and then checking against modulo 50. The last line, for example, is actually the count after day 999, with 3887 rabbits and 814 wolves. Your loop goes from 1 to 999.
Topic archived. No new replies allowed.