user. Dynamically allocate space for both input matrices.
Implement 4 matrix operators: add, subtract, multiply and inverse, each of which should be implemented
as a function using call by reference. Devise appropriate tests within each function to determine whether
the operator is valid for the dimensions specified – if not, an error should be printed.
Inverse should only be implemented for 2×2 matrices only and only for the A matrix. Attempting to
compute the inverse of any other size should print an error message.
Full credit will only be given for solutions that follow the specification, but also where each part of the
program has been thoroughly tested, both with valid and invalid data. Use appropriate error messages
whenever the user enters invalid data.
Hint: You need to devise a suitable structure to store the dimensions and the data of each matrix. The
function call, for example, to multiply A and B together should be as simple as
multiply_matrix(A, B)
where A and B are structures (or pointers to structures). You may choose simply to print out the result on
the screen, or store the result in a matrix C with a separate print_matrix function, e.g.
the screen, or store the result in a matrix C with a separate print_matrix function, e.g.
NOTE -the example is shown in the attached file
Sample output of program:
1 – Enter matrices A & B
2 – Add matrices
3 – Subtract matrices
4 – Multiply matrices
5 Ăâ Inverse of A
6 – Quit program
Option: 1
Number of rows in A: 2
Number of columns in A: 3
Enter matrix data:
1 2 3
3 4 5
Number of rows in B: 2
Number of columns in B: 3
Enter matrix data:
4 3 2
2 1 0
(menu is printed out again)
Option: 2
A + B =
5.0 5.0 5.0
5.0 5.0 5.0
Option: 3
A – B =
-3.0 -1.0 1.0
1.0 3.0 5.0
Option: 4
Inverse of A is invalid for a 2×3 matrix
Option: 1
Number of rows in A: 2
Number of columns in A: 2
Enter matrix data:
2.0 1.0
1.0 2.0
Number of rows in B: 2
Number of columns in B: 2
Enter matrix data:
0.5 0.0
0.0 0.5
Option: 4
A * B =
1.0 0.5
0.5 1.0
Option: 5
Inverse of A =
0.66667 -0.33333
-0.33333 0.66667
Special note:
If you enter option 1 to enter A and B, and then you enter 1 again later in the program to enter new A and B matrices, the entry routine should free the previously allocated space (in order to avoid memory leaks)





