Thread: Matrix

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    4

    Matrix addition using recursion

    hi guys, im doing a matrix addition using recursion. however it dosent seems to work.

    Code:
    //for (row=0; row<row_size; row++)
            //for (col=0; col<col_size; col++) 
                //mtxC[row][col] = mtxA[row][col] + mtxB[row][col]; using of for loop
                
    
    // using of recursion
    
                for(counter ;counter<1; counter++)
                {
                   store = col_size;            
                }  //col_size is the maximum size of the column eg. mtxA[][col_size]
    
                if(row_size==0 && col_size == 0)
                {
                         mtxC[row_size][col_size] = mtxA[row_size][col_size] + mtxB[row_size][col_size];
                         return mtxC,mtxA,mtxB,row_size,col_size;   
                    // if both row_size and col_size reaches 0 which means it has comes to the end of the program and return all the values back to the place where its being called.
                }
                else
                {
                mtxC[row_size][col_size] = mtxA[row_size][col_size] + mtxB[row_size][col_size];
                            if(row_size!=0 && col_size==0) 
                            {
                            col_size=store;
                            row_size--;
                            col_size++;
                            }
                return sumMatrix(mtxC,mtxA,mtxB,row_size,col_size-1); 
                }
    Last edited by 花颂; 03-20-2013 at 07:45 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    4
    Quote Originally Posted by laserlight View Post
    How does it not work?
    u see, i cant find out which part of my coding went wrong.
    assume the user enter a 2*2 array which is --> [0][0] [0][1]
    [1][0] [1][1]
    as a result col_size=1 and row_size=1
    and it will start call up this function sumMatrix .
    passing col_size value to store once only so it will not be affected by any changes that is about to make on col_size
    subsequently the program will find its way down to the else and conduct addition:
    Code:
     mtxC[row_size][col_size] = mtxA[row_size][col_size] + mtxB[row_size][col_size];
    and return col_size-1 and other sumMatrix ​it will continues until
    col_size reaches 0 and row_size will deduct 1 so it can proceed to another row in that array.
    until the last where both row_size and col_size becomes 0 it will then return all the values back.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    for(counter ;counter<1; counter++)
    {
        store = col_size;            
    }
    Do you understand that you are always assigning the same value to "store", thus making the for loop rather needless?

    Code:
    return mtxC,mtxA,mtxB,row_size,col_size;
    In C, you can only return a single value from a function. In your case that's "col_size" because you are using the , (comma) operator in your expression.

    Generally I don't think using recursion for adding to matrices is a good idea.

    Bye, Andreas

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    4
    thanks for replying....
    counter was a global variable, its declared outside the function so the program will only go through it once.
    i didnt know that c can only return 1 variable.... thanks for the information.
    even removing the line i believe my algo should be working. can someone help to check whether my algo is correct or nt

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by 花颂 View Post
    even removing the line i believe my algo should be working. can someone help to check whether my algo is correct or nt
    Why don't you run the progam and check its output? You don't show us a complete program, so we can't test it.

    And since I think that using a recursive algorithm for your problem isn't very smart I'm not really motivated to think through it.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading a matrix and printing out the matrix
    By Lina_inverse in forum C Programming
    Replies: 9
    Last Post: 10-23-2012, 04:09 PM
  2. Replies: 6
    Last Post: 05-14-2011, 09:28 AM
  3. adjacency matrix and sparse matrix
    By dpp in forum C Programming
    Replies: 3
    Last Post: 07-11-2010, 10:26 AM
  4. Need help in Matrix Addition & finding Inverse of a Matrix
    By ssatyan.129 in forum C Programming
    Replies: 6
    Last Post: 05-15-2009, 02:48 PM
  5. Matrix: Reloaded + Enter The Matrix (the game)
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-18-2003, 12:35 AM