C++代码输入10个学生的成绩,输出最高成绩
时间: 2024-05-19 22:14:56 浏览: 17
这是一个纯技术问题,我可以回答。你可以使用一个for循环读入学生成绩,并在每次循环中通过比较操作记录当前的最高分数。以下是伪代码:
int max_score = 0;
for(int i = 0; i < 10; i++){
int score;
cin >> score;
if(score > max_score){
max_score = score;
}
}
cout << "最高成绩为:" << max_score << endl;
相关问题
写一段C++代码输入10个学生的成绩,求其平均值,输出最高成绩,并统计低于平均值的人数
以下是您要求的 C 代码:
#include <stdio.h>
int main() {
float scores[10];
float sum = 0;
float avg;
float max = 0;
int count = 0;
// 读取10个成绩
for (int i = 0; i < 10; i++) {
printf("请输入学生 %d 的成绩:", i + 1);
scanf("%f", &scores[i]);
sum += scores[i];
if (scores[i] > max) {
max = scores[i];
}
}
// 计算平均值
avg = sum / 10;
// 统计低于平均值的人数
for (int i = 0; i < 10; i++) {
if (scores[i] < avg) {
count++;
}
}
// 输出结果
printf("平均成绩为:%.2f\n", avg);
printf("最高成绩为:%.2f\n", max);
printf("低于平均值的人数为:%d\n", count);
return 0;
}
当然,这只是一段简单的代码示例,实际应用中还需要对输入进行校验等操作。
设计简单C++学生成绩统计输出保存退出系统源代码
以下是一个简单的C++学生成绩统计输出保存退出系统的源代码,其中包括了基本的输入、统计、输出、保存和退出功能:
```c++
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct Student {
string name;
int score;
};
void input(vector<Student>& students) {
int n;
cout << "请输入学生人数:";
cin >> n;
for (int i = 0; i < n; i++) {
Student s;
cout << "请输入第" << i + 1 << "个学生的姓名和分数:";
cin >> s.name >> s.score;
students.push_back(s);
}
}
void statistics(const vector<Student>& students) {
int sum = 0, max_score = 0, min_score = 100;
for (const auto& s : students) {
sum += s.score;
max_score = max(max_score, s.score);
min_score = min(min_score, s.score);
}
cout << "平均分:" << sum / students.size() << endl;
cout << "最高分:" << max_score << endl;
cout << "最低分:" << min_score << endl;
}
void output(const vector<Student>& students) {
for (const auto& s : students) {
cout << s.name << " " << s.score << endl;
}
}
void save(const vector<Student>& students) {
ofstream ofs("scores.txt");
for (const auto& s : students) {
ofs << s.name << " " << s.score << endl;
}
ofs.close();
cout << "保存成功!" << endl;
}
int main() {
vector<Student> students;
while (true) {
cout << "请选择操作:1.录入学生信息 2.统计成绩 3.输出成绩单 4.保存成绩单 5.退出系统" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
input(students);
break;
case 2:
statistics(students);
break;
case 3:
output(students);
break;
case 4:
save(students);
break;
case 5:
cout << "谢谢使用!" << endl;
return 0;
default:
cout << "输入有误,请重新输入!" << endl;
break;
}
}
}
```
这个程序通过一个 `Student` 结构体来保存学生姓名和分数,使用了 `vector` 容器来保存所有的学生信息。程序提供了五个操作:录入学生信息、统计成绩、输出成绩单、保存成绩单和退出系统。其中,录入学生信息和保存成绩单需要用户手动输入,而统计成绩、输出成绩单和退出系统则直接输出或返回。在保存成绩单时,程序将所有学生的信息按照姓名和分数的顺序保存到一个名为 `scores.txt` 的文件中。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)