用c++实现全班成绩统计

​#include <iostream>
#include <vector>
using namespace std;

int main() {
  vector<int> scores;
  int num_students, score, total_score = 0;

  cout << "请输入学生数量:" << endl;
  cin >> num_students;

  for (int i = 1; i <= num_students; i++) {
    cout << "请输入" << i << "号学生的成绩:" << endl;
    cin >> score;
    scores.push_back(score);
    total_score += score;
  }

  double average_score = total_score / num_students;
  int max_score = scores[0], min_score = scores[0];

  for (int i = 1; i < num_students; i++) {
    if (scores[i] > max_score) max_score = scores[i];
    if (scores[i] < min_score) min_score = scores[i];
  }

  cout << "班级平均分为:" << average_score << endl;
  cout << "最高分为:" << max_score << endl;
  cout << "最低分为:" << min_score << endl;

  return 0;
}

        这个程序要求用户输入学生数量和每个学生的成绩,然后计算班级的平均分、最高分和最低分,并将这些信息输出到屏幕上。

        程序首先使用一个vector来存储每个学生的成绩,使用push_back函数将每个成绩添加到向量中。然后,程序遍历vector来计算平均分、最高分和最低分。最后,程序将这些数据输出到屏幕上。

        请注意,此程序假设用户输入的每个值都是有效的整数。如果输入的值不是整数,则程序可能会产生意外的结果或崩溃。在实际应用中,需要添加错误处理代码来确保输入的数据是有效的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Galaxy银河

你的鼓励是我的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值