# How to sum up each column of matrices

• 04-16-2003
tennizbal
How to sum up each column of matrices
HI,

Pardon me I'm not very good in looping programs and still learning every now and then....:(

Assuming I have a N rows by 3 columns matrix.
I have these data stored in a text file initially.
Now I wish to extract these data from the text file and perform further mathematical operations.

My main concern:

1. How to Extract the 3 columns from the text file in order to proceed to the 2nd step:

2. How do I sum up each column?? :confused:

I really appreciate if anyone can help me ...
• 04-16-2003
read everything into a 2D array of Nx3. Use a loop variable called i which starts at 0 and goes to N - 1 and within the loop add all the values of array[i][0] to get the first column total, array[i][1] to get the total of the second column, and array[i][2] to get the total of the third column.
• 04-16-2003
tennizbal
Performing Mathematical operations On a Txt file from C++ Program
Hi,

I'm wondering if it's possible to perform mathematical operations, i.e. addition, on a txt file via my C++ Program?:(

As related to my previous thread, I have to write the compiled data (in C++) into txt file format and then to retrieve this txt file later for further use.

All I can think of is the use of:

... while (!filepointer4.eof ())
{
double vector[3];
....filepointer << vector[0] << vector[1] << vector[2];
....
I know this loop will extract the data I need from that particular file.

I'm really confused on how to sum up all data i.e. I want something like :

X = sumation of vector[0] from entire txt file
Y = sumation of vector[1] from entire txt file
Z = sumation of vector[2] from entire txt file

-----------------------------------------------------------------------------------

P.S. I appreciate all replies on my previous thread. Thank you.:(
• 04-16-2003
tennizbal
I get the hint. ...

But can anyone give me a clearer explanation about programming wise...

I'm using something like while (!filepointer4.eof()).... as to extract the text file's data. But the adding part doesn't seem logical....

```int sum = 0; ifstream ReadFile; ... while(something) {   ReadFile >> Temp;   sum += Temp; }```
```int input[3][3]; int i, j, x; i = -1; while( i less than 3) {   j = 0;   increment i;   while(j less than 3)       read in value to x       assign x to input[i][j]       increment j;   }```