how to work out the sum of a coloumn, in C

nzrusty's Avatar, Join Date: Dec 2006
Newbie Member
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.
#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)
				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.
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
You have posted it as an Article under the Article / Source code section. I have moved it to the Queries and Discussion forum.

Duplicate of help pleas with program. Thread closed.