Thread: Matrices

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    15

    Matrices

    I am currently working on an assignment to simulate how an operating system handles deadlocks.

    Anyway, I am having difficulties creating a Matrix -- Does anyone have a preference that they use when creating one? I was trying to use a two-dimensional array, but I must admit it is not working very well. (As in when I pass it to a function).

    I am also using irregular structures that I created on my own as objects in the matrix.

    Thank you for your help in advance! =D

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You can figure out how to pass arrays, simply by copy/pasting the declaration of the array you want to pass.

    Eg
    int matrix[10][20];

    You would have a function
    void foo ( int matrix[10][20] );

    And you would pass the matrix to the function like so
    foo ( matrix );

    Accessing the array inside the function is identical to accessing the array outside the function. The only thing you can't do inside the function is sizeof(array) (you get the pointer size, not the array size).

    In this respect, it's no different from how you would pass a scalar value to a function.

    Now you can mess about expressing the function in alternative forms, like
    void foo ( int matrix[][20] );
    void foo ( int (*matrix)[20] );
    but that's just extra work
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715

    Matrices

    You can pass matrices to a function using one dimensional array to simulate matrices:
    example

    int num_rows,num_columns,i,j;
    double *mat;
    printf("enter dimensions of matrix:");
    scanf("%d %d",&num_rows,&num_columns);
    mat=(double *)malloc(num_rows*num_columns*sizeof(double));
    /*Entering values from stdin*/
    for(i=0;i<num_rows;i++)
    for(j=0;j<num_columns;j++)
    scanf("%lf",&mat[i*num_columns+j]);
    ...........................

    void show_matrix(double*mat,int numr,int numk)
    .........

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