Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This answer is total overkill, but this is my <code>struct</code> based approach which n.m. called "a whole 'nother story."</p> <p>If this is a new program (i.e. you get to define what the input / output to your function looks like) and you intend to do a lot of work with matrices, I would be inclined to use a struct to represent a matrix, and just pass around pointers to instances of that struct. </p> <p>The code below shows one possible approach. Note there are a couple "tricks" going on here. First of all, the data is all stored in a single contiguous block--this is likely to improve performance for a number of reasons. One potential downside to this technique is that resizing a matrix becomes expensive since you have to allocate an entirely new instance and copy data. But if you find that to be a problem, you can always change your implementation, assuming you always use matrix_get() and matrix_set() functions to access values in the matrix.</p> <p>Also, the matrix struct and the memory that data pointer points to is all allocated in a single malloc call. If you use this technique, just be aware that of data alignment issues. For example, if you change data to point to 64-bit integers or doubles, you'll need to add padding to make sure everything is 8-byte aligned. Alternatively, just <code>malloc</code> the data pointer as a separate array in the <code>new_matrix()</code> function, provided you remember to free it in <code>free_matrix()</code>.</p> <p>I've left as an exercise to the OP to write the multiply function. </p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; struct matrix { int rows; int cols; int * data; }; struct matrix * new_matrix( int rows, int cols ) { struct matrix * m = NULL; /* Allocate a block of memory large enough to hold the matrix 'header' struct * as well as all the row/column data */ m = malloc( sizeof(struct matrix) + (rows * cols * sizeof(int) ) ); if( m ) { m-&gt;rows = rows; m-&gt;cols = cols; /* Some ugly pointer math to get to the first byte of data */ m-&gt;data = (int*) ( (char *) m + sizeof(*m) ); } return m; } void free_matrix( struct matrix * m ) { free( m ); } int matrix_set( struct matrix * m, int row, int col, int val) { if( col &gt;= m-&gt;cols || row &gt;= m-&gt;rows ) return -1; m-&gt;data[ m-&gt;cols * row + col ] = val; return 0; } int matrix_get( struct matrix * m, int row, int col, int * val) { if( col &gt;= m-&gt;cols || row &gt;= m-&gt;rows ) return -1; else { *val = m-&gt;data[ m-&gt;cols * row + col ]; return 0; } } void print_matrix( struct matrix * m ) { int r,c; int val; for( r = 0; r &lt; m-&gt;rows; r++ ) { for( c = 0; c &lt; m-&gt;cols; c++ ) { matrix_get( m, r, c, &amp;val ); printf( "%5d%s", val, c + 1 &lt; m-&gt;cols ? "," : "" ); } printf("\n"); } } int main (int argc, char **argv) { int r,c; struct matrix * m = new_matrix( 5, 5 ); for( r = 0; r &lt; m-&gt;rows; r++ ) { for( c = 0; c &lt; m-&gt;cols; c++ ) { matrix_set( m, r, c, (r +1)* 10 + c + 1 ); } } print_matrix( m ); free_matrix( m ); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload