建立一个对象数组,内放5个学生的数据(学号、成绩).设立一个函数max,用指向对象的指针做函数参数,输入5个学生的学号和成绩,在max 函数中找出 5个学生中成绩最高者,并输出其学号及最高成绩。
时间: 2024-05-10 14:21:07 浏览: 34
```c++
#include<iostream>
using namespace std;
class Student {
public:
int id;
int score;
};
void max(Student* stu, int n) {
int maxScore = -1;
int maxId = -1;
for (int i = 0; i < n; i++) {
if (stu[i].score > maxScore) {
maxScore = stu[i].score;
maxId = stu[i].id;
}
}
cout << "最高成绩的学生ID:" << maxId << endl;
cout << "最高成绩:" << maxScore << endl;
}
int main() {
Student stu[5];
for (int i = 0; i < 5; i++) {
cout << "请输入学生" << i+1 << "的学号和成绩:";
cin >> stu[i].id >> stu[i].score;
}
max(stu, 5);
return 0;
}
```
相关问题
6. 建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。
好的,您的问题是关于建立学生对象数组的函数,以下是示例代码:
```c++
#include <iostream>
using namespace std;
class Student {
public:
int id;
int score;
};
void max(Student* arr, int size) {
Student* maxStudent = &arr[0];
for (int i = 1; i < size; i++) {
if (arr[i].score > maxStudent->score) {
maxStudent = &arr[i];
}
}
cout << "最高者学号为:" << maxStudent->id << endl;
}
int main() {
Student students[5] = {{1, 80}, {2, 90}, {3, 85}, {4, 75}, {5, 95}};
max(students, 5);
return 0;
}
```
这个示例演示了如何创建一个名为 Student 的类,这个类有两个属性:学号 id 和成绩 score。我们创建了一个包含 5 个学生的数组,然后定义了 `max` 函数,该函数接受一个指向 Student 对象数组的指针和数组大小作为参数。函数循环遍历数组并查找成绩最高的学生,然后输出此学生的学号。
希望这个示例代码能够帮到您!如果您有更多的问题,请继续问我。
建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。
以下是代码实现:
```C++
#include <iostream>
using namespace std;
class Student {
public:
int id; // 学号
float score; // 成绩
};
void max(Student* students) {
float max_score = students[0].score; // 设初始的最高成绩为第一个学生的成绩
int max_index = 0; // 最高成绩所在学生在数组中的下标
for (int i = 1; i < 5; i++) {
if (students[i].score > max_score) {
max_score = students[i].score;
max_index = i;
}
}
cout << "学号为 " << students[max_index].id << " 的学生成绩最高,成绩为 " << max_score << endl;
}
int main() {
Student students[5] = {{520, 89}, {521, 78}, {522, 92}, {523, 86}, {524, 95}};
max(students);
return 0;
}
```
上述代码创建了一个名为 Student 的类,用来存储学号和成绩,然后创建了一个 max 函数,它的参数是一个指向 Student 对象的指针。在 max 函数中使用了一个 for 循环,找出数组中成绩最高的学生,在函数结束时输出该学生的学号和成绩。最后在 main 函数中创建了一个包含 5 个学生成绩数据的 Student 对象数组,并将其传递给 max 函数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)