Thread: matrix program issue, recursion

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    1

    matrix program issue, recursion

    hi, i'm working on a program that allows you to enter a matrix, and then do all kinds of stuff to it. i'm having problems with my determinant function, though. I want to use recursion so that if doesn't matter what size matrix is entered. there is a seperate function that gets a sub-matrix for me, but the recursion is killing me.

    Code:
    double determinant(double m [][MAXCOLS], int msize)
    {
    
        double det = 0;
        double subm [MAXCOLS][MAXCOLS];
        int i;
        int sign = 0;
    
        if(msize < 3)
    	det = m[0][0] * m[1][1] - m[0][1] * m[1][0];
        else
        {
    	for(i = 0; i < msize; i++)
    	{
    	    if(i%2 == 0)
    		sign = 1;
    	    else
    	    {
    		sign = -1;
    	    }
    
    	get_submatrix(m, subm, msize, 0, i);
    	det += (sign * m[0][i] * determinant(subm, msize-i));
    	}
        }
    
        return det;
    }
    that's my determinant function as of right now. it gives me an infinite loop. the arguments of my submatrix function are:
    arg1, double array [MAXCOLS][MAXCOLS]
    arg2, double array [MAXCOLS][MAXCOLS]
    arg3, int (the size of the square matrix)
    arg4, int (row to be removed)
    arg5, int (column to be removed)

    if you need more info, just let me know, and i appreciate any help that i can get!

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    38
    Are you sure it's an infinite loop? Then check if your submatrix is really decreasing in size. Take a 3-3 test matrix and print lot's of stuff to see if it works ok.

    For larger matrices, your method will seem to run eternally. Better form a LU-decomposition and extract the determinant from U's diagonal.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program - inverse of a matrix
    By chaugh in forum C Programming
    Replies: 4
    Last Post: 01-18-2010, 11:00 PM
  2. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  3. Help on C program, recursion
    By joeblack437 in forum C Programming
    Replies: 8
    Last Post: 11-07-2008, 03:41 PM
  4. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  5. Very handy matrix functions - part 1
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 05-20-2004, 10:38 AM