I'm having some trouble returning multiple values in this program that calculates min, max, mean, median. The first thing I did was pass reference arguments, and it worked - but I read that creating a struct or class is the preferred method to returning multiple values.
So I tried and I haven't been able to get good results. Here is what I've got so far.
#include "std_lib_facilities.h"
struct maxv{
int min_value;
int max_value;
double mean;
int median;
};
maxv calculate(vector<int>& max)
{
sort(max.begin(), max.end());
min_value = max[0];
int m = 0;
m = (max.size()-1);
max_value = max[m];
for(int i = 0; i < max.size(); ++i) mean += max[i];
mean = (mean/(max.size()));
int med = 0;
if((max.size())%2 == 0) median = 0;
else
{
med = (max.size())/2;
median = max[med];
}
}
int main()
{
vector<int>numbers;
cout << "Input numbers. Press enter, 0, enter to finish.\n";
int number;
while(number != 0){
cin >> number;
numbers.push_back(number);}
vector<int>::iterator i = (numbers.end()-1);
numbers.erase(i);
maxv result = calculate(numbers);
cout << "MIN: " << result.min_value << endl;
cout << "MAX: " << result.max_value << endl;
cout << "MEAN: " << result.mean << endl;
cout << "MEDIAN: " << result.median << endl;
keep_window_open();
}
Obviously the variables in the calculate function are undeclared. I'm simply not sure how to implement this in the correct way to return the correct values. So far from the things I've tried, I've gotten VERY construed values. Any help would be appreciated - thanks.
P.S. I've looked at other threads regarding this topic and I'm still kind of confused since there aren't really any differences between the arguments that need to be passed to calculate() and the variables in the maxv struct.