C语言简单问题,求大家帮忙看看!

2024-11-08 04:53:54
有2个网友回答
网友(1):

#include
float sum(float *array, int number);
float average(float *array, int number);

void main()
{
float a[10];
int i;
printf("请输入数组:\n");
for(i=0;i<10;i++)
{
scanf("%f",&a[i]);
}

printf("和为%f\n",sum(a,10));
printf("平均数为:%f\n",average(a,10));
}

float sum(float *array, int number)
{
int j;
float s=0;
for(j=0;j s += array[j];
return s;
}

float average(float *array, int number)
{
int j;
float s=0, aver=0;
for(j=0;j s += array[j];
aver = s/number;
return aver;
}

网友(2):

float sum(float *array,int number)
{
int i = 0;
float result = 0;

for (i = 0; i < number; i++)
{
result = *(array + i) + result;
}

return result;
}

float average(float *array,int number)
{
int i = 0;
float result;

for (i = 0; i < number; i++)
{
result = result + *(array + i);
}
return result / number;
}