Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. determinant of matrix in c N*N dimension return process kill .I took from net can anyone help me out where exactly its making this issue and how to overcome this issue.As per my program i pass @dimensional matrix of order x=100;

determinant of matrix in c N*N dimension return process kill .I took from net can anyone help me out where exactly its making this issue and how to overcome this issue.As per my program i pass @dimensional matrix of order x=100;

Scheduled Pinned Locked Moved C / C++ / MFC
helpcssdata-structuresperformancetutorial
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mybm1
    wrote on last edited by
    #1

    Quote:

    float determinant1(float **a,int n)
    {
    int i,j,j1,j2 ; // general loop and matrix subscripts
    float det = 0 ; // init determinant
    float **m = NULL ; // pointer to pointers to implement 2d
    // square array

    if (n < 1)    {   }                // error condition, should never get here
    
    else if (n == 1) {                 // should not get here
        det = a\[0\]\[0\] ;
        }
    
    else if (n == 2)  {                // basic 2X2 sub-matrix determinate
                                       // definition. When n==2, this ends the
        det = a\[0\]\[0\] \* a\[1\]\[1\] - a\[1\]\[0\] \* a\[0\]\[1\] ;// the recursion series
        }
    
    
                                       // recursion continues, solve next sub-matrix
    else {                             // solve the next minor by building a
                                       // sub matrix
        det = 0 ;                      // initialize determinant of sub-matrix
    
                                           // for each column in sub-matrix
        for (j1 = 0 ; j1 < n ; j1++) {
                                           // get space for the pointer list
            m = (float \*\*) malloc((n-1)\* sizeof(float \*)) ;
    
            for (i = 0 ; i < n-1 ; i++)
                m\[i\] = (float \*) malloc((n-1)\* sizeof(float)) ;
                       //     i\[0\]\[1\]\[2\]\[3\]  first malloc
                       //  m -> +  +  +  +   space for 4 pointers
                       //       |  |  |  |          j  second malloc
                       //       |  |  |  +-> \_ \_ \_ \[0\] pointers to
                       //       |  |  +----> \_ \_ \_ \[1\] and memory for
                       //       |  +-------> \_ a \_ \[2\] 4 doubles
                       //       +----------> \_ \_ \_ \[3\]
                       //
                       //                   a\[1\]\[2\]
                      // build sub-matrix with minor elements excluded
            for (i = 1 ; i < n ; i++) {
                j2 = 0 ;               // start at first sum-matrix column position
                                       // loop to copy source matrix less one column
                for (j = 0 ; j < n ; j++) {
                    if (j == j1) continue ; // don't copy the minor column element
    
                    m\[i-1\]\[j2\] = a\[i\]\[j\] ;  // copy source element into new sub-matrix
                                            // i-1
    
    S 1 Reply Last reply
    0
    • M mybm1

      Quote:

      float determinant1(float **a,int n)
      {
      int i,j,j1,j2 ; // general loop and matrix subscripts
      float det = 0 ; // init determinant
      float **m = NULL ; // pointer to pointers to implement 2d
      // square array

      if (n < 1)    {   }                // error condition, should never get here
      
      else if (n == 1) {                 // should not get here
          det = a\[0\]\[0\] ;
          }
      
      else if (n == 2)  {                // basic 2X2 sub-matrix determinate
                                         // definition. When n==2, this ends the
          det = a\[0\]\[0\] \* a\[1\]\[1\] - a\[1\]\[0\] \* a\[0\]\[1\] ;// the recursion series
          }
      
      
                                         // recursion continues, solve next sub-matrix
      else {                             // solve the next minor by building a
                                         // sub matrix
          det = 0 ;                      // initialize determinant of sub-matrix
      
                                             // for each column in sub-matrix
          for (j1 = 0 ; j1 < n ; j1++) {
                                             // get space for the pointer list
              m = (float \*\*) malloc((n-1)\* sizeof(float \*)) ;
      
              for (i = 0 ; i < n-1 ; i++)
                  m\[i\] = (float \*) malloc((n-1)\* sizeof(float)) ;
                         //     i\[0\]\[1\]\[2\]\[3\]  first malloc
                         //  m -> +  +  +  +   space for 4 pointers
                         //       |  |  |  |          j  second malloc
                         //       |  |  |  +-> \_ \_ \_ \[0\] pointers to
                         //       |  |  +----> \_ \_ \_ \[1\] and memory for
                         //       |  +-------> \_ a \_ \[2\] 4 doubles
                         //       +----------> \_ \_ \_ \[3\]
                         //
                         //                   a\[1\]\[2\]
                        // build sub-matrix with minor elements excluded
              for (i = 1 ; i < n ; i++) {
                  j2 = 0 ;               // start at first sum-matrix column position
                                         // loop to copy source matrix less one column
                  for (j = 0 ; j < n ; j++) {
                      if (j == j1) continue ; // don't copy the minor column element
      
                      m\[i-1\]\[j2\] = a\[i\]\[j\] ;  // copy source element into new sub-matrix
                                              // i-1
      
      S Offline
      S Offline
      Stefan_Lang
      wrote on last edited by
      #2

      Check your compiler documentation on how to increase the stacksize. Very likely your program dies due to stack overflow. This tends to happen when you have very deep levels of function calls, and 100 levels of recursion qualifies as very deep! Alternately, test your program with smaller matrices, say 10x10 rather than 100x100. If it works, it's a stack overflow issue. To prevent stack overflow, the only safe method is to avoid recursion, i. e. you should transform your program into one that doesn't require recursion. Otherwise you need to set a maximum recursion level and a set a stacksize that is sufficient to run the program with the maximum defined recursion level; you should exit the program with an error message, if the input would lead to a recursion level exceeding the predefined maximum.

      GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

      M 1 Reply Last reply
      0
      • S Stefan_Lang

        Check your compiler documentation on how to increase the stacksize. Very likely your program dies due to stack overflow. This tends to happen when you have very deep levels of function calls, and 100 levels of recursion qualifies as very deep! Alternately, test your program with smaller matrices, say 10x10 rather than 100x100. If it works, it's a stack overflow issue. To prevent stack overflow, the only safe method is to avoid recursion, i. e. you should transform your program into one that doesn't require recursion. Otherwise you need to set a maximum recursion level and a set a stacksize that is sufficient to run the program with the maximum defined recursion level; you should exit the program with an error message, if the input would lead to a recursion level exceeding the predefined maximum.

        GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

        M Offline
        M Offline
        mybm1
        wrote on last edited by
        #3

        thank you for ur suggestion but can u kindly tell me how to set the maximum recrusion size or else the without recrusion process..

        S 1 Reply Last reply
        0
        • M mybm1

          thank you for ur suggestion but can u kindly tell me how to set the maximum recrusion size or else the without recrusion process..

          S Offline
          S Offline
          Stefan_Lang
          wrote on last edited by
          #4

          What I meant is, check programatically the depth of your recursion. E. g. define a static counter within your recursive function, and always compare it to the maximum allowed value, like this:

          const int max_recursion_level = 20;
          int foo(int n) {
          int result = 0;
          // define static recursion counter
          static int recursion_level = 0;
          // increment at the start of the function, before the first recursive call
          ++recursion_level;
          // safety check
          if (recursion_level > max_recursion_level)
          throw("recursion level exceeded!");

          if (n<=0)
          result = 0;
          else if (n==1)
          result = 1;
          else
          result = foo(n-1) + foo(n-2);

          // decrement counter at the end of the function, after the last recursive call
          --recursion_level;

          return result;
          }

          As for calculating the determinant without recursion, I am sure there are plenty of algorithms on the web if you search for it. I?ve found one in the first response to this question: http://cboard.cprogramming.com/cplusplus-programming/30001-determinant-calculation.html[^]

          GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

          M 1 Reply Last reply
          0
          • S Stefan_Lang

            What I meant is, check programatically the depth of your recursion. E. g. define a static counter within your recursive function, and always compare it to the maximum allowed value, like this:

            const int max_recursion_level = 20;
            int foo(int n) {
            int result = 0;
            // define static recursion counter
            static int recursion_level = 0;
            // increment at the start of the function, before the first recursive call
            ++recursion_level;
            // safety check
            if (recursion_level > max_recursion_level)
            throw("recursion level exceeded!");

            if (n<=0)
            result = 0;
            else if (n==1)
            result = 1;
            else
            result = foo(n-1) + foo(n-2);

            // decrement counter at the end of the function, after the last recursive call
            --recursion_level;

            return result;
            }

            As for calculating the determinant without recursion, I am sure there are plenty of algorithms on the web if you search for it. I?ve found one in the first response to this question: http://cboard.cprogramming.com/cplusplus-programming/30001-determinant-calculation.html[^]

            GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

            M Offline
            M Offline
            mybm1
            wrote on last edited by
            #5

            Actually i am confused now do i need to call int foo() inside the determinant()? if at all i have to set counter can u please help me to implement in my code.

            S 1 Reply Last reply
            0
            • M mybm1

              Actually i am confused now do i need to call int foo() inside the determinant()? if at all i have to set counter can u please help me to implement in my code.

              S Offline
              S Offline
              Stefan_Lang
              wrote on last edited by
              #6

              No, to simplify matters I programmed a recursive function, foo(). It is an implementation of the Fibonacci sequence. The only actual code of that function is the if/else/else-if block. In your case, the determinant function is the recursive function. To check the recursion level, you should do the same as I did for foo(): add a static counter variable that is initialized to 0, define a maximum recursion level somewhere accessible, and maintain that counter when you enter and leave your function. Note that all of my advice is based on the assumption that your problem is caused by a stack overflow. It is an educated guess of mine, but I cannot test it, because I don't know what compiler and what settings you are using. Have you verified this is the cause of your problem? If not, then following my advice may not solve it!

              GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

              M 1 Reply Last reply
              0
              • S Stefan_Lang

                No, to simplify matters I programmed a recursive function, foo(). It is an implementation of the Fibonacci sequence. The only actual code of that function is the if/else/else-if block. In your case, the determinant function is the recursive function. To check the recursion level, you should do the same as I did for foo(): add a static counter variable that is initialized to 0, define a maximum recursion level somewhere accessible, and maintain that counter when you enter and leave your function. Note that all of my advice is based on the assumption that your problem is caused by a stack overflow. It is an educated guess of mine, but I cannot test it, because I don't know what compiler and what settings you are using. Have you verified this is the cause of your problem? If not, then following my advice may not solve it!

                GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                M Offline
                M Offline
                mybm1
                wrote on last edited by
                #7

                So nice of fren i set the recrusion_level in my program and while running its displaying exceed recrusion level as per printf i wrote and in infinte.any suggestion i really need to overcome this.

                S 1 Reply Last reply
                0
                • M mybm1

                  So nice of fren i set the recrusion_level in my program and while running its displaying exceed recrusion level as per printf i wrote and in infinte.any suggestion i really need to overcome this.

                  S Offline
                  S Offline
                  Stefan_Lang
                  wrote on last edited by
                  #8

                  I think you do not understand: maybe you should just forget the recursion counter - it is only a safeguard, and will not help you calculate the determinant! Please check out the link to code I posted above. It does not use recursion and therefore does not need a counter. And it might even be slightly faster.

                  GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups