3 Replies - 6926 Views - Last Post: 11 October 2011 - 03:50 PM Rate Topic: -----

#1 C-Boo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-October 11

How to find average of integers in a file?

Posted 11 October 2011 - 03:00 PM

Hi, I'm currently working on a problem called Find the average of positive integers. I have been successful at getting the code to read the file but I cant seem to figure out how to get the average to be computed in the code. If anyone could help me out that would be great. Thanks !
#include <stdio.h>
#include <math.h>

int main(int argc, char* argv[]){
  int i,sum,digit, n,avg;
  FILE *fin;
  sum = 0;

  fin = fopen("testdata29", "r");
  for(i = 0; (i<=sum); i++){
    fscanf(fin, "%d", &n);
      digit += n;
      n++;
      avg = avg +(digit/ sum);
      printf("The average is %d.\n", avg);
  }
  fclose(fin);
  return 0;
}



ps. I have tried looking up how to find the average but all i come up with is the basic way to average out numbers not in a way that pertains to my coding.

Errors:
Float point exception and I'm not sure what that means exactly.

This post has been edited by C-Boo: 11 October 2011 - 03:02 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to find average of integers in a file?

#2 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: How to find average of integers in a file?

Posted 11 October 2011 - 03:16 PM

Well in your loop just add your numbers and count your numbers, then after the loop divide the total sum by the number of numbers.

The floating point exception is probably because you initialize sum at 0 and the you divide something by sum(0) which not valid, not in programming not in mathematics.
Was This Post Helpful? 1
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: How to find average of integers in a file?

Posted 11 October 2011 - 03:20 PM

Also make sure that the numbers you are reading in are also integers. If you have decimal values in your numbers file, then you will need to use floats to hold the answer.

Play with this...

#include <stdio.h>
#include <math.h>

int main(int argc, char* argv[]){
  int i = 0, sum = 0, n = 0,avg = 0;
  FILE *fin;

  // Open file
  fin = fopen("testdata29", "r");
  
  // Keep reading in integers (which are placed into "n") until end of file (EOF)
  while(fscanf(fin, "%d", &n) != EOF){
      // Add number to sum
      sum += n;

      // Increment counter for number of numbers read
      i++;

      // Average is sum of numbers divided by numbers read
      avg = (sum / i);
  }

  // After the loop is done, show the average
  printf("The average is %d.\n", avg);

  fclose(fin);
  return 0;
}



:)
Was This Post Helpful? 1
  • +
  • -

#4 C-Boo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 11-October 11

Re: How to find average of integers in a file?

Posted 11 October 2011 - 03:50 PM

Thank you very much for your help! I really appreciate it. I was close! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1