建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其相关数据。
时间: 2024-04-29 18:24:58 浏览: 9
好的,这是一个关于编程的问题。以下是相关的代码:
```cpp
#include <iostream>
using namespace std;
class Student {
public:
int id;
int score;
};
void max(Student* students, int size) {
Student* p = students;
Student* max = p;
for (int i = 0; i < size; i++) {
if (p->score > max->score) {
max = p;
}
p++;
}
cout << "学号:" << max->id << ",成绩:" << max->score << endl;
}
int main() {
Student students[] = {
{1, 90},
{2, 85},
{3, 92},
{4, 88},
{5, 87}
};
max(students, 5);
return 0;
}
```
输出结果为:
```
学号:3,成绩:92
```
希望可以帮到您!
相关问题
建立一个对象数组,内放5个学生的学号和成绩,设立一个函数max,用指向对象的指针做函数参数,在max函数种找出5个学生中成绩最高者,并输出其学号。用C++实现
以下是实现代码:
```cpp
#include <iostream>
using namespace std;
class Student {
int id;
float score;
public:
Student(int i, float s) : id(i), score(s) {}
int getId() { return id; }
float getScore() { return score; }
};
void max(Student *arr[], int size) {
int maxIndex = 0;
for (int i = 1; i < size; i++) {
if (arr[i]->getScore() > arr[maxIndex]->getScore()) {
maxIndex = i;
}
}
cout << "最高分数的学生学号为:" << arr[maxIndex]->getId() << endl;
}
int main() {
Student s1(1001, 89.5);
Student s2(1002, 78.5);
Student s3(1003, 92.0);
Student s4(1004, 85.5);
Student s5(1005, 91.0);
Student *arr[5] = { &s1, &s2, &s3, &s4, &s5 };
max(arr, 5);
return 0;
}
```
这里创建了一个 `Student` 类,包含学生的学号和成绩属性,然后在 `main` 函数中创建了 5 个学生对象,并将它们的指针存储在一个指针数组 `arr` 中。最后调用 `max` 函数,使用指向对象的指针作为参数,在其中遍历整个数组,找到成绩最高的学生,并输出其学号。
建立一个对象数组,内放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 函数。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)