建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作为函数参数,在max函数中找到5个学生中成绩最高者,并输出其学号
输入:
定义数组时给定5个学生的数据 (1,90), (2,61.5), (3,90.5), (4,82.5), (5,76.5)
输出:
输出学号3
示例:
input
Student(1,90),Student(2,61.5),Student(3,90.5),Student(4,82.5),Student(5,76.5)
output
3
提示:
不要用键盘输入,直接用给定的数组值赋值;输出必须是3
代码如下
#includeusing namespace std; class Student { public: Student(int n, float s) : num(n), score(s) {} int num; float score; }; void max(Student *pt, int n) { Student *p = pt, ma(0, 0); float max; for (max = pt->score; p < pt + n; p++) if ((p->score) > max) { max = p->score; ma = *p; } cout << ma.num << endl; } int main() { Student stu[5] = { Student(1, 90), Student(2, 61.5), Student(3, 90.5), Student(4, 82.5), Student(5, 76.5) }; max(stu, 5); //system("pause"); return 0; }



