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

时间:2014-09-15 17:36:10

标签: c++ visual-c++

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

我已经计算了平均值,但不知道如何计算最差平均值(如40名学生的最低平均值)并显示具有此数字的身份证号码。

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

#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;
}

4 个答案:

答案 0 :(得分:0)

因此,您希望将这些平均值存储在std::vector<float>std::sort中并获得最低值。然后回去找那些有平均水平的学生。

答案 1 :(得分:0)

这样的事情:

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

#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;
    }
};

答案 2 :(得分:0)

working example

#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));
}

答案 3 :(得分:0)

以下是使用std::accumulatestd::min_element执行此操作的更多C ++方式(为简洁起见,我删除了对任何&gt; 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。

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

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

其余代码只输出结果。

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