Integer division issue.

On line 87 I'm having an issue with integer division. I keep getting 1 as the result. I tried using static_cast, but that wasn't able to resolve my issue.

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
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;

const double DEATH_RATE = 0.1;

const double CONVERSION_RATE = 0.00001;

const double GROWTH_RATE = 0.4;

const double WOLF_ABILITY = 0.0005;

const int CAPACITY = 30000;

const int YEARS = 101;

const int TABLE_INCREMENT = 10;

void validate(int[], int[], int, char*[]);
void populate(int[], int[]);
void computeMinimum(int[], int&);
void computeMaximum(int[], int&);
void writeOut(ofstream&, int, int, int, int, int[], int[]);


int main(int argc, char *argv[])
{
    int wolfMax = 0;
    int wolfMin = 0;
    int mooseMax = 0;
    int mooseMin = 0;

    int wolves[101];
    int moose[101];
    moose[0] = 10000;
    wolves[0] = 100;
    ofstream report;
    report.open("report.txt");
    cout << argv[1];

    validate(moose, wolves, argc, argv);
    populate(wolves, moose);
    computeMinimum(wolves, wolfMin);
    computeMaximum(wolves, wolfMax);
    computeMinimum(moose, mooseMin);
    computeMaximum(moose, mooseMax);
    writeOut(report, wolfMin, wolfMax, mooseMin, mooseMax, wolves, moose);

    return 0;
}

void computeMaximum(int data[], int& q)
{
    int comp = data[0];
    for(int k = 1; k < YEARS; k++)
    {
        if(comp < data[k])
        {
            comp = data[k];
            q = k;
        }
    }
}

void computeMinimum(int data[], int& p)
{
    int comp = data[0];
    for(int j = 1; j < YEARS; j++)
    {
        if(comp > data[j])
        {
            comp = data[j];
            p = j;
        }
    }
}

void populate(int wolves[], int moose[])
{


    for(int i = 1; i < YEARS; i++)
    {
        wolves[i] = ((1 - DEATH_RATE + CONVERSION_RATE * moose[i - 1]) * wolves[i -1]);
        moose[i] = ((1 + GROWTH_RATE - (GROWTH_RATE * moose[i - 1])) / (CAPACITY - WOLF_ABILITY * wolves[i - 1]) * moose[i - 1]); // This is where I'm having an issue.
    }

}

void validate(int moose[], int wolves[], int argc, char *argv[])
{
    if(argc == 3)
    {
        wolves[0] = atoi(argv[1]);
        moose[0] = atoi(argv[2]);
    }

}

void writeOut(ofstream& report, int wolfMin, int wolfMax, int mooseMin, int mooseMax, int wolves[], int moose[])
{
    report << "----------" << "Year" << "----------" << "Wolf" << "----------" << "Moose" << endl;
    for(int k = 0; k < YEARS; k += TABLE_INCREMENT)
    {
        report << setw(15) << k << setw(15) << wolves[k] << setw(15) <<moose[k] << endl;
    }
    report << "The minimum population for wolves was " << wolves[wolfMin] << " in year " << wolfMin << endl;
    report << "The maximum population for wolves was " << wolves[wolfMax] << " in year " << wolfMax << endl;
    report << "The minimum population for moose was " << moose[mooseMin] << " in year " << mooseMin << endl;
    report << "The minimum population for wolves was " << moose[mooseMax] << " in year " << mooseMax << endl;
}
How did you use static_cast on line 87? Did you encapsulate the numerator or the whole quotient? To better help you, paste (or even better edit), the code above with the static_cast command.
I believe this is what I tried.

 
((1 + GROWTH_RATE - static_cast<double>(GROWTH_RATE * moose[i - 1])) / (CAPACITY - WOLF_ABILITY * wolves[i - 1]) * moose[i - 1])
I tried to do this:

 
moose[i] = static_cast<int>((1 + GROWTH_RATE - (GROWTH_RATE * moose[i - 1]))) / (CAPACITY - WOLF_ABILITY * wolves[i - 1]) * moose[i - 1]);


EDIT: For some reason unbeknownst to me, I'm not getting any output when I run your program. Brb while I troubleshoot that.
Last edited on
I tried putting static_cast<int> in and I'm only getting zeroes now.
Hello Jack816,

I do not think the problem is where to put static_cast<int>, but in the formula for moose it self.

I changed the function populate to this for testing:
1
2
3
4
5
6
7
8
9
10
void populate(int wolves[], int moose[])
{
	for (int i = 1; i < 5 /*YEARS*/; i++)
	{
		wolves[i] = ((1 - DEATH_RATE + CONVERSION_RATE * moose[i - 1]) * wolves[i - 1]);
		moose[i] = ((1 + GROWTH_RATE - (GROWTH_RATE * moose[i - 1])) / (CAPACITY - WOLF_ABILITY * wolves[i - 1]) * moose[i - 1]); // This is where I'm having an issue.
		cout << "\n formula =  " << ((1 + GROWTH_RATE - (GROWTH_RATE * moose[i - 1])) / (CAPACITY - WOLF_ABILITY * wolves[i - 1]) * moose[i - 1]);
		cout << "\n moose " << i << " = " << moose[i] << endl;
	}
}

I changed the for loop to < 5 just to keep it short. When I run this I notice when i is > 1 the output that I get is zero.

Also to avoid any problems you should initialize the array when you first start
1
2
int wolves[101]{ 0 };  // Will initialize each element to 0.
int moose[101]{ 0 };  // Will initialize each element to 0. 


Still have some thinking to do on the formula on line 87 as to what might work better.

Hope that helps,
Andy
Jack816,

Could you possibly post the mathematical formulae that you are trying to encode in lines 86 and 87?

As a predator-prey population model there are some anomalies. Are you sure that it is bracketed correctly?

A biologically plausible version of lines 86 and 87 might be
1
2
        wolves[i] = wolves[i-1] *(1 - DEATH_RATE + CONVERSION_RATE * moose[i - 1] );
        moose[i] = moose[i-1] * ( 1 + GROWTH_RATE - GROWTH_RATE * moose[i-1]/CAPACITY  - WOLF_ABILITY * wolves[i-1] );

This produces a believable amount of moose (meese?).

(Line 112 has a minor typo in)
Last edited on
Topic archived. No new replies allowed.