Passing an array to a function to calculate the min, max, and mean.

So, I'm trying to model the populations of fox and geese on a island. The populations of both the fox and geese are stored in two separate arrays. I have to write functions to calculate the min, max, and mean of the data stored in these arrays. I'm pretty stuck on how to do this, I've given it my best attempt, but I can't figure it out. It would be easier if I could change the prototypes of the min, max, and mean functions, but the assignment requires those exact prototypes.

It would be really great if someone could help me out, even if it's just a hint in the right direction.

Here's what I have so far:
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
#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;
    
    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(i, fdata);
	gmax = maximum(i, gdata);
	fmin = minimum(i, fdata);
	gmin = minimum(i, 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 << " " << gmax << endl;
    cout << fmin << " " << fmin << 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 = data[0];

	if(data[n]>max){max = data[n];}
        return max;
}
    
int minimum(int n, int data[]){
   int min = data[0];

	if(data[n]<min){min = data[n];}
        return min;
}
    
double mean(int n, int data[]){

}
To get the min and max element you can use a loop to loop through all the elements in the array and see what is the greatest or lowest value.

Mean is just another word for the average value of the array elements. You just have to sum all the elements in the array and then you divide the sum by n to get the mean. Note that the return type of mean should be a double so make sure that at least one of the operands is a double when you do the division or otherwise you will not get the decimals.
At the moment, you functions will overwrite the maximum value.

Consider this:

Say, data[0]=0, data[1] = 5 and data[2] = 3.

Your function looks at compares data[i] with data[0] and returns the highest value.

So, when i = 0, it'll return 0.
When i=1, it'll return 5.
When i=2, it'll return 3.

Your third iteration there (i=2) has overwritten the previous maximum value of 5.

You could pass YEARS in as the first parameter and loop through the array inside of the function to work out the maximum, minimum and mean values. Then just perform this function after your loop in the main function.

Thanks! This helped a lot, however I'm not getting correct values, but they are very close. Any ideas?
My values:
fox max = 622
fox min = 100
fox ave = 520
goose max = 23404
goose min = 8338
goose ave = 10449

Actual values:
fox max = 642
fox min = 93
fox ave = 442
goose max = 11112
goose min = 8442
goose ave = 10019

This is my new code:
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;
}

Last edited on
What do you mean by 'actual' values?

Have you got them by querying elements in the array? Or are they the solution that you should be getting? (For example, do you have an answer sheet and your answers don't match up?)

Those are the values I should be getting according to the homework assignment. They were given so that we could check to make sure it was working properly.
Here is the assignment page:
http://1300.dupland.com/node/69
I should have thought to post this earlier..
In each case, the data is an array that contains exactly n integers. Your output should be formatted similar to this example (though your numbers will be different)

- From the min/max/ave section of your assignment brief
Last edited on
smacks self in face*

I should read instructions better. Thanks for all your help! I deeply appreciate it! You are an amazing human being.
Haha, no worries. :-)
Topic archived. No new replies allowed.