473,382 Members | 1,576 Online

# 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 13814
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