
May 17th, 2013, 06:08 PM
#16
Re: A PredatorPrey Simulation
Originally Posted by rockx
I just wish to know if my coding is correct or have i made errors in my coding
Since you've used float variables throughout and if the code compiles there's nothing wrong with your C++ "coding". Any problems must lay with the algorithm you're using.
The algorithm can be deducted from the C++ code of course but how much effort should be put on the people here really? If all burden is on us then you could as well be cut out of the loop. Post the task and let us send in answers directly to your professor? We'll promise to credit you as the major inspirator.
Last edited by nuzzle; May 17th, 2013 at 06:45 PM.

May 17th, 2013, 06:47 PM
#17
Re: A PredatorPrey Simulation
hi nuzzle, i have already submitted my assignments back in April, when it was due. and i have uploaded the code that i had submitted, all i need to do now is that to make sure where i went wrong, i got my final exams coming in a few days time so i m basically trying to prepare for that. i m not asking anyone to do my assignment for me, i just needed a little guidance to perfect myself.
but i really appreciate the help and the guidance given to me.

May 17th, 2013, 08:23 PM
#18
Re: A PredatorPrey Simulation
Originally Posted by rockx
hi nuzzle, i have already submitted my assignments back in April, when it was due. and i have uploaded the code that i had submitted, all i need to do now is that to make sure where i went wrong, i got my final exams coming in a few days time so i m basically trying to prepare for that. i m not asking anyone to do my assignment for me, i just needed a little guidance to perfect myself.
Well, you're bullshitting, plain and simple.
It doesn't work here. At least not with me. Anyway, good luck .
Last edited by nuzzle; May 17th, 2013 at 08:26 PM.

May 19th, 2013, 05:17 AM
#19
Re: A PredatorPrey Simulation
Originally Posted by rockx
This was due in checked in April. I just wish to get to the correct coding so i could know where i went wrong
Did you not get any comments/feedback from your teacher? If not, then check your results with another student or with another implementation, e.g. in a spreadsheet. Find out where any difference originates and then analyze which implementation is correct.
So far, you haven't done more than post your code and ask people to correct it. Programming doesn't work that way; you need to be able to verify your own code and, if not correct, hunt down and fix any errors. Whenever you get stuck, we can help you, but you'll need to show us all the steps you've taken. Otherwise, we cannot help you; we can only do the work for you (which is pointless).
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it.  P. D. Ouspensky

May 19th, 2013, 02:20 PM
#20
Re: A PredatorPrey Simulation
Lets imagine in the reef the 2 fishes reside in has been divided into 7 different sectors, which are populated by Kernighans and Ritchies. This predatorprey relationship determines largely the population size. However, the population is also influenced by migration of fish. Measurements show that every month a certain fraction in a sector will migrate to a neighbouring sector.
So lets now use functions and arrays to calculate the population whilst keeping the migration into consideration.
Suppose that in a given month the number of Kernighan in sector i is pop_k[i], and the population of Ritchies is pop_r[i]. The number of Kernighans in sector i changes as follows:
The population pop_k[i] decreases by alpha_k · pop_k[i]. This is the number of Kernighans that would starve if there were no Ritchies to eat.
• The population pop_k[i] increases by beta_k · pop_k[i] · pop_r[i]. This is the number of new Kernighans because they can feed on Ritchies.
• The population pop_k[i] decreases by gamma_k · pop_k[i]2. This decrease in the number of Kernighans is due to competition between Kernighans.
• The population pop_k[i] increases by omega_k · pop_k[left(i)]. This is the number of Kernighans that migrated from the sector to the left of sector i.
• The population pop_k[i] increases by omega_k · pop_k[right(i)]. This is the number of Kernighans that migrated from the sector to the right of sector i.
• The population pop_k[i] decreases by 2 omega_k · pop_k[i]. This is the number of Kernighans that migrated from the sector i to one of the two neighbouring sectors.
• If the result of this computation is smaller than 0.001, then there are no Kernighan fish left.
The number of Ritchies in sector i changes as follows:
The population pop_r[i] increases by alpha_r · pop_r[i]. This is the number of new Ritchies, if there were no Kernighan to eat them..
• The population pop_r[i] decreases by beta_r · pop_k[i] · pop_r[i]. This is the number of Ritcchies eaten by Kernighans.
• The population pop_r[i] decreases by gamma_r · pop_r[i]2. This decrease in the number of Ritcchies tis due to competition between Ritchies.
• The population pop_r[i] increases by omega_r · pop_r[left(i)]. This is the number of Ritcchies that migrated from the sector to the left of sector i.
• The population pop_r[i] increases by omega_r · pop_r[right(i)]. This is the number of Ritcchies that migrated from the sector to the right of sector i.
• The population pop_r[i] decreases by 2 omega_r · pop_r[i]. This is the number of Ritcchies that migrated from the sector i to one of the two neighbouring sectors.
Measurements have shown that the constants have the following values:
• alpha_k = 0.4;
• beta_k = 0.4
• gamma_k=0.036
• omega_k=0.1
• alpha_r = 0.2;
• beta_r = 0.2
• gamma_r=0.036
• omega_r=0.1
So to implement a simulator that computes the populations for a given number of months for all sectors. This simulator has to take into account the predatorprey model and the influence of migration. It should give the user an option to set the population of Ritchies in a randomly selected sector to 0.
Here is my code
Code:
#include <iostream>
#include <stdlib.h>
#include <cstring>
#include <iomanip>
#include <cstdlib>
using namespace std;
const int N=7;
void tellUser();
int GetMonth();
bool AskZero();
void Header();
int left(int i);
//int right(int i);
//void display_pops(int iteration,double pop1[], double pop2[], int size);
//void mycopy(double original[],double copy[],int size);
//void init_pop(double value,double pop[], int size);
int main()
{
double pop_r[N];
const double ALPHA_K = 0.4;
const double BETA_K = 0.4;
const double GAMMA_K = 0.036;
const double OMEGA_K = 0.1;
const double ALPHA_R = 0.2;
const double BETA_R = 0.2;
const double GAMMA_R = 0.036;
const double OMEGA_R = 0.1;
tellUser();
GetMonth();
bool response = AskZero();
Header();
if (response)
{
int sector = rand() % N ;
pop_r[sector] = 0;
}
//double starve_k, new_k, comp_k, migrate_k, emigrate_kl,emigrate_kr, migrated_k;
//double new_r, eaten_r, comp_r, migrate_r, emigrate_rl,emigrate rr, migrated_r;
/*
starve_k[i] = ALPHA_K * pop_k[i];
new_k[i] = BETA_K * pop_k[i] * pop_r[i];
comp_k[i] = GAMMA_K * pop_k[i] * pop_k[i];
emigrate_kl[i] = OMEGA_K * pop_k [left(i)];
emigrate_kr[i] = OMEGA_K * pop_k [right(i)];
migrated_k[i] = 2 * OMEGA_K * pop_k [i];
new_r[i] = ALPHA_R * pop_r[i];
eaten_r[i] = BETA_R * pop_k[i] * pop_r[i];
comp_r[i] = GAMMA_R * pop_r[i] * pop_r[i];
emigrate_rl[i] = OMEGA_R * pop_r[i] [left(i)];
emigrate_rr[i] = OMEGA_R * pop_r [right(i)];
migrated_r[i] = 2 * OMEGA_R * pop_r [i];
pop_k[i] = pop_k[i]  starve_k[i] + new_k[i]  comp_k[i] + emigrate_kl[i] + emigrate_kr[i]  migrated_k[i];
pop_r[i] = pop_r[i] + new_r[i]  eaten_r[i]  comp_r[i] + emigrate_rl[i] + emigrate_rr[i]  migrated_r[i];
*/
system ("PAUSE");
return 0;
}
void tellUser()
{
cout << "\n\n\n\t\t=====================================\n" << endl;
cout << "\t\t Mongolian Reef Fish Simulator \n" << endl;
cout << "\t\t=====================================\n" << endl;
system ("PAUSE");
system ("cls");
}
int GetMonth()
{
int month=0;
cout << "\n\n\n\t===============================================\n" << endl;
cout << "\t How many months do you want to simulate? \n" << endl;
cout << "\t==============================================\n" << endl;
cin >> month;
system ("cls");
}
bool AskZero()
{
char response;
cout << "\n\nDo want the number of Ritchies in a randomly selected sector to be zero[y/n]\n" << endl;
cin >> response;
if (response == 'N'  response == 'n')
{
return false;
}
if (response == 'Y' response == 'y')
{
return true;
}
}
void Header()
{
cout << "\t      "<<endl;
cout << "\t sector #0  sector #1  sector #2  sector #3  sector #4  sector #5  sector #6 "<<endl;
cout << "\t kern,ritc  kern,ritc  kern,ritc  kern,ritc  kern,ritc  kern,ritc  kern,ritc " <<endl;
cout << "\t      "<<endl;
}
int left(int i)
{
int sector;
}

May 19th, 2013, 02:49 PM
#21
Re: A PredatorPrey Simulation
Apart from the fact that AskZero doesn't always return a value, GetMonth never returns a value, left doesn't do anything and doesn't return a value, you don't set the initial conditions for the different sectors (apart from asking if one should randomly be set to 0) and you have all the calculation code commented, what is your question (mindful that we won't write your code for you)?
Have you actually produced a program design for this simulation? If you haven't, your first task should be to produce the design. If you have a design, what is your problem in coding the program from the design?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only  not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums  and not via private messages!
C++17 Compiler: Microsoft VS2017 (15.6.6)
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
OnDemand Webinars (sponsored)
