简体   繁体   English

我对我的输出感到困惑,该输出给出了用户输入的数字的总和的最接近平均值(将小数点四舍五入为整数)

[英]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.

相关问题 我怎样才能摆脱零,如果输入小数,它会打印小数,但如果输入整数,它会打印.00? - How can I get rid of zeros, if decimal is entered it prints decimal, but if whole number is entered it prints .00? 如何将结果输出到用户先前输入的小数点数 (C)? - How do I output a result to the number of decimal-points user previously entered (C)? C中输入数字的平均值-总和始终为0 - Average of entered numbers in C - sum always 0 如何获取strtok()返回十进制数字而不是整数? - How would I get strtok() to return the decimal number and not whole numbers? 我对输出感到困惑 - I'm confused about the output 数字先于用户输入的数字 - Numbers precedes number user entered 在C中,我的程序输出的数字不在用户输入数组内或数组中任何数字之和不输出任何内容 - In C, my program to output a number not inside a user input array or not sum of any numbers in the array is not printing anything 我收到错误无效操作数到二进制 + (有 &#39;float&#39; 和 &#39;float *&#39;) sum+=number; ,在我的程序中计算数字的平均值? - I receive the error invalid operands to binary + (have ‘float’ and ‘float *’) sum+=number; ,in my program to calculate an average of numbers? 在C语言中,迭代函数没有给出正确的输出,该输出应该是不是总和(来自用户输入的数据)或数据数组中的数字? - In C, iterative function is not giving the right output which is supposed to be a number not in sum(from data entered by user) or in data array? 我从用户输入中查找最接近的数字对的 C 程序没有打印正确的输出? - My C program to find closest pair of numbers from user input is not printing the right output?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM