[英]I am confused about my output that gives the closest average of sum of numbers entered by the user.(rounds the decimal to a whole number)
Complete the following program to find the closest number to average in the array. 完成以下程序,找到数组中平均数最接近的数字。 For example, the array {2, 4, 6, 3, 9, 10} has average of 5.666667 and closest number to average in the array is 6. Note that closest number can be larger than or smaller than the average.
例如,数组{2、4、6、3、9、10}的平均值为5.666667,并且数组中与平均值最接近的数为6。请注意,最接近的数可以大于或小于平均值。
#include <stdio.h>
#include <math.h>
int main()
{
int i;
double num[6],average, sum=0, closest;
printf("Enter 6 doubles\n"); //2,4,6,9,3,10
for (i=0; i<6; i++)
scanf("%lf",&num[i]);
//professor's code
for (i=0; i<6; i++)
sum += num[i]; //sum=34
average=sum/6; //5.6666667
closest = num[0]; //why are we even using this??? //initialize?
for (i=0; i<6; i++)
if(fabs(num[i]-average) < fabs(closest-average)) //|6-5.7|<|0-5.7|-> 0.7<5.7->true
closest = num[i]; // why is it giving 6??
//where is it being rounded to the closest number??
//professor's code ends
printf("Closest is %lf\n", closest);
return(0);
}
//Output:
//Closest is 6
Your title and code comments suggest that you think the program (always) returns the whole number closest to the average of its inputs. 您的标题和代码注释表明您认为该程序(总是)返回最接近其输入平均值的整数。 It does not.
它不是。 It returns the input that is closest to the average of all the inputs.
它返回输入最接近平均值的全部投入。 That will of course be a whole number if all the inputs are whole numbers, but you're inputting them as floating-point numbers, so they need not be whole numbers.
如果所有输入都是整数,那当然就是整数,但是您将它们输入为浮点数,因此它们不必是整数。 The output therefore needn't be one, either.
因此,输出也不必为一。
For example, if these inputs are presented to your program: 例如,如果这些输入出现在您的程序中:
1 2 2 2 2 1.8
1 2 2 2 2 1.8
, it will accept them and output ,它将接受它们并输出
Closest is 1.800000
最近的是1.800000
. 。 Additionally, the output is not certain to be particularly close to the average.
此外,不确定输出是否特别接近平均值。 For example, try these inputs:
例如,尝试以下输入:
1 1 1 1 1 1000000
1 1 1 1 1 1000000
. 。 You should get
你应该得到
Closest is 1.000000
最近是1.000000
. 。 That's a whole number, but it misses the average by about 166666.
这是一个整数,但比平均值少了166666。
With respect to the questions embedded in the code: 关于代码中嵌入的问题:
closest = num[0]; //why are we even using this??? //initialize?
The loop immediately following that in the code looks for inputs that are closer to the average than closest
is. 紧接着,在代码回路寻找更接近平均投入比
closest
的。 That's meaningless if closest
has not been assigned a value, and it potentially leads to the wrong result if closest
does not contain a value equal to one of the inputs. 这是毫无意义的,如果
closest
尚未分配的值,它可能会导致错误的结果,如果closest
不包含等于一个输入的值。 Of course, with closest
starting with the same value as num[0]
, the output would be the same if the loop started testing at index 1 instead of index 0. 当然,
closest
起始值与num[0]
相同,如果循环从索引1而不是索引0开始测试,则输出将相同。
//where is it being rounded to the closest number??
It is not rounded. 它不是四舍五入的。 At that point,
closest
already contains a value equal to one of the inputs. 此时,
closest
值已包含等于输入之一的值。 It does not need to be rounded, and in fact rounding would in many cases yield a result that was not among the inputs. 不需要四舍五入,实际上四舍五入会产生不在输入范围之内的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.