I am writing a program that will read in an unknown amount of integers and print the average of them. I can't seem to figure out why this won't work.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]){
int n, amount = 1, sum = 0, average;
FILE *fin = fopen(argv[1], "r");
while(fscanf(fin,"%d",&n) != EOF){
fscanf(fin,"%d",&n);
sum += n;
amount++;
}
average = sum/amount;
printf("%d", average);
fclose(fin);
return 0;
}
The file is
I wrote some test code to print out the sum and amount after the while loop and it printed out 2 2.
EDIT: Nevermind, when i set amount back to 0 it worked. Strange because the first time i had it equal to 0 it didn't work but oh well.