Thread: Matrices

  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
    28,413
    You might want to post the smallest and simplest compilable program that demonstrates the problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Chappell Hill, Texas
    Posts
    2,332
    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
    Mainframe assembler programmer by trade. C coder when I can.

  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
    Chappell Hill, Texas
    Posts
    2,332
    "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?
    Mainframe assembler programmer by trade. C coder when I can.

  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
    Chappell Hill, Texas
    Posts
    2,332
    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
    Mainframe assembler programmer by trade. C coder when I can.

  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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    my laptop doesnt allow the compiler to work
    Some specially designed laptop with compiler disabled environment?..
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

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, 09: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, 09:20 AM