6-8 输出结构体数组中年龄最大者的数据

给定程序中,函数fun的功能是:将形参std所指结构体数组中年龄最大者的数据作为函数值返回,并在主函数中输出。

函数接口定义:

struct student fun(struct student  std[], int  n);

其中 std 和 n 都是用户传入的参数。 函数fun的功能是将含有 n 个人的形参 std 所指结构体数组中年龄最大者的数据作为函数值返回。

裁判测试程序样例:

#include <stdio.h>

struct student
{char  name[10];
 int  age;
};
struct student fun(struct student  std[], int  n);
int main()
{
struct student std[5]={"aaa",17,"bbb",16,"ccc",18,"ddd",17,"eee",15  };
struct student  max;
max=fun(std,5);
printf("\nThe result:\n");
printf("\nName : %s,   Age :  %d\n", max.name,max.age);
return 0;
 }


/* 请在这里填写答案 */

输出样例:


The result:

Name : ccc,   Age :  18

代码如下:

 

struct student fun(struct student  std[], int  n)
{
	int max=0,i;
	for(i=1;i<n;i++)
	{
		if(std[i].age>std[max].age)
		max=i;
	}
	return std[max];
}

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值