前言:
此篇面向C语言初学者,针对函数调用、循环和判断方面的练习,这里的知识点是函数调用里的数组元素作函数实参。
正文:
代码展示:
#include<stdio.h>
int main() {
int max(int x,int y); //函数声明
int a[10],m,n,i;
printf("enter 10 integere number:");
for(i=0; i<10; i++) //输入10个数给a[0]~a[10]
scanf("%d",&a[i]);
printf("\n");
for(i=1,m=a[0],n=0; i<10; i++) {
if(max(m,a[i])>m) { //若max函数返回的值大于m
m=max(m,a[i]); //max函数返回的值取代m原值
n=i; //把此数组元素的序号记下来,放在n中
}
}
printf("The largest number is %d\nit is the %dth number.\n",m,n+1);
return 0;
}
int max(int x,int y) { //定义max函数
return(x>y?x:y); //返回x和y中的大者
}
注意:
①这里把处理问题的代码写成了max( )函数的形式。
②数组元素可以用作函数实参,不能用作形参。
总结:
运行结果:

这篇文章适合C语言初学者,通过实例讲解如何利用函数调用来找出数组中的最大值。程序定义了一个max函数,接收两个整数参数并返回较大的那个。在主函数中,用户输入10个整数存储在数组中,然后遍历数组,用max函数比较当前最大值并记录最大值的索引。最终输出最大值及其在数组中的位置。
。&spm=1001.2101.3001.5002&articleId=128711726&d=1&t=3&u=9279d1ecdc9947d499aa46c5251e3f9f)

被折叠的 条评论
为什么被折叠?



