我必须计算数组内整数的平均值:
int main(void)
{
int num;
printf("How many elements do you want to add in to the array? --> ");
scanf("%d", &num);
float array[10];
float total, average;
for(int i=0; i<num; i++){
printf("Insert the element: ");
scanf("%f", &array[i]);
total = total + array[i];
}
average = total/num;
printf("The average is: %.2f\n", total);
return 0;
}
输出:
How many elements do you want to add in to the array? --> 3
Insert the element: 5
Insert the element: 6
Insert the element: 7
The average is: 18.00
我希望输出是
6.00
,但它是18.00
。我该如何解决? 最佳答案
#include<stdio.h>
int main()
{
float avg,sum=0;
int i;
float marks[3];//array declaration
for(i=0;i<=2;i++)
{
printf("enter marks\n");
scanf("%f",&marks[i]); //store data in array
printf("you have entered %f\n",marks[i]);
sum=sum+marks[i];
}
printf("\nou have entered sum=%f\n",sum);
avg=sum/3;
printf("average marks=%f\n",avg);
return 0;
}
关于c - 计算数组中整数的平均值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57339588/