A matrix is a twodimensional
data structure. It is usually organized as a row of columns
of numbers. Examples of matrices are:
A=[2 3 4
2 4 5
1 1 3 ] B=[5 6 2
2 5 3
1 6 7 ]
A and B are examples of 3 by 3 matrices. Each element of the matrix can be addressed by
the row, column indices, mathematically written as a(i,j) where i and j are the row and
column indices, respectively. In your program, you will use twodimensional
arrays of
floating point values to represent such matrices. In this assignment, you may assume that
the a matrix will be no larger than 5x5, but may also be smaller, with unequal
dimensions. You should use global variables to hold the dimensions of each matrix. So
the matrix A should be defined as:
double A[5][5];
and current dimension values (that may change as the user inputs new matrices):
int columnsA = 3; /* initial number of columns in A*/
int rowsA = 3; /* initial number of rows in A*/
Your program should be menu driven where the current matrix A is always displayed and
the user then selects matrix operations from the following menu:
| 1.0 0.0 0.0 |
A = | 0.0 1.0 0.0 |
| 0.0 0.0 1.0 |
(1)Set matrix A
(2)Add a matrix B (matrix addition)
(3)Multiply with a matrix B (matrix multiplication)
(4)Transpose matrix A
(5)Exit
Please enter your choice:
Your program should perform the following operations for the options:
(1) Enter matrix A
This function should allow the user to set the value of A by entering the dimensions and a
number for each element of the matrix.
For example, for a 2x3 matrix, it should prompt for values as follows:
Please enter the number of rows for A: 2
Please enter the number of columns for A: 3
Please enter A[0][0]: 1
Please enter A[0][1]: 2
Please enter A[0][2]: 1
Please enter A[1][0]: 2
Etc.
(2) Matrix Addition
Here, a second matrix B should be entered by the user. Then, the program should
compute the addition of the matrices A + B and assign the result back to matrix A so that
the result of the matrix addition is displayed before the next menu. (If you prefer, you
may store the result of the addition first in a local matrix C, and then copy C over to A).
Hint: Matrix addition is defined as follows:
C(i,j) = A(i,j) + B(i,j) for all values of i and j
(3) Matrix Multiplication
Again, this is very similar to option (2). Here, however, the matrix A should be
multiplied by matrix B by use of a matrix multiplication.
Hint: Each element in the result of a matrix multiplication A * B is computed as the sum
of the products of the row elements of A and the column elements of B.
(4) Transpose of the Matrix
Compute the transpose of matrix A.
Hint: The transpose of a matrix is obtained by swapping the row and column indices, i.e.
T(j, i) = A(i, j) for all i and j
(5) Exit
This option will terminate your program.
Implementation
To implement your program, you must use separate functions to perform the operations
required by the menu options. The function names should detail what the function does.
Please use the following function names: SetMatrixA( ), DisplayMatrixA( ),
AddMatrixBtoA( ) to perform the matrix addition, MultiplyMatrixBtoA( ) for matrix
multiplication, TransposeMatrixA(), and InvertMatrixA().
Define all matrices as global variables so that all functions have easy access to them.
There should be three matrices used in your program:
·Matrix A: used as the current matrix and the one on which all operations are
performed. The result of each operation should also be stored in matrix A for use in
the next operation. At the beginning of your program, this matrix should be initialized
to a 3x3 identity matrix (as shown at the beginning).
·Matrix B: used as the operand matrix. That is, when performing the addition,
multiplication, matrix A should be the first, and matrix B should be the second
operand. This should be consistent throughout the program.
·Matrix C: used as a temporary matrix. You may temporarily store the result of a
matrix operation in C. However, before returning to displaying the menu options,
matrix C should be copied over into A so that the result can be used for further
operations.
Script file
To demonstrate that your program works correctly, perform the following steps and
submit them as your script file:
1.Set matrix A to the following: A=[2 1 4 5
1 −1 −2 3
0 −3 3 −2 ]
2.Add the following matrix B to A: B=[2 −3 −5 −1
2 3 2 6
1 3 −3 1 ]
3.Multiply the resulting matrix with the following: B=[0 0 1
3 1 2
4 −3 −2
1 0 1 ]
4.Transpose matrix A.
5.Exit the program.