Tuesday, 27 August 2013

reading from a file and printing double ratios in C

reading from a file and printing double ratios in C

I have file which has a series of numbers : 9 1 2 1
I try to findout the ratios subsum/sum
#include <stdio.h>
void main()
{
double subSum = 0;
double sum = 13;
double num = 0;
double x = 0;
FILE* file = fopen("text.txt", "r");
while (fscanf(file, "%f", &num) > 0)
{
subSum = subSum + num;
x = subSum / sum;
printf("%f",x);
printf("\n");
}
fclose(file);
}
but it keeps printing zeros(0.00000000000) why ???

No comments:

Post a Comment