How to sum up each column of matrices

This is a discussion on How to sum up each column of matrices within the C++ Programming forums, part of the General Programming Boards category; HI, Pardon me I'm not very good in looping programs and still learning every now and then.... Assuming I have ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    3

    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??


    I really appreciate if anyone can help me ...
    Last edited by tennizbal; 04-16-2003 at 09:13 AM.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    3

    Unhappy 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


    Please Help me ....
    -----------------------------------------------------------------------------------

    P.S. I appreciate all replies on my previous thread. Thank you.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    3
    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....

    Am I on the right track ?? Please I need more information....

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    int sum = 0;
    ifstream ReadFile;
    
    ...
    
    while(something)
    {
       ReadFile >> Temp;
       sum += Temp;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Here's a possible schematic for reading into a 2D array from a file. This presumes you know how many values there are in the file. The exact way you'll do this depends on the exact format of the file, what you know about the file in advance, what tools you have at your disposal (for example vectors instead of arrays) etc.

    Code:
    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;
       
    }
    Once you have read the file into input, then you can manipulate the material, such as adding up the column (or row totals) as posted before.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Minor Problem
    By stewie1986 in forum C Programming
    Replies: 6
    Last Post: 11-30-2007, 08:40 AM
  2. a sum equal to or in excess of 100
    By lyoncourt in forum C Programming
    Replies: 6
    Last Post: 10-07-2007, 06:43 PM
  3. Retrieving Multiple inputs with fgets?
    By Axel in forum C Programming
    Replies: 25
    Last Post: 09-13-2005, 05:04 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. string to int conversion not using atoi()
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 05-19-2004, 01:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21