我有以下程序调用两个函数,一个收集数组的总和,一个收集平均值:
#include <stdio.h>
float array_sum (float myNumbers[],int size)
{
float sum = 0;
int i; /* Variable used in loop counter */
for (i = 0; i < size; ++i)
{
sum +=myNumbers[i];
}
}
float array_average (float myNumbers[],int size)
{
float sum = 0;
float average = 0;
int i; /* Variable used in loop counter */
for (i = 0; i < size; ++i)
{
sum +=myNumbers[i];
}
average = sum/size;
printf("avg = %5.2f", average);
}
int main (void)
{
int size;
int sum;
printf ("Enter The Amount Of Numbers In Your Array: ");
scanf("%i", &size);
float myNumbers[size];
int i;
for (i = 0; i < size; ++i)
{
printf ("Enter the number:");
scanf (" %f",&myNumbers[i]);
}
array_sum(myNumbers,size);
array_average(myNumbers,size);
return 0;
}
我想要做的是在我的程序结束时同时显示结果array_sum
和结果。此外,我认为通过函数而不是再次计算数组总和array_average
会更有效吗?任何想法将不胜感激!array_sum
array_average