Exercise: Matrix Functions
Question
Below is a program that uses a C structure called Matrix to represent a two-dimensional matrix. This structure contains an array data to store matrix values and two variables (nrows and ncols) to store the dimensions of the matrix.
Assume that the matrix data is stored in the array data row-wise (row by row). As we haven’t covered dynamic allocation of memory yet, for now, we assume a matrix can hold a maximum number of values equal to 1024.
You’ll be required to write two functions. One will be for printing a matrix and another for multiplying two matrices.
-
Task 1:
Your first task is to write a function
printMatrixthat prints a matrix to the screen. A function skeleton forprintMatrixis provided.Your function will be called on two matrices. Here’s the expected output.
[ 1.200 2.300 3.400 4.500 5.600 6.700 ] [ 5.500 6.600 7.700 1.200 2.100 3.300 ] -
Task 2:
Your next task is to write a function
matrixMultthat performs matrix multiplication. A function skeleton is provided for this function.Your function will be applied to two matrices and the output will be printed. Here is an example of what the output of your finished program might look like:
[ 9.360 12.750 16.830 24.100 31.890 41.030 38.840 51.030 65.230 ]
Exercise: Matrix Functions
Question
Below is a program that uses a C structure called Matrix to represent a two-dimensional matrix. This structure contains an array data to store matrix values and two variables (nrows and ncols) to store the dimensions of the matrix.
Assume that the matrix data is stored in the array data row-wise (row by row). As we haven’t covered dynamic allocation of memory yet, for now, we assume a matrix can hold a maximum number of values equal to 1024.
You’ll be required to write two functions. One will be for printing a matrix and another for multiplying two matrices.
-
Task 1:
Your first task is to write a function
printMatrixthat prints a matrix to the screen. A function skeleton forprintMatrixis provided.Your function will be called on two matrices. Here’s the expected output.
[ 1.200 2.300 3.400 4.500 5.600 6.700 ] [ 5.500 6.600 7.700 1.200 2.100 3.300 ] -
Task 2:
Your next task is to write a function
matrixMultthat performs matrix multiplication. A function skeleton is provided for this function.Your function will be applied to two matrices and the output will be printed. Here is an example of what the output of your finished program might look like:
[ 9.360 12.750 16.830 24.100 31.890 41.030 38.840 51.030 65.230 ]