# reading a matrix and printing out the matrix

• 10-22-2012
Lina_inverse
reading a matrix and printing out the matrix
Hi Im writing a function to read the matrix user enters and print it out, the main function:

Code:

```double **read_matrix(int rows, int cols); void print_matrix(int rows, int cols, double **mat); int main(){ double **matrix;   int rows, cols;   /* First matrix */   printf("Matrix 1\n");   printf("Enter # of rows and cols: ");   scanf("%d %d",&rows,&cols);   printf("Matrix, enter %d reals: ",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 */ }```
the 2 functions, read and print are what I am having problems with:

Code:

```double **read_matrix(int rows, int cols){  double matrix = (double **) malloc(sizeof(double *)*rows); int i=0;   for(i=0; i<rows; i++){     /* Allocate array, store pointer  */     matrix2[i] = (double *) malloc(sizeof(double)*cols);   } } void print_matrix(int rows, int cols, double **mat){  for(i=0; i<rows; i++){    /* Iterate of each row */     for(j=0; j<cols; j++){    /* In each row, go over each col element  */       printf("%f ",matrix2[i][j]); /* Print each row element */     }     printf("\n");        /* Finish a row, start a new line */   } }```
any help to finish the 2 functions?

this is what it's suppose to look like:

Enter # of rows and cols: 4 2
Matrix, enter 8 reals: -.4 -.3 -.2 -.1 0 .1 .2 .3
2.00e-01 -1.00e-01
0.00e+00 1.00e-01
2.00e-01 3.00e-01
• 10-22-2012
You are creating a double dimension matrix in read matrix. You need to return a pointer to a double matrix. So, *** on the return.

You shouldn't be casting the return from malloc. This is C, not C++. In print_matrix, you are using the wrong matrix name. Your parameter says "mat". Int main() requires a return 0 at the end.

I haven't check the code all the way through, so there may be other errors, also. When you compile, you should be seeing them, or warnings, at least. If you are seeing none, be sure your errors and warnings are turned up on your compiler, and pay close attention to them.

Also, list them here, when you need assistance. It really helps us find problems without having to use our own compilers.
• 10-22-2012
laserlight
Quote:

You are creating a double dimension matrix in read matrix. You need to return a pointer to a double matrix. So, *** on the return.

Yeah, though I suggest that you create a struct for this, e.g.,
Code:

```typedef struct matrix {     int rows;     int cols;     double **entries; } matrix_type;```
This way, you can just return a pointer to a matrix_type, and you won't also need to keep passing the number of rows and columns separately.
• 10-22-2012
Lina_inverse
by using a struct, how do i store the inputs into the array?
• 10-22-2012
Lina_inverse
and btw, how do print out the numbers in the format of : ie. positive .2 is a 2.00e-01
• 10-22-2012
laserlight
Quote:

Originally Posted by Lina_inverse
by using a struct, how do i store the inputs into the array?

I think it would be easier to convert your print_matrix function as an example:
Code:

```void print_matrix(const matrix_type *matrix) {     /* Print the matrix in row major order */     int i;     for (i = 0; i < matrix->rows; i++) {         int j;         for (j = 0; j < matrix->cols; j++) {             printf("%f ", matrix->entries[i][j]);         }         printf("\n");     } }```
Quote:

Originally Posted by Lina_inverse
how do print out the numbers in the format of : ie. positive .2 is a 2.00e-01

Read up on the format specifiers that can be used with printf.
• 10-22-2012
Lina_inverse
Code:

```double **read_matrix(int rows, int cols){ typedef struct matrix {     int rows;     int cols;     double **entries; } matrix_type; matrix_type **mat=malloc(n*sizeof(matrix_type)); for(i=0;i<n;i++){ } }```
• 10-22-2012
laserlight
The struct should be defined before the function, not in the function body. It may be simpler to read the number of rows and columns into the members of a matrix object, then write:
Code:

```void read_matrix(matrix_type *matrix) {     int i;     matrix->entries = malloc(matrix->rows * matrix->entries[0]);     for (i = 0; i < matrix->rows; i++) {         /* ... */     } }```
• 10-23-2012
AndiPersti
Quote:

Originally Posted by laserlight
Code:

`matrix->entries = malloc(matrix->rows * matrix->entries[0]);`

You're missing the sizeof operator:
Code:

`matrix->entries = malloc(matrix->rows * sizeof(matrix->entries[0]));`
Bye, Andreas
• 10-23-2012
SashaN
It can also be done in this manner.
Code:

```#include <stdio.h> void read_matrix(double *matrix, int rows, int cols); void print_matrix(double *matrix, int rows, int cols); int main(void) {     int rows, cols;     printf("Matrix 1\n");     printf("Enter # of rows and cols: ");     scanf("%d %d",&rows,&cols);     double matrix[rows][cols];         printf("Matrix, enter %d reals: ",rows*cols);     read_matrix(&matrix[0][0],rows,cols);     printf("Your Matrix\n");     print_matrix(&matrix[0][0],rows,cols);     return 0; } void read_matrix(double *matrix, int rows, int cols) {     int i, j;     for (i=0; i<rows; ++i)         for (j=0; j<cols; ++j)             scanf("%lf", (matrix+i*cols+j)); } void print_matrix(double *matrix, int rows, int cols) {     int i, j;     for (i=0; i<rows; ++i)         for (j=0; j<cols; ++j)             printf("%lf\n", *(matrix+i*cols+j));     printf("/n"); }```