Does anybody know how to write a general formula in c that calculates the determinant of a matrix of any size. <br>Or even one for 2x2 or 3x3 to start with.<br>thanks very much.<br>dave
Hey Buddy,<br>This revives old memories of college days. Well I dont remeber the exact formula but I do remeber how I had implemeted it. The trick lies in remebering that one need not worry about adding or deleteing if we consider the matrix to be circular i.e use a lotta mods.<br>I have written a program which I have tested for order 3 and I believe it will work fine for higher order matrices too.<br>------------------------------------------------------<br>/* Increase the value of N for higer order matrix */<br>#define N 3<br>int a[N][N];<br><br>main() {<br> int i,j;<br> /* Fill the values for the matrix */<br> for (i=0;i<N;i++)<br> for(j=0;j<N;j++)<br> a<i>[j]=1;<br> /* Play around with the values for testing purposes */<br> a[1][0]=2;<br> printf("determinant is %d",det(N,N));<br>}<br><br>det(int start,int size)<br>{<br> int i,deter=0;<br> for(i=N-start;i<size;i++) {<br> if (size == 3)<br> deter+= a[(N-size)%N]<i> * det2((N-size+1)%N,(i+1)%N);<br> else<br> deter+= det((start+1)%N,size-1);<br> printf("temp value is %d\n",deter);<br> }<br> return(deter);<br>}<br><br>det2(int starti,int startj) {<br> int endi,endj;<br> endi = (starti+1) %N;<br> endj = (startj+1) %N;<br> return(a[starti][startj]*a[endi][endj] -<br> a[starti][endj]*a[startj][endi]);<br>}<br>-------------------------------------------------------<br>Adios<br>amit <p> amit<br><a href=mailto: crazy_indian@mailcity.com> crazy_indian@mailcity.com</a><br><a href= > </a><br>to bug is human to debug devine
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.