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;
}
}
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 ";
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]
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?
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.
@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.