Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try this one. may be helpful to you. </p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; double **read_matrix(int rows, int cols); void print_matrix(int rows, int cols, double **mat); void free_matrix(int rows, double **mat); double **read_matrix(int rows, int cols){ double **mat = (double **) malloc(sizeof(double *)*rows); int i=0,j=0; for(i=0; i&lt;rows; i++) /* Allocate array, store pointer */ mat[i] = (double *) malloc(sizeof(double)*cols); for(i=0; i&lt;rows; i++){ for(j=0; j&lt;cols; j++){ scanf("%lf",&amp;mat[i][j]); } } return mat; } void print_matrix(int rows, int cols, double **mat){ int i=0,j=0; for(i=0; i&lt;rows; i++){ /* Iterate of each row */ for(j=0; j&lt;cols; j++){ /* In each row, go over each col element */ printf("%lf ",mat[i][j]); /* Print each row element */ } printf("\n"); } } void free_matrix(int rows, double **mat){ int i=0; for(i=0;i&lt;rows;i++) free(mat[i]); free(mat); } int main(){ double **matrix; int rows, cols; /* First matrix */ printf("Matrix 1\n"); printf("Enter # of rows and cols: "); scanf("%d%d",&amp;rows,&amp;cols); printf("Matrix, enter %d reals: \n",rows*cols); matrix = read_matrix(rows,cols); printf("Your Matrix\n"); /* Print the entered data */ print_matrix(rows,cols,matrix); free_matrix(rows, matrix); /* Free the matrix */ return 0; } </code></pre> <p>Execution: </p> <pre><code>:~$ gcc exam.c :~$ ./a.out Matrix 1 Enter # of rows and cols: 3 4 Matrix, enter 12 reals: 1 2 3 4 5 6 7 8 9 9 0 1 Your Matrix 1.000000 2.000000 3.000000 4.000000 5.000000 6.000000 7.000000 8.000000 9.000000 9.000000 0.000000 1.000000 </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. 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.
 

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