Exercise: Dynamic Matrix Function

Write code to solve problem.

In an earlier exercise, we saw how to print a matrix and how to multiply two matrices. But the size of the matrix was a fixed constant. Now, we want to accomplish the same using a matrix that’s created using dynamic memory allocation, so that the number of rows and columns of the matrix are passed as variables to a function createMatrix.

Question

Complete the definitions of the functions createMatrix, destroyMatrix, printMatrix, and matrixMult provided below.

First, your function will be called with two matrices. After that, the function will be applied to these matrices and the output will be printed. Here is an example of what the output from your finished program might look like:

Shell
[
1.200 2.300
3.400 4.500
5.600 6.700
]
[
5.500 6.600 7.700
1.200 2.100 3.300
]
[
9.360 12.750 16.830
24.100 31.890 41.030
38.840 51.030 65.230
]

Note: For the printMatrix and matrixMult functions, you may make minor modifications to the solution seen in the earlier exercise.

Exercise: Dynamic Matrix Function

Write code to solve problem.

In an earlier exercise, we saw how to print a matrix and how to multiply two matrices. But the size of the matrix was a fixed constant. Now, we want to accomplish the same using a matrix that’s created using dynamic memory allocation, so that the number of rows and columns of the matrix are passed as variables to a function createMatrix.

Question

Complete the definitions of the functions createMatrix, destroyMatrix, printMatrix, and matrixMult provided below.

First, your function will be called with two matrices. After that, the function will be applied to these matrices and the output will be printed. Here is an example of what the output from your finished program might look like:

Shell
[
1.200 2.300
3.400 4.500
5.600 6.700
]
[
5.500 6.600 7.700
1.200 2.100 3.300
]
[
9.360 12.750 16.830
24.100 31.890 41.030
38.840 51.030 65.230
]

Note: For the printMatrix and matrixMult functions, you may make minor modifications to the solution seen in the earlier exercise.

C
#include <stdio.h>
#include <stdlib.h>
typedef struct {
double *data;
int nrows;
int ncols;
} Matrix;
void printMatrix(Matrix *M);
void matrixMult(Matrix *A, Matrix *B, Matrix *C);
Matrix *createMatrix(int nrows, int ncols);
void destroyMatrix(Matrix *M);
int main(int argc, char *argv[])
{
Matrix *A = createMatrix(3, 2);
// Uncomment the following code when you implement createMatrix
// otherwise it will give a segmention fault if createMatrix
// is not implemented correctly
/*
A->data[0] = 1.2;
A->data[1] = 2.3;
A->data[2] = 3.4;
A->data[3] = 4.5;
A->data[4] = 5.6;
A->data[5] = 6.7;
printMatrix(A);
Matrix *B = createMatrix(2, 3);
B->data[0] = 5.5;
B->data[1] = 6.6;
B->data[2] = 7.7;
B->data[3] = 1.2;
B->data[4] = 2.1;
B->data[5] = 3.3;
printMatrix(B);
Matrix *C = createMatrix(3, 3);
matrixMult(A, B, C);
printMatrix(C);
destroyMatrix(A);
destroyMatrix(B);
destroyMatrix(C);
*/
return 0;
}
// Your code goes below...
Matrix *createMatrix(int nrows, int ncols)
{
// Fill in the code here
}
void destroyMatrix(Matrix *M)
{
// Fill in the code here
}
void printMatrix(Matrix *M)
{
// Fill in the code here
printf("So far printMatrix does nothing\n");
}
void matrixMult(Matrix *A, Matrix *B, Matrix *C)
{
// Fill in the code here
printf("So far matrixMult does nothing\n");
}