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 error at //answer[r][s]=input[p][q];

determinant of matrix in c N*N dimension return error at //answer[r][s]=input[p][q];

Scheduled Pinned Locked Moved C / C++ / MFC
performancehelp
10 Posts 3 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

    float determinant(float **input,int order)
    {

    int j,p,q,t,i;
    float pr;
    float d;
    float *c;
    //c=memory_alloc_2D1(row,column);
    float **answer;
    answer=memory_alloc_2D1(order,order);
    float **b;
    b=memory_alloc_2D1(order,order);

    for(j=1;j<=order;j++)
    {
    int r=1,s=1;
    for(p=1;p<=order;p++)
    {

           for(q=1;q<=order;q++)
           		 {
            		        if(p!=1&&q!=j)
              				{
    			
    	answer\[r\]\[s\]=input\[p\]\[q\]; // error as segmentation fault
                				s++;
                				if(s>order-1)
                 				{
                   					r++;
                   					s=1;
                  				}   
              				 }
            		 }
        }
    
     	for(t=1,pr=1;t<=(1+j);t++)
     		pr=(-1)\*pr;
     		c\[j\]=pr\*determinant(b,order-1);
     }
     		for(j=1,d=0.0;j<=order;j++)
     		{
      			 d=d+(input\[1\]\[j\]\*c\[j\]);
     		}
    

    return(d);

    }

    main.c

    float determinantmatrix;
    determinantmatrix=determinant(covariance1,waveframesize);

    //covariance1 is the input matrix

    J D 2 Replies Last reply
    0
    • M mybm1

      float determinant(float **input,int order)
      {

      int j,p,q,t,i;
      float pr;
      float d;
      float *c;
      //c=memory_alloc_2D1(row,column);
      float **answer;
      answer=memory_alloc_2D1(order,order);
      float **b;
      b=memory_alloc_2D1(order,order);

      for(j=1;j<=order;j++)
      {
      int r=1,s=1;
      for(p=1;p<=order;p++)
      {

             for(q=1;q<=order;q++)
             		 {
              		        if(p!=1&&q!=j)
                				{
      			
      	answer\[r\]\[s\]=input\[p\]\[q\]; // error as segmentation fault
                  				s++;
                  				if(s>order-1)
                   				{
                     					r++;
                     					s=1;
                    				}   
                				 }
              		 }
          }
      
       	for(t=1,pr=1;t<=(1+j);t++)
       		pr=(-1)\*pr;
       		c\[j\]=pr\*determinant(b,order-1);
       }
       		for(j=1,d=0.0;j<=order;j++)
       		{
        			 d=d+(input\[1\]\[j\]\*c\[j\]);
       		}
      

      return(d);

      }

      main.c

      float determinantmatrix;
      determinantmatrix=determinant(covariance1,waveframesize);

      //covariance1 is the input matrix

      J Offline
      J Offline
      jeron1
      wrote on last edited by
      #2

      I'll guess that you are attempting to access past the end of an array. code like this,

      for(j=1;j<=order;j++)

      would normally look like this.

      for(j=0;j<order;j++)

      as array access is zero based, from 0 to (size - 1)

      "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst

      M 1 Reply Last reply
      0
      • J jeron1

        I'll guess that you are attempting to access past the end of an array. code like this,

        for(j=1;j<=order;j++)

        would normally look like this.

        for(j=0;j<order;j++)

        as array access is zero based, from 0 to (size - 1)

        "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst

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

        it compile finely but while running segmentation fault occur and couldnt printf the output.

        J 1 Reply Last reply
        0
        • M mybm1

          it compile finely but while running segmentation fault occur and couldnt printf the output.

          J Offline
          J Offline
          jeron1
          wrote on last edited by
          #4

          That issue (indexing past the end of an array) isn't a syntactic issue and won't be flagged by the compiler, it is a logic issue and you will only see a problem at run time.

          "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst

          M 1 Reply Last reply
          0
          • J jeron1

            That issue (indexing past the end of an array) isn't a syntactic issue and won't be flagged by the compiler, it is a logic issue and you will only see a problem at run time.

            "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst

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

            but i cant find out where the logic is getting wrong can u help me out.

            J D 2 Replies Last reply
            0
            • M mybm1

              but i cant find out where the logic is getting wrong can u help me out.

              J Offline
              J Offline
              jeron1
              wrote on last edited by
              #6

              Do you understand what I said about array access?

              "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst

              M 1 Reply Last reply
              0
              • M mybm1

                float determinant(float **input,int order)
                {

                int j,p,q,t,i;
                float pr;
                float d;
                float *c;
                //c=memory_alloc_2D1(row,column);
                float **answer;
                answer=memory_alloc_2D1(order,order);
                float **b;
                b=memory_alloc_2D1(order,order);

                for(j=1;j<=order;j++)
                {
                int r=1,s=1;
                for(p=1;p<=order;p++)
                {

                       for(q=1;q<=order;q++)
                       		 {
                        		        if(p!=1&&q!=j)
                          				{
                			
                	answer\[r\]\[s\]=input\[p\]\[q\]; // error as segmentation fault
                            				s++;
                            				if(s>order-1)
                             				{
                               					r++;
                               					s=1;
                              				}   
                          				 }
                        		 }
                    }
                
                 	for(t=1,pr=1;t<=(1+j);t++)
                 		pr=(-1)\*pr;
                 		c\[j\]=pr\*determinant(b,order-1);
                 }
                 		for(j=1,d=0.0;j<=order;j++)
                 		{
                  			 d=d+(input\[1\]\[j\]\*c\[j\]);
                 		}
                

                return(d);

                }

                main.c

                float determinantmatrix;
                determinantmatrix=determinant(covariance1,waveframesize);

                //covariance1 is the input matrix

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                maibam debina wrote:

                //covariance1 is the input matrix

                But you've not shown how it's defined. If you have something like:

                float covariance1[5][8];

                And you are trying to access it in determinant() like:

                for (int p = 1; p <= 5; p++)
                covariance1[p][q]...

                You can expect an access violation.

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                M 1 Reply Last reply
                0
                • M mybm1

                  but i cant find out where the logic is getting wrong can u help me out.

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  The for() loops are wrong.

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                  1 Reply Last reply
                  0
                  • J jeron1

                    Do you understand what I said about array access?

                    "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst

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

                    even though i change it, its not working..

                    1 Reply Last reply
                    0
                    • D David Crow

                      maibam debina wrote:

                      //covariance1 is the input matrix

                      But you've not shown how it's defined. If you have something like:

                      float covariance1[5][8];

                      And you are trying to access it in determinant() like:

                      for (int p = 1; p <= 5; p++)
                      covariance1[p][q]...

                      You can expect an access violation.

                      "One man's wage rise is another man's price increase." - Harold Wilson

                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

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

                      I cant get u properly can u please ellaborate...

                      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