Write a C program that is able to view, add, or multiply two matrices input by t
ID: 3845420 • Letter: W
Question
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 5x5 matrix. Your program should be able to support any size up to a 5x5 (e.g. 2x3, 1x2, 4x4, 5x3, 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.
Explanation / Answer
The program is as follows:
#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 matrix ");
scanf("%d%d", &row1, &col1);
if (row1 > 5 || col1 > 5)
printf("The maximum value for row or column is 5 ");
} while (row1 > 5 && col1 > 5)
printf("Enter the elements of first matrix ");
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 matrix ");
scanf("%d%d", &row2, &col2);
if (row2 > 5 || col2 > 5)
printf("The maximum value for row or column is 5 ");
} while (row2 > 5 && col2 > 5)
printf("Enter the elements of second matrix ");
for (i = 0; i < row2; i++)
for (j = 0; j < col2; j++)
scanf("%d", &second[i][j]);
do {
printf("1.View the matrices ");
printf("2.Add the matrices ");
printf("3.Multiply the matrices ");
printf("4.Quit ");
switch (choice) {
case 1:
printf ("First Matrix: ");
for (i = 0; i < row1; i++){
for (j = 0; j < col1; j++)
printf("%d %c", first[i][j], ' ');
printf(" ");
}
printf ("Second Matrix: ");
for (i = 0; i < row2; i++){
for (j = 0; j < col2; j++)
printf("%d %c", second[i][j], ' ');
printf(" ");
}
case 2:
if ((row1 == row2) && (col1 == col2)){
printf ("Sum of Matrices: ");
for (i = 0; i < row1; i++){
for (j = 0; j < col1; j++)
printf("%d %c", first[i][j]+second[i][j], ' ');
printf(" ");
}
}
else {
printf("Rows and columns of matrices are not compatible for addition ");
}
case 3:
if (col1 == row2){
printf ("Multiplication of Matrices: ");
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(" ");
}
}
else {
printf("Rows and columns of matrices are not compatible for multiplication ");
}
}
} while (choice ! = 4)
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.