John Doe John Doe - 4 years ago 62
C Question

simple math w/ values read from a file into an array

#include <stdio.h>
int main(void)
{
int file, i, total, min, max, num;
float avg;
int scores[1000];
int morescores[1000];
min = 10000000;
max = -10000000;


FILE *afile;
afile = fopen("scores.txt", "r");

i=0;
while(fscanf(afile, "%d", &num) != EOF) {
i++;
}
printf("The number of values in scores.txt is %d\n", i);

//so we know there are 35 values in this file

fclose(afile);


afile = fopen("scores.txt", "r");
i=total=0;
while(fscanf(afile, "%d", &scores[i]) != EOF) {
i++;
total += scores[i];

avg = total/i;

if (scores[i] < min) {
min = scores[i];
} else if (scores[i] > max) {
max = scores[i];
}
}
printf("The total of the integers is %d.\n", total);
printf("The number of integers in the file is %d.\n", i);
printf("The average of the integers is %f.\n", avg);
printf ("The minimum is %d.\n", min);
printf ("The maximum is %d.\n", max);


fclose(afile);
return (0);
}


I am attempting to read all the values from a file scores.txt and do mathematical expressions with these values. I don't know what to call the specific values within the files when doing math with them..when I put scores[i] into the expressions it doesn't work. Any suggestions?

Answer Source

You have to move the incrementation of i

while(fscanf(afile, "%d", &scores[i]) != EOF) {
   i++;

at the end of the loop:

while(fscanf(afile, "%d", &scores[i]) != EOF) {
   ...
   i++;
}

because you store a value in scores[n] and use scores[n+1]...

Your code becomes:

#include <stdio.h>

int main(void) {
   int    num;
   float  avg;
   int    scores[1000];
   int    morescores[1000];
   int    min = 10000000;
   int    max = -10000000;
   FILE * afile = fopen("scores.txt", "r");
   if( ! afile ) {
      perror("scores.txt");
      return 1;
   }
   int    i = 0;
   while( fscanf( afile, "%d", &num ) != EOF ) {
      i++;
   }
   printf("The number of values in scores.txt is %d\n", i);
   fclose(afile);
   afile = fopen("scores.txt", "r");
   int total = 0;
   i = 0;
   while( fscanf( afile, "%d", &(scores[i])) != EOF) {
      total += scores[i];
      if (scores[i] < min) {
        min = scores[i];
      }
      else if (scores[i] > max) {
        max = scores[i];
      }
      i++;
   }
   avg = total/i;
   printf("The total of the integers is %d.\n", total);
   printf("The number of integers in the file is %d.\n", i);
   printf("The average of the integers is %f.\n", avg);
   printf("The minimum is %d.\n", min);
   printf("The maximum is %d.\n", max);
   fclose( afile );
   return 0;
}

And the execution becomes:

aubin@Breizh-Atao ~/Dev/C $ echo 1 2 3 4 5 6 7 8 9 10 > scores.txt
aubin@Breizh-Atao ~/Dev/C $ gcc minMax.c -o MinMax
aubin@Breizh-Atao ~/Dev/C $ ./MinMax 
The number of values in scores.txt is 10
The total of the integers is 55.
The number of integers in the file is 10.
The average of the integers is 5.000000.
The minimum is 1.
The maximum is 10.
aubin@Breizh-Atao ~/Dev/C $ 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download