user12230466
user12230466

Reputation: 27

Printing out max and average with one function

Combine max and average into a single NEW function that “returns” both the maximum and the average of a set of entered numbers. Use a single function to this.

#include<iostream>

using namespace std;

double maxAverage(double& max, double& average);
int main()
{

  maxAverage(max,average);

  return 0;
}

double maxAverage(double& max, double& average)
{
  double val = 0;
  double total = 0;
  double count = 0;


  cout<<"Please enter a value, or -1 when you're done."<<endl;
  cin>>val;
  while(val!=-1){
    total+=val;
    count++;
    cout<<"Please enter a value, or -1 when you're done."<<endl;
    cin>>val;
    if(val>max)
      max = val;
  }
  average = total / count;
  return average;
  return max;
}

I have an error when calling the function and not sure really how to this problem this is what I have so far.

Upvotes: 0

Views: 75

Answers (2)

Dai
Dai

Reputation: 155250

Approach 1: Using by-ref (&) parameters correctly:

You need to declare max and average before the call-site, and pass them by-reference:

double max = 0;
double average = 0;

maxAverage( &max, &average );

The maxAverage function does not need a return value and should be changed to void.

Approach 2: Using a new struct

Functions are easier to reason about when they're simpler - and using return-values are simpler than using output parameters or by-ref parameters. Consider using a new struct to return those values.

struct MaxAverageResult {
    double max;
    double average;
}

int main( int argcx, char* argv* )
{
    MaxAverageResult r = maxAverage();
    cout << r.max << " " << r.average << endl;
    return 0;
}

MaxAverageResult maxAverage()
{
    // etc

    MaxAverageResult r;
    r.max = max;
    r.average = average;
    return r;
}

The syntax is simpler in C++11 with Uniform Initialization:

MaxAverageResult maxAverage()
{
    // etc

    return { max, average };
}

Approach 3: Using std::tuple<T1,T2> (aka std::pair<T1,T2>):

This approach is identical to the above, but instead of declaring a new struct MaxAverageResult you use std::pair and the make_pair function:

int main( int argcx, char* argv* )
{
    std::pair<double,double> r = maxAverage();
    cout << r.first << " " << r.second << endl;
    return 0;
}

MaxAverageResult maxAverage()
{
    // etc

    return std::make_pair( max, average );
}

Upvotes: 4

Mert K&#246;kl&#252;
Mert K&#246;kl&#252;

Reputation: 2231

You can not return two values from a function so returning them as std::pair makes sense.

Full implementation would be:

std::pair<double,double> maxAverage(double& max, double& average)
{
  double val = 0;
  double total = 0;
  double count = 0;


  cout<<"Please enter a value, or -1 when you're done."<<endl;
  cin>>val;
  while(val!=-1){
    total+=val;
    count++;
    cout<<"Please enter a value, or -1 when you're done."<<endl;
    cin>>val;
    if(val>max)
      max = val;
  }
  average = total / count;
  std::pair<double,double> p;
  p.first = average;
  p.second = max;
  return p;
}  

Calling maxAverage function needs to be like this:

std::pair p =  maxAverage(max,average);
double average = p.first;
double max = p.second;

Upvotes: 1

Related Questions