建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。
时间: 2024-05-14 14:13:08 浏览: 8
这是一个关于C++的问题:
可以建立一个学生类,类成员包括学号和成绩。然后再建立一个包含5个学生对象的数组,用指向对象的指针作为参数传递到max函数中。在max函数中,可以使用循环遍历数组中的每个学生对象,比较成绩,找出最高分,并输出对应的学号。
以下是代码示例:
```
#include <iostream>
using namespace std;
class Student {
public:
int id;
int score;
};
void max(Student* students) {
int maxIdx = 0;
for (int i = 1; i < 5; i++) {
if (students[i].score > students[maxIdx].score) {
maxIdx = i;
}
}
cout << "最高分学生的学号为:" << students[maxIdx].id << endl;
}
int main() {
Student students[5] = {{1, 90}, {2, 85}, {3, 92}, {4, 88}, {5, 95}};
max(students);
return 0;
}
```
输出结果:
```
最高分学生的学号为:5
```
相关问题
建立一个对象数组,内放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++
下面是建立对象数组并实现max函数的代码:
```c
#include <stdio.h>
typedef struct {
int id;
int score;
} Student;
Student students[5] = {
{1, 90},
{2, 85},
{3, 95},
{4, 80},
{5, 88}
};
Student* max(Student* students, int n) {
Student* max_student = &students[0];
for (int i = 1; i < n; i++) {
if (students[i].score > max_student->score) {
max_student = &students[i];
}
}
return max_student;
}
int main() {
Student* max_student = max(students, 5);
printf("最高分学生的学号是:%d\n", max_student->id);
return 0;
}
```
输出结果为:
```
最高分学生的学号是:3
```
这个程序中,我们定义了一个结构体Student,包含学号和成绩两个属性。然后我们定义了一个包含5个Student对象的数组students,并初始化了每个对象的学号和成绩。接着我们定义了一个max函数,它接受一个指向Student对象数组的指针和数组长度作为参数,返回一个指向最高分学生的指针。在max函数中,我们遍历整个数组,找到成绩最高的学生,并返回其指针。最后在main函数中调用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)
![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)