Alex
Alex

Reputation: 231

Trying to return multiple values

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.

Upvotes: 1

Views: 500

Answers (1)

ChrisW
ChrisW

Reputation: 56123

There are three ways to do it.

1) Return a maxv instance from the calculate function

maxv calculate(vector<int>& max)
{
    maxv rc; //return code
    ... some calculations ...
    ... initialize the instance which we are about to return ...
    rc.min_value = something;
    rc.max_value = something else;
    ... return it ...
    return rc;
 }

2) Pass-in a maxv instance by reference

void calculate(vector<int>& max, maxv& rc)
{
    ... some calculations ...
    ... initialize the instance which we were passed as a parameter ...
    rc.min_value = something;
    rc.max_value = something else;
 }

3) Say that calculate is a method of the maxv struct (or even better, the constructor)

struct maxv
{
    int min_value;
    int max_value;
    double mean;
    int median;

    //constructor
    maxv(vector<int>& max)
    {
        ... some calculations ...
        ... initialize self (this instance) ...
        this->min_value = something;
        this->max_value = something else;
     }
};

Upvotes: 8

Related Questions