简体   繁体   English

用一个 function 打印出最大值和平均值

[英]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.将最大值和平均值组合成一个新的 function,它“返回”一组输入数字的最大值和平均值。 Use a single function to this.对此使用单个 function。

#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.调用 function 时出现错误,并且不确定如何解决这个问题,这是我目前所遇到的。

Approach 1: Using by-ref ( & ) parameters correctly:方法 1:正确使用 by-ref ( & ) 参数:

You need to declare max and average before the call-site, and pass them by-reference:您需要在调用站点之前声明maxaverage ,并通过引用传递它们:

double max = 0;
double average = 0;

maxAverage( &max, &average );

The maxAverage function does not need a return value and should be changed to void . maxAverage function 不需要返回值,应更改为void

Approach 2: Using a new struct方法 2:使用新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.函数更简单时更容易推理 - 使用返回值比使用 output 参数或 by-ref 参数更简单。 Consider using a new struct to return those values.考虑使用新struct来返回这些值。

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:具有统一初始化的 C++11 中的语法更简单:

MaxAverageResult maxAverage()
{
    // etc

    return { max, average };
}

Approach 3: Using std::tuple<T1,T2> (aka std::pair<T1,T2> ):方法 3:使用std::tuple<T1,T2> (又名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:这种方法与上述方法相同,但不是声明一个新的struct MaxAverageResult ,而是使用std::pairmake_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 );
}

You can not return two values from a function so returning them as std::pair makes sense.您不能从 function 返回两个值,因此将它们作为std::pair返回是有意义的。

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:调用maxAverage function 需要是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM