473,326 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Finding the determinant of a matrix

I am having trouble making this recursive C function work for square matrices of size greater than 3 rows and columns.

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<math.h>
  4. int M[100];
  5. float DET(float A[][100],int dim)
  6. {
  7. int i=100,j,k,l;
  8. static int det=0;
  9. static int DIM=dim;
  10. if(dim>2)
  11.     for(i=0;i<DIM;i++)
  12.     {
  13.     if(i==0&&DIM==dim)for(j=0;j<100;j++)M[j]=100;
  14.     for(j=DIM-1;j>=2;j--)
  15.     if(M[j]==i)continue;
  16.     M[dim-1]=i;
  17.     det+=pow(-1,i+dim-1)*A[dim-1][i]*DET(A,dim-1);
  18.     }
  19.     if(i==DIM-1)M[dim-1]=100;
  20. if(dim==2)
  21.       {
  22.     for(i=0;i<DIM;i++)
  23.     {
  24.     k=0;
  25.       for(j=DIM-1;j>=2;j--)
  26.     if(i!=M[j])k++;
  27.     if(k==DIM-2)break;
  28.     }
  29.     for(j=0;j<DIM;j++)
  30.     {
  31.     k=0;
  32.     for(l=DIM-1;l>=2;l--)
  33.     if(j!=M[l])k++;
  34.     if(k==DIM-2&&j!=i)break;
  35.     }
  36.     return(A[0][i]*A[1][j]-A[0][j]*A[1][i]);
  37.       }
  38. if(dim==1)return(A[0][0]);
  39. return(det);
  40. }
  41.  
  42.  
  43. void main()
  44. {
  45. float A[100][100];
  46. int n,k,l;
  47. puts("Input size of square matrix:");
  48. scanf("%d",&n);
  49. puts("Input square matrix:");
  50. for(k=0;k<n;k++)
  51. {
  52. printf("\n");
  53. for(l=0;l<n;l++)
  54. {
  55. printf("Input a%d%d: ",k+1,l+1);
  56. scanf("%f",&A[k][l]);
  57. }
  58. }
  59. printf("\n\nThe determenant of the matrix is %0.2f",DET(A,n));
  60. getch();
  61. }
  62.  
If anyone can tell me where I went wrong in writing the program I would be grateful.
P.S.:Here are some points to make the program clearer:
_This program works by using method of minor.
_M[100] is an array of integers used to store in decreasing order of its elements which column in the matrix does not belong to the minor associated with the element chosen on a certain row.
_In the function, dim represents wich row is being used, while i(in the first for loop) represents which column is being used.
Aug 15 '08 #1
29 13808
arnaudk
424 256MB
As noted in a recent thread, finding the determinant by a minors expansion is very slow (O(n!) for an nxn matrix), there are much better methods like the finding the LU decomposition which is O(n^3). Also, there are a number of libraries which can do all this for you, such as GSL.
Aug 15 '08 #2
As noted in a recent thread, finding the determinant by a minors expansion is very slow (O(n!) for an nxn matrix), there are much better methods like the finding the LU decomposition which is O(n^3). Also, there are a number of libraries which can do all this for you, such as GSL.
I know there are other methods , but I want to use this one, so please read the program and try it and maybe you'll know what's wrong with it.
Aug 15 '08 #3
arnaudk
424 256MB
What kind of problems are you having with your code? Does it compile? Does it give you the wrong answer?
Aug 15 '08 #4
What kind of problems are you having with your code? Does it compile? Does it give you the wrong answer?
it compiles,but It gives the wrong answer for square matrices of order greater than or equal to 4
Aug 15 '08 #5
arnaudk
424 256MB
Since there is probably an arithmetic error in your code, why not get the function DET to print out the matrix that it is given in a nice tabular form, as well as the cofactor so that you can check that everything is working as expected and you are getting the correct cofactor and associated minor?

Some general suggestions:
  • The return type of main is int, not void.
  • Use more intuitive variable names, if i represents a column, call it col.
  • Use brief comments to explain what's going on if you ever want anyone else to lay eyes on your code.
  • Scrunching up code like if(i==0&&DIM==dim)for(j=0;j<100;j++)M[j]=100; makes it hard to read.
  • Use const wherever you don't expect a variable to be changed, this is to prevent accidentally changing it and to enable some compiler optimizations: float DET(const float A[100][100], const int dim)
  • Using static variables in a recursively called function is arcane. Rather than using static variables, why not pass the variables to each new function call, in this case, pass det and DIM as arguments to the function DET(), the first call to DET will be called with arguments det=0 and DIM=dim.
  • You don't need a special case for dim==2; you can do a cofactor expansion on a 2x2 matrix as well, the minors in this case are just rank 1 matrices (i.e. numbers).
Aug 15 '08 #6
newb16
687 512MB
Why is det int while A is float?
Aug 15 '08 #7
Since there is probably an arithmetic error in your code, why not get the function DET to print out the matrix that it is given in a nice tabular form, as well as the cofactor so that you can check that everything is working as expected and you are getting the correct cofactor and associated minor?

Some general suggestions:
  • The return type of main is int, not void.
  • Use more intuitive variable names, if i represents a column, call it col.
  • Use brief comments to explain what's going on if you ever want anyone else to lay eyes on your code.
  • Scrunching up code like if(i==0&&DIM==dim)for(j=0;j<100;j++)M[j]=100; makes it hard to read.
  • Use const wherever you don't expect a variable to be changed, this is to prevent accidentally changing it and to enable some compiler optimizations: float DET(const float A[100][100], const int dim)
  • Using static variables in a recursively called function is arcane. Rather than using static variables, why not pass the variables to each new function call, in this case, pass det and DIM as arguments to the function DET(), the first call to DET will be called with arguments det=0 and DIM=dim.
  • You don't need a special case for dim==2; you can do a cofactor expansion on a 2x2 matrix as well, the minors in this case are just rank 1 matrices (i.e. numbers).
Thanks for your first advice, I printed all the minors like you said but still can't figure out what's wrong, and sorry if the randomly chosen variable names cuased you a headache, so if there is something unclear to you I will clear it up. Here's the program:
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<math.h>
  4. int M[100];
  5. float DET(float A[][100],int dim)
  6. {
  7. int i=100,j,k,l,m;
  8. static float det=0;
  9. static int DIM=dim;
  10. if(dim>2)
  11.     for(i=0;i<DIM;i++)
  12.     {
  13.     if(i==0&&DIM==dim)for(j=0;j<100;j++)M[j]=100;
  14.     for(j=DIM-1;j>=2;j--)
  15.     if(M[j]==i)continue;
  16.        /*here printing starts*/
  17.                   for(k=0;k<dim;k++)
  18.                   {
  19.                printf("\n");
  20.                   for(j=0;j<DIM;j++)
  21.         {
  22.         m=0;
  23.         for(l=DIM-1;l>=dim;l--)
  24.         if(j!=M[l])m++;
  25.         if(m==DIM-dim)printf("%0.0f   ",A[k][j]);
  26.         }
  27.                            }
  28.                   printf("\n\n");
  29.                 /*here printing ends*/
  30.     M[dim-1]=i;
  31.     det+=pow(-1,i+dim-1)*A[dim-1][i]*DET(A,dim-1);
  32.     }
  33.     if(i==DIM-1)M[dim-1]=100;
  34. if(dim==2)
  35.  {
  36.       /*here printing starts*/
  37.        for(k=0;k<dim;k++)
  38.        {
  39.                     printf("\n");
  40.                        for(j=0;j<DIM;j++)
  41.         {
  42.         m=0;
  43.         for(l=DIM-1;l>=dim;l--)
  44.         if(j!=M[l])m++;
  45.         if(m==DIM-dim)printf("%0.0f   ",A[k][j]);
  46.         }
  47.                }
  48.                printf("\n\n");
  49.       /*here printing ends*/
  50.     for(i=0;i<DIM;i++)
  51.     {
  52.     k=0;
  53.       for(j=DIM-1;j>=2;j--)
  54.     if(i!=M[j])k++;
  55.     if(k==DIM-2)break;
  56.     }
  57.     for(j=0;j<DIM;j++)
  58.     {
  59.     k=0;
  60.     for(l=DIM-1;l>=2;l--)
  61.     if(j!=M[l])k++;
  62.     if(k==DIM-2&&j!=i)break;
  63.     }
  64.     return(A[0][i]*A[1][j]-A[0][j]*A[1][i]);
  65.  }
  66. if(dim==1)return(A[0][0]);
  67. return(det);
  68. }
  69.  
  70.  
  71. void main()
  72. {
  73. float A[100][100];
  74. int n,k,l;
  75. puts("Input size of square matrix:");
  76. scanf("%d",&n);
  77. puts("Input square matrix:");
  78. for(k=0;k<n;k++)
  79. {
  80. printf("\n");
  81. for(l=0;l<n;l++)
  82. {
  83. printf("Input a%d%d: ",k+1,l+1);
  84. scanf("%f",&A[k][l]);
  85. }
  86. }
  87. printf("\n\nThe determenant of the matrix is %0.2f",DET(A,n));
  88. getch();
  89. }
  90.  
Aug 15 '08 #8
arnaudk
424 256MB
So... were the minors displayed correct or not? For example, run your program on this invertible matrix and check the minors and cofactors are correct and have the right sign
Expand|Select|Wrap|Line Numbers
  1. .
  2.    (1 2 3 4)
  3. det(1 2 1 1) = -12
  4.    (1 1 2 1)
  5.    (5 6 7 8)
  6.  
and [code]...[/code] tags are good. Please use them.
Aug 15 '08 #9
JosAH
11,448 Expert 8TB
So... were the minors displayed were correct or not? For example, run your program on this invertible matrix and check the minors and cofactors are correct and have the right sign
Expand|Select|Wrap|Line Numbers
  1. .
  2.    (1 2 3 4)
  3. det(1 2 1 1) = -12
  4.    (1 1 2 1)
  5.    (5 6 7 8)
  6.  
and [code]...[/code] tags are good. Please use them.
Yup correct (using my LUP function) ;-)

kind regards,

Jos
Aug 15 '08 #10
arnaudk
424 256MB
Yup correct (using my LUP function) ;-)
Phew! I usually prefer to work in special units where 1 = -1 = 2 = 1/2. In those units I never have to hunt down minus signs or factors of 2 and my answers are always correct. :-)
Aug 15 '08 #11
JosAH
11,448 Expert 8TB
Phew! I usually prefer to work in special units where 1 = -1 = 2 = 1/2. In those units I never have to hunt down minus signs or factors of 2 and my answers are always correct. :-)
Well, my LUP implementation isn't the most efficient one when it comes to pivot
selection but then again it has to deal with symbolics as well and it does a fine
job when it comes to that e.g.

Expand|Select|Wrap|Line Numbers
  1. | a b |
  2. | c d | = a*d-b*c
  3.  
It can do any larger dimensions as well ;-)

kind regards,

Jos
Aug 15 '08 #12
So... were the minors displayed correct or not? For example, run your program on this invertible matrix and check the minors and cofactors are correct and have the right sign
Expand|Select|Wrap|Line Numbers
  1. .
  2.    (1 2 3 4)
  3. det(1 2 1 1) = -12
  4.    (1 1 2 1)
  5.    (5 6 7 8)
  6.  
and [code]...[/code] tags are good. Please use them.

OK, now I've perfected the program, and all the minors are displayed correctly, but It still gives the wrong answer, and I'm still trying to find out what's wrong.
here's the program, test it on C++(V5.02) if you have it:
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<math.h>
  4. int M[100];
  5. float DET(float A[][100],int dim)
  6. {
  7. int i,j,k,l,m;
  8. static float det=0;
  9. static int DIM=dim;
  10. if(dim>2)
  11.     for(i=0;i<DIM;i++)
  12.     {
  13.     if(i==0&&DIM==dim)for(j=0;j<100;j++)M[j]=100;
  14.     m=0;
  15.     for(j=DIM-1;j>=2;j--)
  16.     if(M[j]==i)m++;
  17.     if(m!=0)continue;
  18.        /*here printing starts*/
  19.        for(k=0;k<dim;k++)
  20.        {
  21.     printf("\n");
  22.        for(j=0;j<DIM;j++)
  23.         {
  24.         m=0;
  25.         for(l=DIM-1;l>=2;l--)
  26.         if(j!=M[l])m++;
  27.         if(m==DIM-2)printf("%0.0f   ",A[k][j]);
  28.         }
  29.     }
  30.        printf("\n\n");
  31.     /*here printing ends*/
  32.     M[dim-1]=i;
  33.     det+=pow(-1,i+dim-1)*A[dim-1][i]*DET(A,dim-1);
  34.     M[dim-1]=100;
  35.     }
  36. if(dim==2)
  37.  {
  38.     /*here printing starts*/
  39.        for(k=0;k<dim;k++)
  40.        {
  41.     printf("\n");
  42.            for(j=0;j<DIM;j++)
  43.         {
  44.         m=0;
  45.         for(l=DIM-1;l>=2;l--)
  46.         if(j!=M[l])m++;
  47.         if(m==DIM-2)printf("%0.0f   ",A[k][j]);
  48.         }
  49.     }
  50.        printf("\n\n");
  51.     /*here printing ends*/
  52.     for(i=0;i<DIM;i++)
  53.     {
  54.     k=0;
  55.       for(j=DIM-1;j>=2;j--)
  56.     if(i!=M[j])k++;
  57.     if(k==DIM-2)break;
  58.     }
  59.     for(j=0;j<DIM;j++)
  60.     {
  61.     k=0;
  62.     for(l=DIM-1;l>=2;l--)
  63.     if(j!=M[l])k++;
  64.     if(k==DIM-2&&j!=i)break;
  65.     }
  66.     return(A[0][i]*A[1][j]-A[0][j]*A[1][i]);
  67.  }
  68. if(dim==1)return(A[0][0]);
  69. return(det);
  70. }
  71.  
  72.  
  73. void main()
  74. {
  75. float A[100][100];
  76. int n,k,l;
  77. puts("Input size of square matrix:");
  78. scanf("%d",&n);
  79. puts("Input square matrix:");
  80. for(k=0;k<n;k++)
  81. {
  82. printf("\n");
  83. for(l=0;l<n;l++)
  84. {
  85. printf("Input a%d%d: ",k+1,l+1);
  86. scanf("%f",&A[k][l]);
  87. }
  88. }
  89. printf("\n\nThe determenant of the matrix is %0.2f",DET(A,n));
  90. getch();
  91. }
  92.  
I just have one question:
When I write
det+=pow(-1,i+dim-1)*A[dim-1][i]*DET(A,dim-1); where the return value of the DET(A,dim-1) function is det, is the value added to det just (sign)*A[dim-1][i] multiplied by the determinant of its minor?
P.S: I tried your matrix, and got -8550.
Aug 17 '08 #13
JosAH
11,448 Expert 8TB
When I write
det+=pow(-1,i+dim-1)*A[dim-1][i]*DET(A,dim-1); where the return value of the DET(A,dim-1) function is det, is the value added to det just (sign)*A[dim-1][i] multiplied by the determinant of its minor?
When you write X+= Y then the entire expression Y is evaluated and its value
is added to X for whatever the values of X and Y here the new value of X is
Y larger than it was before.

kind regards,

Jos

ps. you definitely should use other variable names and use a proper indentation
and spaces to make your code more readable.
Aug 17 '08 #14
When you write X+= Y then the entire expression Y is evaluated and its value
is added to X for whatever the values of X and Y here the new value of X is
Y larger than it was before.

kind regards,

Jos

ps. you definitely should use other variable names and use a proper indentation
and spaces to make your code more readable.
You didn't understand what I meant.
When I add to the value of the det static variable a return value which is that very same static variable(which is happening in the function as long as dim-1 is not equal to 2), will it add itself twice? Or will it just add the return value associated with the dim-1 part of the recursivity to the initial value of det in the part of the recursivity where I made the statement det+=pow(-1,i+dim-1)*A[dim-1][i]*DET(A,dim-1); ?
Aug 17 '08 #15
JosAH
11,448 Expert 8TB
You didn't understand what I meant.
When I add to the value of the det static variable a return value which is that very same static variable(which is happening in the function as long as dim-1 is not equal to 2), will it add itself twice? Or will it just add the return value associated with the dim-1 part of the recursivity to the initial value of det in the part of the recursivity where I made the statement det+=pow(-1,i+dim-1)*A[dim-1][i]*DET(A,dim-1); ?
If the DET method in that last assignment expression also changes static variable
det then the entire thing causes undefined behaviour; you are changing the value
of an LHV (here: det) more than once before a sequence point is reached.
Anything can happen here.

kind regards,

Jos
Aug 17 '08 #16
arnaudk
424 256MB
curiously enough, you're really making your life difficult by insisting on having those static variables. :-) Why don't you take heed of the comments in post #6?
Aug 17 '08 #17
curiously enough, you're really making your life difficult by insisting on having those static variables. :-) Why don't you take heed of the comments in post #6?
I'm writing this so you don't find the program arcane anymore,PLEASE try to read it, cause it still gives the wrong answer for DIM greater than 3
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<math.h>
  4. int M[100];
  5. float DET(float A[][100],int dim,int DIM,float det)
  6. {
  7. int i=100,j,k,l;
  8. if(dim>2)
  9.     for(i=0;i<DIM;i++)
  10.     {
  11.     if(i==0&&DIM==dim)
  12.     for(j=0;j<100;j++)M[j]=100;
  13.     for(j=DIM-1;j>=2;j--)
  14.     if(M[j]==i)continue;
  15.     M[dim-1]=i;
  16.     det+=pow(-1,i+dim-1)*A[dim-1][i]*DET(A,dim-1,DIM,det);
  17.     }
  18.     if(i==DIM-1)M[dim-1]=100;
  19. if(dim==2)
  20.       {
  21.     for(i=0;i<DIM;i++)
  22.     {
  23.     k=0;
  24.     for(j=DIM-1;j>=2;j--)
  25.     if(i!=M[j])k++;
  26.     if(k==DIM-2)break;
  27.     }
  28.     for(j=0;j<DIM;j++)
  29.     {
  30.     k=0;
  31.     for(l=DIM-1;l>=2;l--)
  32.     if(j!=M[l])k++;
  33.     if(k==DIM-2&&j!=i)break;
  34.     }
  35.     return(A[0][i]*A[1][j]-A[0][j]*A[1][i]);
  36.       }
  37. if(dim==1)return(A[0][0]);
  38. return(det);
  39. }
  40.  
  41.  
  42. void main()
  43. {
  44. float A[100][100];
  45. int n,k,l;
  46. puts("Input size of square matrix:");
  47. scanf("%d",&n);
  48. puts("Input square matrix:");
  49. for(k=0;k<n;k++)
  50. {
  51. printf("\n");
  52. for(l=0;l<n;l++)
  53. {
  54. printf("Input a%d%d: ",k+1,l+1);
  55. scanf("%f",&A[k][l]);
  56. }
  57. }
  58. printf("\n\nThe determenant of the matrix is %0.2f",DET(A,n,n,0));
  59. getch();
  60. }
  61.  
Aug 18 '08 #18
newb16
687 512MB
Expand|Select|Wrap|Line Numbers
  1.     if(i==0&&DIM==dim)
  2.     for(j=0;j<100;j++)M[j]=100;
  3.     for(j=DIM-1;j>=2;j--)
  4.     if(M[j]==i)continue;
  5.     M[dim-1]=i;
  6.  
This is still arcane.
Expand|Select|Wrap|Line Numbers
  1.     if(i==0&&DIM==dim)
  2.        for(j=0;j<100;j++)
  3.            M[j]=100;
  4.     for(j=DIM-1;j>=2;j--)
  5.         if(M[j]==i)continue;
  6.     M[dim-1]=i;
  7.  
This is less so.
Aug 18 '08 #19
arnaudk
424 256MB
No one here is going to debug your code for you, you'll have to do that yourself. You can get advice here on specific problems which is pretty good for free.

You don't need a special case for dim==2. That you continue to pollute your code with this non-special case seems to indicate that you don't fully understand how do do a minor expansion.
I still find your code is too compact and variable names abstruse, and the fact you can't get it to work proves you can't see what's going on either. Code should be written for human beings, not just for computers.
If your minors and cofactors are correct, then you should have gotten the correct answer. Therefore, I don't believe they are correct.
The minors expansion for the aforementioned matrix looks like this:
Expand|Select|Wrap|Line Numbers
  1. |1 2 3 4|   
  2. |1 2 1 1|     |2 1 1|     |1 1 1|     |1 2 1|    |1 2 1|
  3. |1 1 2 1| = 1 |1 2 1| - 2 |1 2 1| + 3 |1 1 1| - 4|1 1 2|
  4. |5 6 7 8|     |6 7 8|     |5 7 8|     |5 6 8|    |5 6 7|
  5.  
Do you get these minors, with the columns in the right order and the correct cofactors and signs? Are you sure you then get the correct minors for the 3x3 matrices? Remember that swapping columns introduces minus signs.
Aug 18 '08 #20
No one here is going to debug your code for you, you'll have to do that yourself. You can get advice here on specific problems which is pretty good for free.

You don't need a special case for dim==2. That you continue to pollute your code with this non-special case seems to indicate that you don't fully understand how do do a minor expansion.
I still find your code is too compact and variable names abstruse, and the fact you can't get it to work proves you can't see what's going on either. Code should be written for human beings, not just for computers.
If your minors and cofactors are correct, then you should have gotten the correct answer. Therefore, I don't believe they are correct.
The minors expansion for the aforementioned matrix looks like this:
Expand|Select|Wrap|Line Numbers
  1. |1 2 3 4|   
  2. |1 2 1 1|     |2 1 1|     |1 1 1|     |1 2 1|    |1 2 1|
  3. |1 1 2 1| = 1 |1 2 1| - 2 |1 2 1| + 3 |1 1 1| - 4|1 1 2|
  4. |5 6 7 8|     |6 7 8|     |5 7 8|     |5 6 8|    |5 6 7|
  5.  
Do you get these minors, with the columns in the right order and the correct cofactors and signs? Are you sure you then get the correct minors for the 3x3 matrices? Remember that swapping columns introduces minus signs.
I've run the program countless times and the minors were always perfecty correct, but the answer remained incorrect for matrices of order 4 or greater(for 3x3 matrices they are correct), and there is no column swapping in the program.
Anyway, I've simplified and explained the program here as much as possible for you, so if you can understand it finally, then tell me the problem.(by the way, this gives the same answers as before)
Here I explain with order of the code from up to down:
1.The skip_col integer array is used to know which columns are to be skipped when finding the minor matrix
2.The parameters of this function are the square matrix A, the number of rows which is changed by the recursivity, and the SIZE of the matrix which is a constant(which in this case is both the number of rows and columns since it is a square matrix)
3.det is the return value of the function
4.As you may have noticed, the funtion DET is called recursively from inside a for loop
5.this recursive funtion moves from the last row to the first row,and the for loop(the first one) is from the first column to the last column
6.everytime the function is called inside the for loop, the for loop is passed again to find all the minors of the minor of the pivot which was called which would be A[row-1][col]
7.the step if(col==0&&row==size) is just to set the initial values of the array skip_col and the value det, it is only passed once in the entire program
8.skip_col is filled with 100's completely instead of zero's because unike 0, 100 is very unlikely to be found as a column's number
9.and det is assigned an initial value of zero
10.then, using a for loop I check if the column col is one of the columns that belong to the minor associated with the previous elements that were used in calling in the previous steps of the recursivity
11.using the integer test, which is always set to zero before this for loop, I test if this is one of the column numbers that are stored in skip_col
12.if test shows that this column does belong to skip_col, then continue is used to skip the remaining statements and check the next column col while on the same row
13.if col here is the right column, then first it is stored in skip_col. Notice that the elements of skip_col are stored in decreasing order(from row-1 to 1, skip_col[0] is never used and its value will always stay 100)
14.Then I add to the value of det the element multiplied by its minor
15.everytime DET(A,row-1,SIZE) recieves a return value the column associated with row-1(which is stored in skip_col) is set back to 100
16.in if(row==1) is the return value after which recursivity goes backward, and this is the first row of the matrix
17.then same test as before
18.if test shows that col is the column that belongs to all the minors col is taken
19.the value is returned to the phase where row was equal to 2
20.det is returned everytime the last column is passed in the first for loop and it is the final return value
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<math.h>
  4. int skip_col[100];
  5. float DET(float A[][100],int row,int SIZE)
  6. {
  7. int col,i,test;
  8. float det;
  9.  
  10. if(row>1)
  11. for(col=0;col<SIZE;col++)
  12. {
  13.    if(col==0&&row==SIZE)
  14.    {
  15.    for(i=0;i<100;i++)
  16.        skip_col[i]=100;
  17.    det=0;
  18.    }
  19.    test=0;
  20.     for(i=SIZE-1;i>1;i--)
  21.            if(col==skip_col[i])test++;
  22.        if(test!=0)continue;
  23.     skip_col[row-1]=col;
  24.     det+=pow(-1,(row-1)+col)*A[row-1][col]*DET(A,row-1,SIZE);
  25.    skip_col[row-1]=100;
  26. }
  27.  
  28. if(row==1)
  29. {
  30.    for(col=0;col<SIZE;col++)
  31.    {
  32.    test=0;
  33.    for(i=SIZE-1;i>1;i--)
  34.            if(col==skip_col[i])test++;
  35.        if(test==0)break;
  36.    }
  37.     return(A[0][col]);
  38. }
  39.  
  40. return(det);
  41. }
  42.  
  43.  
  44. void main()
  45. {
  46. float A[100][100];
  47. int n,k,l;
  48. puts("Input size of square matrix:");
  49. scanf("%d",&n);
  50. puts("Input square matrix:");
  51. for(k=0;k<n;k++)
  52. {
  53. printf("\n");
  54. for(l=0;l<n;l++)
  55. {
  56. printf("Input a%d%d: ",k+1,l+1);
  57. scanf("%f",&A[k][l]);
  58. }
  59. }
  60. printf("\n\nThe determenant of the matrix is %0.2f",DET(A,n,n));
  61. getch();
  62. }
  63.  
Aug 18 '08 #21
Experts my ass, I knew I should have never posted here in the first place.
Aug 20 '08 #22
JosAH
11,448 Expert 8TB
Experts my ass, I knew I should have never posted here in the first place.
Ok, goodbye and good luck with your code (a partial LUP decomposition would
have been much faster and easier to implement).

kind regards,

Jos
Aug 20 '08 #23
newb16
687 512MB
Experts my ass, I knew I should have never posted here in the first place.
No one is going to debug somebody else's trivial but complex code debugging of which ( by definition ) does not reward in any way ( e.g. 'I showed off my knowlegde of language constructions! I'm cool" or "This is because this algorithm that I know works not like implemented here" etc )
Aug 20 '08 #24
No one is going to debug somebody else's trivial but complex code debugging of which ( by definition ) does not reward in any way ( e.g. 'I showed off my knowlegde of language constructions! I'm cool" or "This is because this algorithm that I know works not like implemented here" etc )
I'm not asking anyone to debug my code, post #21 made the algorithm as clear as possible, anyone can understand just by reading it, but obviously none bothered.
Aug 20 '08 #25
JosAH
11,448 Expert 8TB
I'm not asking anyone to debug my code, post #21 made the algorithm as clear as possible, anyone can understand just by reading it, but obviously none bothered.
I can't speak for others but at least I didn't bother to dig through that code and
you can't force anyone to feel bothered; that code is just highly inefficient even
if it does run properly.

kind regards,

Jos
Aug 20 '08 #26
I SOLVED IT !!!!!!!!!!!!!!!!!!!!!!
IT IS 100% CORRECT!
HERE IT IS ''EXPERTS'':

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<math.h>
  4. int skip_col[100];
  5. float DET(float A[][100],int row,int SIZE)
  6. {
  7. int col,i,test;
  8. float det=0;
  9. int minor_col=0;
  10.  
  11. if(row>1)
  12. for(col=0;col<SIZE;col++)
  13. {
  14.    if(col==0&&row==SIZE)
  15.    {
  16.    for(i=0;i<100;i++)
  17.        skip_col[i]=100;
  18.    }
  19.    test=0;
  20.     for(i=SIZE-1;i>1;i--)
  21.            if(col==skip_col[i])test++;
  22.        if(test!=0)continue;
  23.     skip_col[row-1]=col;
  24.     det+=pow(-1,(row-1)+minor_col)*A[row-1][col]*DET(A,row-1,SIZE);
  25.    minor_col++;
  26.    skip_col[row-1]=100;
  27. }
  28.  
  29. if(row==1)
  30. {
  31.     for(col=0;col<SIZE;col++)
  32.    {
  33.    test=0;
  34.    for(i=SIZE-1;i>=1;i--)
  35.            if(col==skip_col[i])test++;
  36.        if(test==0)break;
  37.    }
  38.     return(A[row-1][col]);
  39. }
  40.  
  41. return(det);
  42. }
  43.  
  44.  
  45. void main()
  46. {
  47. float A[100][100];
  48. int n,k,l;
  49. puts("Input size of square matrix:");
  50. scanf("%d",&n);
  51. puts("Input square matrix:");
  52. for(k=0;k<n;k++)
  53. {
  54. printf("\n");
  55. for(l=0;l<n;l++)
  56. {
  57. printf("Input a%d%d: ",k+1,l+1);
  58. scanf("%f",&A[k][l]);
  59. }
  60. }
  61. printf("\n\nThe determenant of the matrix is %0.2f",DET(A,n,n));
  62. getch();
  63. }
  64.  
READ IT AND MAYBE YOU'LL LEARN SOMETHING.
Aug 23 '08 #27
JosAH
11,448 Expert 8TB
READ IT AND MAYBE YOU'LL LEARN SOMETHING.
Did *you* learn anything?

kind regards,

Jos (moderator)
Aug 23 '08 #28
newb16
687 512MB
I SOLVED IT !!!!!!!!!!!!!!!!!!!!!!
IT IS 100% CORRECT!
HERE IT IS ''EXPERTS'':
Experts are not those who rush to solve trivial tasks for free.

READ IT AND MAYBE YOU'LL LEARN SOMETHING.
I learned that a human equipped with teh brain is able to write and debug teh codez on his own.
p.s. And as you wrote that minors are calculated corectly but result is wrong - the bug was exactly in calculating final sum of products of minors and coefficients.
Aug 23 '08 #29
sicarie
4,677 Expert Mod 4TB
I SOLVED IT !!!!!!!!!!!!!!!!!!!!!!
IT IS 100% CORRECT!
HERE IT IS ''EXPERTS'':
Congratulations, and good effort.

I'd recommend three things, in the future, though:

1) use standard libraries as much as possible.

Obviously, at some point this will be impossible if you get a job as a programmer (outside of FOSS development), and you will use libraries on custom apps, but it's good practice for figuring out what is in those custom libraries to start now - looking through the libraries you have available to achieve the result you want.

On this note, I challenge you to remove conio.h from your program. I only glanced through your code, but it looks like there is only one reason you have it in there, and that functionality is easily duplicated. Conio itself is non-standard whose implementation varies by version, and can cause issues if you were to distribute a package using a different version than is what the end users have - so it's good to get in the habit of not using things that you can replicate easily without.

2) Use a standard indentation pattern

This isn't Python where indentation affects execution (or compilation), but picking one of the more standard indentations (three spaces or a tab) will make your code much, much more readable

3) Comments

Commenting code is another way to increase readability and reuse - you can easily look at the description of a module or loop, its inputs and outputs, and either modify it or use it in another program (though if it's really that useful, you can make it into its own module that can be included...
Aug 23 '08 #30

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: aruna | last post by:
i want a c program to find the determinant of matrix and inverse of matrix as two separate c programs . please help me
1
by: naren | last post by:
can anyone help with the code for determinant,cofactor and inverse of a matrix in c++ lang? i am not getting the logic.....
1
by: avdhoot | last post by:
Hi Everybody, I am doing some project in VB6 where I ahve to calculate the Determinant of the matrix. I have got a function MDETERM but not able to pass on the parameters of the matrix and get the...
14
by: James Stroud | last post by:
Hello All, I'm using numpy to calculate determinants of matrices that look like this (13x13):
4
by: krishnai888 | last post by:
I had already asked this question long back but no one has replied to me..I hope someone replies to me because its very important for me as I am doing my internship. I am currently writing a code...
2
by: pvsgangadhar | last post by:
I need the program in c-langauge to find the determinant of a matrix for specified no.or rows and columns.
1
by: haderika | last post by:
Hey, I'm having trouble overloading the ~ operator (the determinant of the matrix) in a case of 2x2 matrices. So i have a matrix class, with the constructor, overloading +, += and ~ operators. the...
1
by: Glenton | last post by:
Hi All Here is a very simple little class for finding a shortest route on a network, following Dijkstra's Algorithm: #!/usr/bin/env python #This is meant to solve a maze with Dijkstra's...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.