建立一个对象数组,内放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 如何在scanpy上做差异基因和通路富集?
- ¥20 关于#硬件工程#的问题,请各位专家解答!
- ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
- ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
- ¥30 截图中的mathematics程序转换成matlab
- ¥15 动力学代码报错,维度不匹配
- ¥15 Power query添加列问题
- ¥50 Kubernetes&Fission&Eleasticsearch
- ¥15 報錯:Person is not mapped,如何解決?
- ¥15 c++头文件不能识别CDialog