# How to find average of integers in a file?

Page 1 of 1

## 3 Replies - 22808 Views - Last Post: 11 October 2011 - 03:50 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=250848&amp;s=8a26d698f5de4370626ed22b197da367&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 C-Boo

Reputation: 0
• 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

Reputation: 454
• Posts: 864
• 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.

### #3 Martyr2

• Programming Theoretician

Reputation: 5178
• Posts: 13,898
• 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){
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;
}

```

### #4 C-Boo

Reputation: 0
• 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!