简体   繁体   English

如何在 C 中正确平均这些数字?

[英]How can I average these numbers correctly in C?

I wrote some code that reads numbers from another file and it's supposed to input the numbers from the file and output the average.我写了一些从另一个文件中读取数字的代码,它应该输入文件中的数字和 output 的平均值。 The numbers I input are (1,2,3,4,5,6,7,8,9,10).我输入的数字是(1,2,3,4,5,6,7,8,9,10)。 But the average it's giving me is 2,4,6,8.10.但它给我的平均值是 2,4,6,8.10。

How do I correct this?我该如何纠正?

Code:代码:

#include <stdio.h>
#include <stdlib.h>

int main(){
  FILE *myFile;
  myFile = fopen("numbers.txt", "r");
  int numberArray[10];
  int i;
  int sum = 0;
  int n = 0;
  int avg = 0;

  if (myFile == NULL){
    printf("Error Reading File\n");
    exit (0);
  }

  for (i = 0; i < 10; i++){
    fscanf(myFile, "%d,", &numberArray[i] );
  }
  for (i = 0; i < 10; i++){
    sum += n;
    i++;
    avg = (sum / i);
    printf("Average is: %d\n\n", numberArray[i]);
  }

  fclose(myFile);
  return 0;
}

"...The numbers I input are (1,2,3,4,5,6,7,8,9,10) Instead of the average its giving me 2,4,6,8.10. How do I correct this?..." “...我输入的数字是 (1,2,3,4,5,6,7,8,9,10) 而不是平均值,它给了我 2,4,6,8.10。我该如何纠正这个……”

Focusing on only the final loop, a few items to consider that will address this issue, and a few others:只关注最后一个循环,需要考虑的几个项目将解决这个问题,以及其他一些:

Depending on values of numerator and divisor the average might not be exactly correct due to integer division rounding error.根据分子和除数的值,由于 integer 除法舍入误差,平均值可能不完全正确。 if this is an issue for you, the first code snippet addresses it.如果这对您来说是个问题,第一个代码片段会解决它。 If not, the following snippets address only the skipping array elements...如果不是,则以下代码段仅处理跳过的数组元素...

As covered, the following code segment in the original post increments i twice, once in the for() loop, then later in the i++ statement.如前所述,原始帖子中的以下代码段将i递增两次,一次在for()循环中,然后在i++语句中。 The following addresses each of these, also corrects assignment statements, all with with comments...以下地址中的每一个,也更正了赋值语句,所有这些都带有注释......

float sum = 0.0;//to avoid integer division rounding error, use a floating point type
float ave = 0.0;

for (i = 0; i < 10; i++){
    //sum += n;//n does not represent the values strore
    sum += (float)numberArray[i];
    //i++;//not needed, i is incremented in for loop
    avg = (sum/i);
    printf("Average is: %f\n\n", ave);
    //                   ^ changed from d to f, 
    //                     and numberArray[i] to ave
}

Note, if the effects of integer division are acceptable for your purposes, then use the following:请注意,如果integer 除法的效果对于您的目的是可以接受的,则使用以下命令:

int sum = 0;
int ave = 0;

for (i = 0; i < 10; i++){
    sum += numberArray[i];
    avg = (sum/i);
    printf("Average is: %d\n\n", ave);
}

And, if outputting only the final result is required (rather than all of the intermediate values), move the last two statements to just after the for loop:并且,如果只需要输出最终结果(而不是所有中间值),请将最后两个语句移到 for 循环之后:

for (i = 0; i < 10; i++){
    sum += numberArray[i];
}
avg = (sum/i);
printf("Average is: %d\n\n", ave);

"Is it possible to place the given average back into the file? " “是否可以将给定的平均值放回文件中?”

The original statement: myFile = fopen("numbers.txt", "r");原语句: myFile = fopen("numbers.txt", "r"); opened the file for read only.以只读方式打开文件。 But placing the resulting average back into the file requires reopening the file for append and using fputs() :但是将得到的平均值放回文件中需要重新打开append的文件并使用fputs()

...
    fclose(myFile);
    //add the following...
    char sAve[20] = {0};
    myFile = fopen("numbers.txt", "a");
    if(myFile)
    {
          sprintf(sAve, "\nAverage is: %f0.6", ave)
          fputs(sAve, myFile);
          fclose(myFile);
    }
    return 0;
}

In this for loop, you increment i twice, once inside the for()-statement, once at i++.在这个 for 循环中,您将 i 递增两次,一次在 for() 语句中,一次在 i++ 中。

for (i = 0; i < 10; i++){

sum += n;

i++;

avg = (sum/ i);

printf("Average is: %d\n\n", numberArray[i]);

}

If I understand correctly, the second increment should be removed.如果我理解正确,应该删除第二个增量。 Also your intended output is not quite clear to me.此外,您的预期 output 我也不太清楚。 If this does not solve it, maybe specify your question.如果这不能解决问题,也许可以指定您的问题。

If you just want the average of all the numbers first add them all together:如果您只想要所有数字的平均值,请先将它们加在一起:

for(i=0; i<10; i++){
sum+=numberArray[i];
}

Then divide by the number of elements然后除以元素个数

avg = sum/10;

Then you can printout the average:然后你可以打印出平均值:

printf("Average is: %d\n", avg); printf("平均值为:%d\n", avg);

I think this is what your code intends to do.我认为这就是您的代码打算做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM