B
Bill Reed
I'm trying to add numbers from a text file and find the average. The
numbers in the file are:
25 50 75 100
I expect the result to be 62.50 but I get 85.00. I've twiddled with
the condition expression of the for statement but apparently I have no
idea what I'm doing. Post-increment or pre-increment seems to make no
difference. Any help appreciated.
/* avg.c : find the average of n integers
in a text file */
#include <stdio.h>
int main() {
int num, i;
float sum;
for(i = 0; i != '\n'; i++)
{
scanf("%d", &num);
sum += num;
}
printf("The average is %.2f\n", sum/i);
return 0;
}
Thanks,
Bill
numbers in the file are:
25 50 75 100
I expect the result to be 62.50 but I get 85.00. I've twiddled with
the condition expression of the for statement but apparently I have no
idea what I'm doing. Post-increment or pre-increment seems to make no
difference. Any help appreciated.
/* avg.c : find the average of n integers
in a text file */
#include <stdio.h>
int main() {
int num, i;
float sum;
for(i = 0; i != '\n'; i++)
{
scanf("%d", &num);
sum += num;
}
printf("The average is %.2f\n", sum/i);
return 0;
}
Thanks,
Bill