简体   繁体   中英

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. The numbers I input are (1,2,3,4,5,6,7,8,9,10). But the average it's giving me is 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?..."

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. 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. 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:

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 (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"); opened the file for read only. But placing the resulting average back into the file requires reopening the file for append and using 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 = 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. 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);

I think this is what your code intends to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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