Im trying to work out the total values of a table, which i have read from a file

x y

1.0 2.876

2.6 4.234

3.7 8.874

4.5 7.698

6.0 9.932

I want to work out the total values of x, i.e sum of.

How would i implement this, please. thanks, here is what i have so far, im new to programming so the sum of section at the end maybe a bit wrong, can u please help.

x y

1.0 2.876

2.6 4.234

3.7 8.874

4.5 7.698

6.0 9.932

I want to work out the total values of x, i.e sum of.

How would i implement this, please. thanks, here is what i have so far, im new to programming so the sum of section at the end maybe a bit wrong, can u please help.

Code:

#include <stdio.h> #include <math.h> int main(void) { char line [101]; char *line_ptr; double x, y; int no_values=-1; double sum_x; int i=0; FILE *input_stream; if ((input_stream=fopen("test_data.dat", "r")) != NULL) { while ((line_ptr=fgets(line, sizeof(line), input_stream))!= NULL) { if ((sscanf (line, "%d %d", &x, &y)) != 2) { no_values++; fprintf (stdout, "%s\n", line); } } } fprintf(stdout, "\n\n\n\nno_values: %d\n", no_values); for (i=0; i<=no_values; i++) { sum_x= x; } fprintf(stdout, "\n\n\n\nsum_x: %lf\n", sum_x); }

*Last edited by shabbir; 13Dec2006 at 11:28.. Reason: Code formating.*