# Matrices

• 05-13-2008
YongzZ
Matrices
Hi,

I would like to find a determinant of a 2*2 matrix.
But there seems to be an error while it is running.

Code:

s=det2(&det,&m1,2,3);
printf("Value of determinant was..%d\n",det);

int det2(int *det,matrix *m1,int row,int col){
int A,B,C,D;
row=2;
col=3;
A=(*m1).arr[row][col];
B=(*m1).arr[row+1][col];
C=(*m1).arr[row][col+1];
D=(*m1).arr[row+1][col+1];
*det=(A*D)-(B*C);
return(0);
}

• 05-13-2008
laserlight
You might want to post the smallest and simplest compilable program that demonstrates the problem.
• 05-13-2008
abh!shek
Where's main()?
Post the complete source code.
• 05-13-2008
YongzZ
here is the code.
getMatrix is another function used to obtain matrix from keyboard.

Code:

int det2(int *val,matrix *m,int row,int col);
main(){
matrix m1, m2;
int s,det;
s=getMatrix(&m1);
s=det2(&det,&m1,2,3);
printf("Value of determinant was..%d\n",det);

int det2(int *det,matrix *m1,int row,int col){
int A,B,C,D;
row=2;
col=3;
A=(*m1).arr[row][col];
B=(*m1).arr[row+1][col];
C=(*m1).arr[row][col+1];
D=(*m1).arr[row+1][col+1];
*det=(A*D)-(B*C);
return(0);
}

• 05-13-2008
zacs7
I assume the matrix is square since that's the algorithm for equating the determinant of a 2x2 square matrix.
However, if it's square,

Code:

...

row=2;
col=3;
A=(*m1).arr[row][col];
B=(*m1).arr[row+1][col];

...

column 3 won't exist, a square matrix is 2 x 2.
Also you're probably indexing the array columns & rows from 1 on paper, but in C arrays are indexed from 0.

Also,
Code:

A = (*m1).arr[row][col];
Can be written as m1->arr[row][col];

Consider the following:

Code:

A =| a  b |
| c  d |

The det(A) = A[0][0].A[1][1] - A[0][1].A[1][0]

Where, A[row][column] (0-based).
• 05-13-2008
Dino
If it's a 2X2 matrix, row 2 or col 2 won't exist either. The indices will be only 0 and 1.

Perhaps you have a larger matrix and are only picking a 2X2 square out of it?

Todd
• 05-13-2008
YongzZ
Firstly thanks.

And yes, I have a larger matrix and I'm extracting the 2 by 2.

Thats why i picked row[2]col[3], which is the first number then the next number of the matrix should be [2+1][3] right?
• 05-13-2008
Dino
"next number" for what?

[2+1][3] will be the number below (as we humans view a matrix) [2][3].

BTW, are you processing this matrix via calculating the cofactors, or is this some other application?
• 05-13-2008
YongzZ
For example,

1 2
3 4

I meant if 1 is [2][3] when 2 will be [3][3] and 3 will be [2][4].

No, im am merely taking the matrices and applying the formula.
• 05-13-2008
Dino
No, it's the other way around - in row-major order.

1 2
3 4

1 = [2][3]
2 = [2][4]
3 = [3][3]
4 = [3][4]

Todd
• 05-13-2008
YongzZ
Thanks all. I try doing it again tmrw. For some reason my laptop doesnt allow the compiler to work. I'll hv to do it on the school's PC.
• 05-13-2008
vart
Quote:

my laptop doesnt allow the compiler to work
Some specially designed laptop with compiler disabled environment?.. ;)