|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
找出十位同学中的最大年龄
#include <stdio.h>
#include <stdlib.h>
#define NUM 10
int max(int n[]);
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int max(int n[])
{
int i,max=n[0];
for(i=0;i<NUM;i++)
{
if(n[i]>n[0])
max=n[i];
}
return max;
}
int main(int argc, char *argv[]) {
int result;
int array[10];
printf("请输入年龄:");
scanf("%d",&array);
result=max(array);
printf("最大年龄为:%d",result);
return 0;
}
输入的时候只是简单的&array是不行的,必须用一个循环写进去。
- #include <stdio.h>
- #include <stdlib.h>
- #define NUM 10
- int max(int array[]);
- /* run this program using the console pauser or add your own getch, system("pause") or input loop */
- int max(int array[])
- {
- int i;
- int max=array[0];
- for(i=0;i<NUM;i++)
- {
- if(max<=array[i])
- {
- max=array[i];
- }
- }
- return max;
- }
- int main(void)
- {
- int i;
- int result;
- int array[10];
- printf("请输入年龄:");
- for(i=0;i<NUM;i++)
- {
- scanf("%d",&array[i]);
- }
- result=max(array);
- printf("最大年龄为:%d",result);
- return 0;
- }
复制代码
|
|