Matrices

This is a discussion on Matrices within the C Programming forums, part of the General Programming Boards category; Hi, I would like to find a determinant of a 2*2 matrix. But there seems to be an error while ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    17

    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);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    19,380
    You might want to post the smallest and simplest compilable program that demonstrates the problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Where's main()?
    Post the complete source code.

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    17
    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);
    }

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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).
    Last edited by zacs7; 05-13-2008 at 05:58 AM.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    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
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    17
    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?

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    "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?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    17
    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.

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    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
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    17
    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.

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    5,636
    my laptop doesnt allow the compiler to work
    Some specially designed laptop with compiler disabled environment?..
    If I have eight hours for cutting wood, I spend six sharpening my axe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C simple Matrices
    By Cyberman86 in forum C Programming
    Replies: 3
    Last Post: 05-07-2009, 05:20 PM
  2. Help getting started..matrices
    By BobDole11 in forum C Programming
    Replies: 7
    Last Post: 11-15-2008, 08:51 PM
  3. adding matrices, help with switches
    By quiet_forever in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2007, 08:21 AM
  4. Comparing Matrices (D3D9)
    By MicroFiend in forum Game Programming
    Replies: 2
    Last Post: 10-12-2005, 08:36 AM
  5. Problem multiplying rotation matrices together
    By Silvercord in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 03-04-2003, 08:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21