【问题标题】:How many numbers higher than average [C++]比平均数高多少[C++]
【发布时间】:2019-05-13 08:31:55
【问题描述】:

我用 30 个随机数填充了一个数组并计算了平均值。我想显示有多少数字高于平均值。我尝试制作一个函数“aboveAverage”并检查数字是否高于平均值,而不仅仅是增加计数“num_over_average++”。问题是我不知道如何将值“avg”从函数传递到另一个函数。

#include <iostream>
#include <ctime>
using namespace std;

const int n = 30;

void fillArray(int age[], int n) {
    srand(time(NULL));
    for (int index = 0; index < n; index++) {
        age[index] = (rand() % 81) + 8;     
    }
}

void printArray(int age[], int n) {
    for (int index = 0; index < n; index++) {
        cout << age[index] << endl;
    }
}

double printAverage(int age[], int n) {
    double sum;
    double avg = 0.0;
    for (int i = 0; i < n; i++) {
        sum = sum + age[i];
    }
    avg = ((double) sum) / n;
    cout <<  avg << endl;
    return avg;
}

void aboveAverage(int age[], int n) {
    double avg;
    int num_over_average = 0;
    for(int i = 0; i < n; i++){
            if(age[i] > avg) {
                num_over_average++;
            }
        }
    cout<<num_over_average;
}
int main(int argc, char *argv[]) {
    int age[n];

    fillArray(age, n);
    cout << "array: " << endl;
    printArray(age, n);
    cout << endl;

    aboveAverage(age, n);

    //example: Days above average: 16
}

【问题讨论】:

  • 我建议您创建一个新函数来计算平均值。然后你可以从printAverage 和aboveAverage 使用它。
  • printAverage 返回平均值,只需将该值赋予 aboveAverage 添加该参数
  • printAverage 应该返回一个double
  • 您应该将 int printAverage 更改为 double printAverage。函数返回的类型是 double 而不是 int。
  • double avg; -- 在 aboveAverage 函数中未初始化。

标签: c++ arrays numbers average


【解决方案1】:

这应该是一个评论,但我没有足够的代表:(

  • 将aboveAverage 更改为void aboveAverage(int age[], int n, double avg)
  • 从printAverage函数返回avg
  • 将main 代码的最后一部分更改为

    double avg;
    avg = printAverage(age, n);
    aboveAverage(age, n, avg);
    

希望这会有所帮助!

【讨论】:

  • 不要害怕,这是一个答案,而不是评论。你也比我早了 5 秒,所以我删除了我的和你的 UV ;-) 可能建议 OP 在cout&lt;&lt;num_over_average; 中也输出 endl
  • 名字应该改一下,double avg = getAverage(age, n); std::cout &lt;&lt; avg &lt;&lt; std::endl; auto count = aboveNumbers(age, n, avg); //....
【解决方案2】:

您有两种使用您的代码的解决方案:

您可以调用printAverage() 在aboveAverage() 中初始化avg:

void aboveAverage(int age[], int n) {
    double avg = printAverage();
    ...
}

或者你在用printAverage() 计算后通过aboveAverage() 参数的平均值:

void aboveAverage(int age[], int n, double avg) {
    ...
}

【讨论】:

    【解决方案3】:

    如果你使用标准库,你可以用两行代码做到这一点:

    double average = std::accumulate(std::begin(age), std::end(age), 0.0) / std::size(age);
    int above_average = std::count_if(std::begin(age), std::end(age),
        [average](double value) { return average < value; });
    

    好的,你可以把它算作三行。

    与问题中的代码相比,这种方法的一个主要优点是您可以将容器类型更改为,例如,vector&lt;double&gt;,而无需更改任何此代码。

    【讨论】:

      【解决方案4】:

      嗯,很简单,但取决于你的情况,我会详细说明。 当它是更大功能的一部分时,我就是这种情况(do-somthing()) 您可以像这样计算平均值并将其传递给您的“aboveAverage”函数并打印它:

      double n_average = printAverage(nArr_ages, n_agesArraySize);
      
      aboveAverage(nArr_ages,  n_agesArraySize, n_averag);
      

      我自己可能会将 printAverage 函数重写为两个函数,一个基于数组返回平均值,另一个不同时打印它,因为它违反了单一职责的 SOLID 原则,并且函数名称应该反映正是它的作用,在这种情况下,可能是 calculateAverage 或 getAverageAge 或任何其他适当的名称(尝试将您的函数命名为英语,这样您的代码就会像歌曲一样被阅读。

      例如:

      const size_t n = 30;
      
      double calculateAverage(int nArr_ages[], int n_agesArraySize) {
          double sum = 0.0;
          double avg = 0.0;
          for (int indexInArray = 0; indexInArray < n_agesArraySize; indexInArray++) {
              sum = sum + age[indexInArray];
          }
          average = ((double) sum) / n_agesArraySize;
          return average;
      }
      
      int aboveAverageCells(int ages[], int n_agesArraySize ) {
          double average = calculateAverage(ages, n);
          int num_over_average = 0;
          for(int indexInArray = 0;  indexInArray < n_agesArraySize; indexInArray++) {
                  if(ages[indexInArray] > avg) {
                      num_over_average++;
                  }
          }
          return num_over_average;
      }
      

      现在只需按顺序调用它们,将返回值保存到 main 函数中的局部变量中,并在 main 中本地使用 cout 打印。

      作为旁注,下次可能为 const 和数组大小的局部函数变量选择不同的名称。

      【讨论】:

      • 请永远不要提议在这种情况下使用全局变量(我不 DV 即使值得 DV ;-))
      • 这是两个不好的建议,没有一个好。使用引用传递引用,我不明白为什么要在这里使用全局变量,如果你计算两个数据集的平均值怎么办?为什么要通过引用传递全局变量?
      • 请注意,编写一个将指针指向变量的函数不是通过引用传递。编写一个接受 reference 的函数是。 void myfunction(int&amp; counter) 引用 counter。
      猜你喜欢
      • 2010-12-03
      • 1970-01-01
      • 2017-12-08
      • 1970-01-01
      • 2018-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-12
      相关资源
      最近更新 更多