Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   how to work out the sum of a coloumn, in C (http://www.go4expert.com/forums/sum-coloumn-c-t2219/)

nzrusty 12Dec2006 23:03

how to work out the sum of a coloumn, in C
 
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.
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);
       
       
}


shabbir 13Dec2006 11:29

Re: how to work out the sum of a coloumn, in C
 
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.


All times are GMT +5.5. The time now is 22:21.