请问下该怎么使用数组做函数的参数,计算2个不同长度的数组中所有元素的平均值?

用数组做函数的参数,计算2个不同长度的数组中所有元素的平均值并打印出来。
已知两数为:
Flaot pot_1 [5]={99,88,77,66,5}
pot_2 [10]={11,22,33,44,55,99,88,77,66,10}
源代码如下
#include<stdio.h>
int average(int array[],int n)
{
int i;
int aver , sum=array[0];
for(i=1;i<n;i++)
sum=sum+array[i];
aver=sum/n;
return(aver);
}
main()
{
int pot_1[5]={99,88,77,66,5};
int pot_2[10]={11,22,33,44,55,99,88,77,66,10};
printf("the average of A is %d\n", average(pot_1,5));
printf("the average of B is %d\n", average(pot_2,10));
while(1);
}
阅读程序回答下列问题:
1. 写出程序执行的结果。
2. 哪是形参?哪是实参?
3. 程序中是否有函数声明语句?若无为什么不需要?

交互式爱情
浏览 225回答 3
3回答

Cats萌萌

#include<stdio.h>int average(int array[],int n) //在主函数前面定义的,所以不用再声明了;{int i;int aver , sum=array[0];for(i=1;i<n;i++)sum=sum+array[i];aver=sum/n;return(aver);}main(){int pot_1[5]={99,88,77,66,5};int pot_2[10]={11,22,33,44,55,99,88,77,66,10};printf("the average of A is %d\n", average(pot_1,5));printf("the average of B is %d\n", average(pot_2,10));while(1);}1运行结果为the average of A is 67the average of B is 502哪是形参?哪是实参?int average(int array[],int n) 函数中的int array[],int n是形参, int pot_1[5]={99,88,77,66,5};int pot_2[10]={11,22,33,44,55,99,88,77,66,10};这两个数组是实参;3;如果你的调用函数在主函数的前面,就不用定义了,要是在主函数后面,就必须在主函数里先声明,才能用;

叮当猫咪

1. 写出程序执行的结果。答:程序的执行结果是:the average of A is 67the average of B is 502. 哪是形参?哪是实参?答:int array[]和int n是形参,pot_1, 5, pot_2和10是实参3. 程序中是否有函数声明语句?若无为什么不需要?答:程序中没有函数声明语句,因为int average(int array[],int n)函数的定义在函数调用之前了。如果是函数的定义放在函数的调用之后的话,则需要在main函数中添加函数声明语句了。如下面这样就需要汪加函数声明了:#include<stdio.h>main(){int average(int array[],int n);int pot_1[5]={99,88,77,66,5};int pot_2[10]={11,22,33,44,55,99,88,77,66,10};printf("the average of A is %d\n", average(pot_1,5));printf("the average of B is %d\n", average(pot_2,10));return 0;}int average(int array[],int n){int i;int aver , sum=array[0];for(i=1;i<n;i++)sum=sum+array[i];aver=sum/n;return(aver);}

慕尼黑5688855

1. 写出程序执行的结果。答:结果如下:the average of A is 67the average of B is 502. 哪是形参?哪是实参?答:array和n是形参,pot_1, 5, pot_2和10是实参3. 程序中是否有函数声明语句?若无为什么不需要?答:程序中没有函数声明语句,因为函数的实现放在了使用它的语句的前面了
打开App,查看更多内容
随时随地看视频慕课网APP