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
Your Matrix -4.00e-01 -3.00e-01 -
2.00e-01 -1.00e-01
0.00e+00 1.00e-01
2.00e-01 3.00e-01