成绩数组中有15个同学的考试成绩,请设计输 入、输出、交换成绩三个子程序,对15个成绩从 高分到低分进行排序。必须有指针、函数、数组,用c++编译
时间: 2024-01-27 21:03:38 浏览: 27
好的,以下是代码实现:
```cpp
#include <iostream>
using namespace std;
/* 输入成绩数组 */
void input_scores(int *scores, int len) {
cout << "请输入" << len << "个同学的成绩:" << endl;
for (int i = 0; i < len; i++) {
cin >> *(scores + i);
}
}
/* 输出成绩数组 */
void output_scores(int *scores, int len) {
cout << "成绩排序结果为:" << endl;
for (int i = 0; i < len; i++) {
cout << *(scores + i) << " ";
}
cout << endl;
}
/* 交换两个数的值 */
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
/* 冒泡排序 */
void bubble_sort(int *scores, int len) {
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - 1 - i; j++) {
if (*(scores + j) < *(scores + j + 1)) {
swap(scores + j, scores + j + 1);
}
}
}
}
int main() {
int scores[15];
input_scores(scores, 15);
bubble_sort(scores, 15);
output_scores(scores, 15);
return 0;
}
```
使用指针可以方便地对数组进行操作,函数可以使程序结构更加清晰,数组则是存储成绩数据的主要数据结构。在 C++ 中,使用了 `iostream` 头文件和 `using namespace std` 语句来进行输入输出。