6-2 输出最大值 -- PTA

6-2 输出最大值 (10分)
根据给定的程序,写成相关的成员函数,完成指定功能。

函数接口定义:
定义max函数,实现输出最高成绩对应的学号以及最高成绩值。

裁判测试程序样例:
#include
using namespace std;
class Student
{public:
Student(int n,float s):num(n),score(s){}
int num;
float score;
};

int main()
{Student stud[5]={
Student(101,78.5),Student(102,85.5),Student(103,98.5),
Student(104,100.0),Student(105,95.5)};
void max(Student* );
Student * p=&stud[0];
max( p );
return 0;
}
/* 请在这里填写答案 */

输入样例:

输出样例:
104 100

-----------------------------------------------------分割线--------------------------------------------------

题目分析

  1. 本题主函数内 语句void max(Student* );
    为函数原型声明。
  2. 主函数内定义了Student类的指针p,并将p指向对象数组Student。
  3. 自定义函数max使用了该指针p。

综上分析,我们可以在自定义函数max()中,定义指针p1用于控制循环结束,定义指针max寻找我们所需最大值。

void max(Student* p)
{
 Student *max, *p1;
 max = p;
 for(p1 = p+1; p1 < p+5; p1++)
    if(max->score < p1->score){
        max->num = p1->num;
        max->score = p1->score;
      }
 cout << max->num << " " << max->score;
 } 
  • 7
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值