Note that there are some explanatory texts on larger screens.

plurals
  1. POC creating a matrix using pointers and then copying elements from the matrix
    primarykey
    data
    text
    <p>Im trying to use a function to copy a row of a matrix and return a pointer to that row, then print that row, but my get_row() function is failing, any help would be most appreciated, random data is a specified piece of the program, i will then have to get a column, transpose and sub matrix the same way, but i hope if i understand how to do it for get_row() ill be able to do the rest: </p> <p>my algorithm in get_row() is wrong here is my code:</p> <p>my main(revised):</p> <pre><code>int main() { int m,n,t,check; double **mat,*matc; check = 0 ; while ( check != 2 ) { printf("\nEnter the size of your matrix m x n in the form m,n : " ); check = scanf( "%d, %d", &amp;m, &amp;n ); _flushall() ; }; int row; mat = (double **) malloc(m * sizeof(double*)) ; for(row = 0; row&lt;m; row++) { mat[row] = (double *) malloc(n * sizeof(double)); } srand((unsigned int) time(NULL)); *rand_matrix(*mat,m,n); print_matrix(*mat,m,n); check = 0 ; while ( check != 1 ) { printf("\nEnter the row you would like to see : " ); check = scanf( "%d", &amp;t ); _flushall() ; }; *matc=*get_row(*mat,n,t); print_matrix(matc,4,n); check = 0 ; while ( check != 1 ) { printf("\nEnter the column you would like to see : " ); check = scanf( "%d", &amp;t ); _flushall() ; } printf("\nMatrix column: [%d]\n",t); get_column( *mat,m, n, t); getch(); transpose( *mat, n, m); getch(); free(mat); } </code></pre> <p>These are the functions im using, have a look at get_row(), and check if you can spot what im doing wrong, cheers</p> <pre><code>//FUNCTION TO POPULATE MATRIX WITH RANDOM DOUBLES double *rand_matrix( double *mat, int m, int n) { double *usermat=mat; int i; for (i=0;i&lt;m*n;i++) { *usermat++=i+1; //populates with 1 to m*n } return mat; } //PRINTS MATRIX void print_matrix( double *mat, int m, int n) { int i,j; printf("\nMatrix dimensions: [%d,%d]\n",m,n); double *usermat=mat; for (i=0;i&lt;m;i++) { usermat=(mat+i*n); for (j=0;j&lt;n;j++) { printf(" %0.2lf ",*usermat++); } printf("\n"); } } //GET ROW double *get_row( double *mat, int n,int t) { int i,j; printf("\nMatrix row: [%d]\n",t); double *usermat=mat; printf("\n"); usermat=(mat+n*(t-1)); for (j=0;j&lt;n;j++) { *usermat++; } printf("\n"); return usermat; } </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.
 

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