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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
// File: island1.cxx
// Written by: Kieran Czerwinski (04/13/12)
// Description:
#include <iomanip>
#include <iostream>
#include <fstream>
using namespace std;
//Named constants:
const double D = 0.1; //death rate of foxes
const double B = 0.00001; //conversion rate
const double R = 0.4; //growth rate of geese
const double A = 0.0005; //ability rate
const double K = 30000.0; //carrying capcity of island
const int YEARS = 500; //number of years to track popuations
//Prototypes:
// The two parameters are the fox and goose populations on the island
// in some year. This function uses these parameters and the declared
// constants to calculate the new fox population in the next year,
// and this value is returned by the function.
int new_fox_pop(int old_fox_pop, int old_goose_pop);
// The two parameters are the fox and goose populations on the island
// in some year. This function uses these parameters and the declared
// constants to calculate the new goose population in the next year,
// and this value is returned by the function.
int new_goose_pop(int old_fox_pop, int old_goose_pop);
int maximum(int n, int data[]); // Returns maximum of the data
int minimum(int n, int data[]); // Returns minimum of the data
double mean(int n, int data[]); // Returns average of the data
int main()
{
int fox;
int goose;
int fdata[YEARS];
int gdata[YEARS];
int fmax;
int gmax;
int fmin;
int gmin;
double fave;
double gave;
cout << "Enter the initial fox population on the island." << endl;
cin >> fox;
cout << "Enter the initial goose population on the island." <<endl;
cin >> goose;
fdata[0] = fox;
gdata[0] = goose;
for(int i = 1; i < YEARS; i++){
if(fox<0){fox=0;}
if(goose<0){goose=0;}
fdata[i] = new_fox_pop(fdata[i-1], gdata[i-1]);
gdata[i] = new_goose_pop(fdata[i-1], gdata[i-1]);
}
fmax = maximum(YEARS, fdata);
gmax = maximum(YEARS, gdata);
fmin = minimum(YEARS, fdata);
gmin = minimum(YEARS, gdata);
fave = mean(YEARS, fdata);
gave = mean(YEARS, gdata);
for(int year = 0; year < 110; year += 10)//displays first 100 years pop
//for every 10 years
{
cout << setw(10) << year
<< setw(10) << fdata[year]
<< setw(10) << gdata[year] << endl;
}
cout << fmax << " " << fmin << " " << fave << endl;
cout << gmax << " " << gmin << " " << gave << endl;
}
int new_fox_pop(int old_fox_pop, int old_goose_pop){
int nfox = int((1 - D + B*old_goose_pop)*old_fox_pop);
return nfox;
}
int new_goose_pop(int old_fox_pop, int old_goose_pop){
int ngoose = int((1 + R - R*old_goose_pop/K - A*old_fox_pop)*old_goose_pop);
return ngoose;
}
int maximum(int n, int data[]){
int max = 0;
for(int i = 0; i<n; i++){
if(data[i]>max){max = data[i];}
}
return max;
}
int minimum(int n, int data[]){
int min = 100000;
for(int i = 0; i<n; i++){
if(data[i]<min){min = data[i];}
}
return min;
}
double mean(int n, int data[]){
double mean;
int total = 0;
for(int i = 0; i<n; i++){
total+=data[i];
}
mean = (total/n);
return mean;
}
|