Write a C program that is able to view, add, or multiply two matrices input by the user. After inputting the two matrices, the user should be presented with a menu of possible operations ◦ View the matrices ◦ Add the matrices ◦ Multiply the matrices ◦ Quit the program The user should be able to make a choice, see the outcome of the operation, and then make another choice from the menu. The program should continue to operate in this manner until the user selects the quit action, at which point the program should exit. 1. Collect user input for two matrices ◦ The size of each matrix in rows and columns ◦ The data stored in the matrices The user should be able to input up to a 5×5 matrix. Your program should be able to support any size up to a 5×5 (e.g. 2×3, 1×2, 4×4, 5×3, etc). The matrices should hold double type floating point numbers and be stored in a single or multidimensional array. 2. Display a menu of available options that includes, at least, the following ◦ View the matrices ◦ Add the matrices ◦ Multiply the matrices ◦ Quit the program The program should loop indefinitely until the user selects the option to quit. Each time an operation is selected, your program should perform the selected operation, display the results, and then present the user with the menu again. 3. The results of the performed operation should only be displayed and not stored, meaning they should not affect the values of the matrices input by the user. Thus performing the same operation repeatedly will return the same results. 4. Before performing an operation, you must ensure that the operation is valid. If the user has input matrices which are not compatible with the selected operation, print an error message indicating the problem and then present them with the menu again. Do not simply quit the program.
Expert Answer
#include<stdio.h>
void main(){
int row1, col1,
int row2, col2;
int sum;
int i, j, k,l;
int first[5][5], second[5][5];
int choice;
do {
printf(“Enter the number of rows and columns of first matrixn”);
scanf(“%d%d”, &row1, &col1);
if (row1 > 5 || col1 > 5)
printf(“The maximum value for row or column is 5n”);
} while (row1 > 5 && col1 > 5)
printf(“Enter the elements of first matrixn”);
for (i = 0; i < row1; i++)
for (j = 0; j < col1; j++)
scanf(“%d”, &first[i][j]);
do {
printf(“Enter the number of rows and columns of second matrixn”);
scanf(“%d%d”, &row2, &col2);
if (row2 > 5 || col2 > 5)
printf(“The maximum value for row or column is 5n”);
} while (row2 > 5 && col2 > 5)
printf(“Enter the elements of second matrixn”);
for (i = 0; i < row2; i++)
for (j = 0; j < col2; j++)
scanf(“%d”, &second[i][j]);
do {
printf(“1.View the matricesn”);
printf(“2.Add the matricesn”);
printf(“3.Multiply the matricesn”);
printf(“4.Quitn”);
switch (choice) {
case 1:
printf (“First Matrix:nn”);
for (i = 0; i < row1; i++){
for (j = 0; j < col1; j++)
printf(“%d %c”, first[i][j], ‘ ‘);
printf(“n”);
}
printf (“Second Matrix:nn”);
for (i = 0; i < row2; i++){
for (j = 0; j < col2; j++)
printf(“%d %c”, second[i][j], ‘ ‘);
printf(“n”);
}
case 2:
if ((row1 == row2) && (col1 == col2)){
printf (“Sum of Matrices:nn”);
for (i = 0; i < row1; i++){
for (j = 0; j < col1; j++)
printf(“%d %c”, first[i][j]+second[i][j], ‘ ‘);
printf(“n”);
}
}
else {
printf(“Rows and columns of matrices are not compatible for additionn”);
}
case 3:
if (col1 == row2){
printf (“Multiplication of Matrices:nn”);
for (i = 0; i < row1; i++){
for (j = 0; j < col2; j++){
sum = 0;
for (k=0; k<col1; k++)
sum = sum + first[i][k] * second[k][j];
printf(“%d %c”, sum, ‘ ‘);
}
printf(“n”);
}
}
else {
printf(“Rows and columns of matrices are not compatible for multiplicationn”);
}
}
} while (choice ! = 4)
}