繁体   English   中英

计算数组元素的平均值并显示它们

[英]calculate average of array elements and display them

问题是:40名学生的班级已经通过5次考试获得了成绩。 实现一个计算平均成绩最差的功能并显示所有平均成绩最差的学生的ID。

我已经计算出平均值,但不知道如何计算最差平均值(如40名学生中最低的平均值)并显示具有该编号的ID号。

到目前为止,这是我写的:

#include<iostream>
#include <iomanip>
using namespace std;
const int MAX_NUM = 6;
int x[MAX_NUM];
int y[5];

int main()
{


float avg;
float total = 0;

for (int i = 0; i < MAX_NUM; i++)
{
    cout << "Enter an ID number: " << endl;
    cin >> x[i];
    cout << "Enter 5 grades: " << endl;
    for (int j = 0; j < 5; j++)
    {
        cin >> y[j];
        while (y[j]>100)
        {
            cout << "Please enter a valid grade that is less than a 100: " << endl;
            cin >> y[j];
        }
        total += y[j];
    }
        avg = total / 5;

    cout << "ID: " << x[i] << endl;
    cout << "Average: "<< avg << endl;
}

因此,您要将这些平均值存储在std::vector<float>std::sort并获得最低的平均值。 然后返回并找到具有该平均值的学生。

像这样:

注意:我添加了一些重要的声明!

#include<iostream>
#include <iomanip>
using namespace std;
const int MAX_NUM = 6;
int x[MAX_NUM];
int y[5];
float AVG[MAX_NUM];
int worstIDCount = 0;

int main()
{


  float avg, min = 1001;
  float total = 0;

  for (int i = 0; i < MAX_NUM; i++)
    {
      avg = 0;
      total = 0;
      cout << "Enter an ID number: " << endl;
      cin >> x[i];
      cout << "Enter 5 grades: " << endl;
      for (int j = 0; j < 5; j++)
    {
      cin >> y[j];
      while (y[j]>100)
        {
          cout << "Please enter a valid grade that is less than a 100: " << endl;
          cin >> y[j];
        }
      total += y[j];
    }
      avg = total / 5;
      AVG[i] = avg;
      if(avg < min)
    min = avg;

      cout << "ID: " << x[i] << endl;
      cout << "Average: "<< avg << endl;
    }
  for(int i = 0; i < MAX_NUM; i++)
    {
      if(AVG[i] == min)
    cout << "Student with WORST Average: ID" << x[i] << endl;
    }
};

工作实例

#include <iostream>
#include <vector>

#include <functional> // mem_fn
#include <algorithm>  // sort, upper_bound
#include <iterator>   // ostream_iterator

struct Student_average {
  int student_id;
  float average;
};

bool compare_student_averages(Student_average const &lhs,
                              Student_average const &rhs) {
  return lhs.average < rhs.average;
}

int main() {
  std::vector<Student_average> averages;

  // collect the data and populate the averages vector
  // ...

  sort(begin(averages), end(averages), compare_student_averages);

  std::cout << "The worst average is: " << averages.front().average << '\n';

  auto end_of_worst_student_averages =
      upper_bound(begin(averages), end(averages), averages.front(),
                  compare_student_averages);

  std::cout << "The IDs of the students with the worst averages are:\n";

  transform(begin(averages), end_of_worst_student_averages,
            std::ostream_iterator<int>(std::cout, "\n"),
            std::mem_fn(&Student_average::student_id));
}

这是使用std::accumulatestd::min_element一种更加C ++的方式(为简便起见,我删除了所有大于100的检查):

#include <iostream>
#include <algorithm>
#include <numeric>

using namespace std;

const int MAX_NUM = 6;
int x[MAX_NUM];
int y[5];

int main()
{
    float avg[5];
    float total = 0;

    for (int i = 0; i < MAX_NUM; i++)
    {
        cin >> x[i];  // ID
        for (int j = 0; j < 5; ++j)
            cin >> y[j];  // grades

        // compute the average for this student
        avg[i] = std::accumulate(y, y + 5, 0) / 5.0F;
        cout << "ID: " << x[i] << endl;
        cout << "Average: "<< avg[i] << endl;
    }

    // compute the worst average
    float* worst_average = std::min_element(avg, avg + MAX_NUM);

    // get the position in the array where the worst is found
    int position = std::distance(avg, worst_average);

    // output results
    cout << "This person has the worst average: " << x[position] 
         <<".  The average is " << *worst_average << "\n";
}

请注意,平均值存储在数组中。 计算每个人的平均值的方法是使用std::accumulate y数组值,然后除以5.0。

由于我们现在已经有了平均值,因此我们想找到数组中最小的项。 为此, min_element用于获取元素存储位置。

这里的技巧是min_element返回一个指向最小项目的指针,因此我们需要计算该指针距avg数组开头的距离。 为此,使用了std::distance函数。 现在,这给了我们最小项目的位置。

其余代码仅输出结果。

如您所见,唯一涉及的循环是输入循环。 分别使用accumulatemin_element来计算平均值和最差平均值。

暂无
暂无

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

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