建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。
1条回答 默认 最新
- qtchen_1988 2021-05-28 12:24关注
#include <iostream> #include <string.h> #define BUF_SIZE 5 using namespace std; struct Stud{ unsigned int id; int score; }; unsigned int max(Stud *s,int n) { unsigned int id = 0; int score; if(s) { score = s[0].score; id = s[0].id; for(int i=0;i<n;i++) { if(score < s[i].score) { score = s[i].score; id = s[i].id; } } } return id; } int main() { Stud stud[BUF_SIZE]; memset(stud,0x0,sizeof(stud)); for(int i=0;i<BUF_SIZE;i++) std::cin >> stud[i].id >> stud[i].score; int id = max(stud,BUF_SIZE); std::cout << id << std::endl; return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
- ¥500 52810做蓝牙接受端
- ¥15 基于PLC的三轴机械手程序
- ¥15 多址通信方式的抗噪声性能和系统容量对比
- ¥15 winform的chart曲线生成时有凸起
- ¥15 msix packaging tool打包问题
- ¥15 finalshell节点的搭建代码和那个端口代码教程
- ¥15 Centos / PETSc / PETGEM
- ¥15 centos7.9 IPv6端口telnet和端口监控问题
- ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作