练习03-03-04 数组元素作函数参数
要求:输入10个数,要求输出其中值最大的元素和该数是第几个数
输出样例:
The array is:
-5 -1 4 -2 3 1 -5 3 4 -3
The max = 4, its position = 3
--------------------------------
代码如下:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int max(int x,int y);
srand((unsigned)time(NULL));
int a[10],i,m,k;
printf("The array is:\n");
for(i=0;i<10;++i){
a[i]=rand()%10-5;
printf("%3d",a[i]);
}putchar('\n');
for(i=0,m=a[0],k=0;i<10;++i){
if(max(m,a[i])>m){
m=a[i];
k=i;
}
}
printf("The largest number = %d, its position = %d",m,k+1);
}
int max(int x,int y)
{
return(x>y?x:y);
}