首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算数组元素的平均值并显示它们

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

Stack Overflow用户
提问于 2014-09-15 17:36:10
回答 4查看 1.9K关注 0票数 0

问题是:一个由40名学生组成的班级获得了5次考试的成绩。实现一个计算最差平均成绩的函数,并显示所有平均成绩最差的学生的ID。‎。

我已经计算了平均数,但不知道如何计算最坏的平均数(在40名学生中最低的平均数中),并显示有这个数字的ID号。

这就是我到目前为止所写的:

代码语言:javascript
复制
#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;
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-09-15 17:45:51

就像这样:

注:我补充了一些重要的陈述!

代码语言:javascript
复制
#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;
    }
};
票数 0
EN

Stack Overflow用户

发布于 2014-09-15 17:42:53

所以你想把这些平均值存储在一个std::vector<float>中,std::sort它并得到最低值。然后回去找那些有平均水平的学生。

票数 0
EN

Stack Overflow用户

发布于 2014-09-15 18:09:20

工作实例

代码语言:javascript
复制
#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));
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25853656

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档