[英]Avergaging elements in array
I'm trying to add the elements in an array.我正在尝试在数组中添加元素。 It's just a simple program to calculate the average of student grades.这只是一个计算学生平均成绩的简单程序。 I know this is probably a rudimentary way to code this, I'm looking to do it more efficiently.我知道这可能是一种基本的编码方式,我希望更有效地做到这一点。 However my code is not returning the average.但是我的代码没有返回平均值。 I would greatly appreciate any help.我将不胜感激任何帮助。 I did try this with a for loop but got the same incorrect answer.我确实用 for 循环尝试过,但得到了同样的错误答案。
#include <stdio.h>
int main()
{
int grades[6];
int average;
int sum = 0;
printf("Please enter your five test scores:\n");
scanf("%d", &grades[0]);
scanf("%d", &grades[1]);
scanf("%d", &grades[2]);
scanf("%d", &grades[3]);
scanf("%d", &grades[4]);
scanf("%d", &grades[5]);
sum = sum + grades[6];
average = sum / 5;
printf("The average of the students test scores is %d:\n", average);
return 0;
}
You should sum all grades and then divide by their amount (in your case it is 6 not 5, because grades
array has 6 elements).您应该将所有等级相加,然后除以它们的数量(在您的情况下,它是 6 而不是 5,因为grades
数组有 6 个元素)。 Here is a code sample:这是一个代码示例:
#include <stdio.h>
int main()
{
int grades[6];
int average;
int sum = 0;
printf("Please enter your six test scores:\n");
scanf("%d", &grades[0]);
scanf("%d", &grades[1]);
scanf("%d", &grades[2]);
scanf("%d", &grades[3]);
scanf("%d", &grades[4]);
scanf("%d", &grades[5]);
for (int i = 0; i < 6; i++)
sum = sum + grades[i];
average = sum / 6;
printf("The average of the students test scores is %d:\n", average);
return 0;
}
I'm trying to add the elements in an array.我正在尝试在数组中添加元素。
This is maybe already a wrong track regarding the title (and the code) where it is about averaging elements in an array .这可能已经是关于标题(和代码)的错误轨道,它是关于平均数组中的元素。
How you build the array is up to you;如何构建阵列取决于您; I choose the simplest way.我选择最简单的方法。 An important decisions is: is size of array and number of values the same?一个重要的决定是:数组的大小和值的数量是否相同? That is what n_grades
does.这就是n_grades
所做的。 The four zeroes in the array initialization illustrate the difference.数组初始化中的四个零说明了差异。
An average most likely should be a floating point number.平均值很可能应该是浮点数。
A nasty problem with averages is the lurking overflow.平均值的一个令人讨厌的问题是潜伏的溢出。 Very unlikely in this setting, but there is a more robust (and elegant) algorithm.在这种情况下不太可能,但有一个更健壮(和优雅)的算法。 The (double)
cast is the un-elegant part, but is needed because the division is between two integers . (double)
cast 是不优雅的部分,但由于除法是在两个integers之间,所以需要它。 Still this is the compact core:这仍然是紧凑的核心:
for (i = 0; i < n_grades; i++)
aver += (double) grades[i] / n_grades;
corresponding to the math formula:对应数学公式:
i<n
A = Sum G_i/n
i=0
("Sum" is the big Sigma) (“总和”是大西格玛)
#include <stdio.h>
int main()
{
int grades[] = {10,10,9,10,11,11,0,0,0,0};
int n_grades = sizeof grades / sizeof*grades; // use all elements
//n_grades = 6; // use only first n elements
double aver = 0;
for (int i = 0; i < n_grades; i++)
aver += (double) grades[i] / n_grades;
printf("The average of the students test scores is %f (n= %d):\n",
aver, n_grades);
return 0;
}
Now it prints:现在它打印:
The average of the students test scores is 6.100000 (n= 10):
or, uncommented, ie limited to 6 grades::或者,未注释,即限制为 6 个等级:
The average of the students test scores is 10.166667 (n= 6):
You're assuming that grades[6]
holds the sum of all the values in the grades
array, which is wrong of course.您假设grades[6]
包含grades
数组中所有值的总和,这当然是错误的。
You need something like:你需要类似的东西:
for (int i = 0; i < 6; i++)
sum = sum + grades[i];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.