Home > Net >  C PPP exercise about returning multiple values
C PPP exercise about returning multiple values

Time:10-26

Write a function that finds the smallest and the largest element of a vector argument and also computes the mean and the median. Do not use global variables. Either return a struct containing the results or pass them back through reference arguments. Which of the two ways of returning several result values do you prefer and why?

So this is an exercise from a book I'm reading right now. What I did was just made a class and returned an object containing all the data but what exactly do they mean be passing the results back through reference arguments? I mean I could think of something like this:

void calculate(vector<double> vec, double &min, double&max, double &median, double &mean)
{
// here I kinda just do everything so find the min, max, etc and because all those are references
// their value automatically gets changed


}

then in main I could have something like:

int main()
{
double min, max, median, mean;
vector<int> v= {values};
calculate(v, min, max , median, mean);

}

Is that what they mean?

CodePudding user response:

pass them back through reference arguments

void calculate(vector<double> vec, double &min, double&max, double &median, double &mean)

Is that what they mean?

Yes. However, we don't pass return values through reference parameters in C .

First of all, since C 17 we have NRVO, so direct return doesn't actually copy/move the return value but instead initializes it in-place. There's no benefit in emulating this through reference parameters.

Secondly, this technique requires default-constructing your object before passing a reference to it. However, it might do something costly for default construction (even though it usually doesn't), so you even lose performance because of such "optimizations".

Finally, the type of the object might not allow default-construction: in C , you usually either put an object in a valid state or throw an exception so you lexically can't use an invalid object. Then, if successful construction requires parameters, you cannot default-construct. Example.

So, your real code would be something like

struct Results { double min, max, median, mean; };
Results calculate(vector<double> const& vec); // you probably don't need to copy the vector

Actually we don't hardcode parameters and even change vector<double> const& vec to auto&& range, but that's another topic.

CodePudding user response:

That's not exactly what they want. Let's see.

"Write a function that finds the smallest and the largest element of a vector argument and also computes the mean and the median" This one right here its very clear. This is the computation you need to do.

"Do not use global variables. Either return a struct containing the results or pass them back through reference arguments."

There are one famous way to return multiples values from a function. You may think, "Ok lets return a vector with multiples values" but that's not what you have to do, and also you can't to that in c with vectors, you can do it with unions and enumerations. The way you can do this is passing by reference.

Let me explain with a example. if you have this function: This is a function to calculate the sum and the product of to variables. As you can see this functions "Return" to values. These values are passing by reference.

void calculate_something(int n,int m, int &sum, int  &product){
    sum = n m; 
    product = n*m; 
}

int main(){
    int sum = 0; 
    int product = 1; 
    calculate_something(5,8,sum,product)
    cout<< sum <<endl; 
    cout<<product<<endl; 
}

  •  Tags:  
  • c
  • Related