It is required to write a Matrix Calculator program using C Programming Language
ID: 3919851 • Letter: I
Question
It is required to write a Matrix Calculator program using C Programming Language that could only perform addition, subtraction, and multiplication of two matrices of appropriate dimensions. Your program should enable the user to select a matrix operation from a list of options that contains the aforementioned operations. Then, the user should be prompted to enter the dimensions of the two operand matrices. Next, your program should prompt the user to enter the elements of each matrix row-wise. Your program should perform the selected matrix operation on operand matrices and display the result on the console. Use input validation loops to make sure that the user selected a valid matrix operation and that the user entered appropriate matrix dimensions for the selected operation. Test your program by computing the following: D-A +B E-A - B. and F-AC Assuming that: 2.2 5.6 8.9 25.3 17.5 11.8 5.3 9.46.7 120.-12.2 38.7 1.2 26.7 14.3 16.1-12.5 7.2 18.2 2.5 12.3 95.2 7.9 68.2 15 -12.5 and C-19.2 22 30 17Explanation / Answer
#include<stdio.h>
int main(){
int matA[100][100];
int matB[100][100];
int matC[100][100];
int i,j,n1,n2,m1,m2,k;
printf("Enter 1-Addition, 2-Subtraction, 3- Multiplication:");
int n;
scanf("%d", &n);
if (n == 1){
while (1) {
printf("Enter the dimensions of first matrix (row <space> column):");
scanf("%d%d",&n1,&m1);
printf("Enter the dimensions of second matrix (row <space> column):");
scanf("%d%d",&n2,&m2);
if (n1 != n2 || m1 != m2){
printf("Error: Invaild values ");
}
else
break;
}
printf("Enter values for first matrix row wise: ");
for (i = 0; i<n1; i++){
printf("Enter row %d :",i);
for (j = 0; j<m1; j++){
scanf("%d",&matA[i][j]);
}
}
printf("Enter values for second matrix row wise: ");
for (i = 0; i<n1; i++){
printf("Enter row %d :",i);
for (j = 0; j<m1; j++){
scanf("%d",&matB[i][j]);
}
}
for (i = 0; i<n1; i++){
for (j = 0; j<m1; j++){
matC[i][j] = matA[i][j] + matB[i][j];
}
}
printf("Answer: ");
for (i = 0; i<n1; i++){
for (j = 0; j<m1; j++){
printf("%d ",matC[i][j]);
}
printf(" ");
}
}
if (n == 2){
while (1) {
printf("Enter the dimensions of first matrix (row <space> column):");
scanf("%d%d",&n1,&m1);
printf("Enter the dimensions of second matrix (row <space> column):");
scanf("%d%d",&n2,&m2);
if (n1 != n2 || m1 != m2){
printf("Error: Invaild values ");
}
else
break;
}
printf("Enter values for first matrix row wise: ");
for (i = 0; i<n1; i++){
printf("Enter row %d :",i);
for (j = 0; j<m1; j++){
scanf("%d",&matA[i][j]);
}
}
printf("Enter values for second matrix row wise: ");
for (i = 0; i<n1; i++){
printf("Enter row %d :",i);
for (j = 0; j<m1; j++){
scanf("%d",&matB[i][j]);
}
}
for (i = 0; i<n1; i++){
for (j = 0; j<m2; j++){
matC[i][j] = matA[i][j] - matB[i][j];
}
}
printf("Answer: ");
for (i = 0; i<n1; i++){
for (j = 0; j<m2; j++){
printf("%d ",matC[i][j]);
}
printf(" ");
}
}
if (n == 2){
while (1) {
printf("Enter the dimensions of first matrix (row <space> column):");
int n1,m1;
scanf("%d%d",&n1,&m1);
printf("Enter the dimensions of second matrix (row <space> column):");
int n2,m2;
scanf("%d%d",&n2,&m2);
if (m1 != n2){
printf("Error: Invaild values ");
}
else
break;
}
printf("Enter values for first matrix row wise: ");
for (i = 0; i<n1; i++){
printf("Enter row %d :",i);
for (j = 0; j<m1; j++){
scanf("%d",&matA[i][j]);
}
}
printf("Enter values for second matrix row wise: ");
for (i = 0; i<n2; i++){
printf("Enter row %d :",i);
for (j = 0; j<m2; j++){
scanf("%d",&matB[i][j]);
}
}
for (i = 0; i<n1; i++){
for (j = 0; j<m2; j++){
int sum = 0;
for (k = 0; k<m1; k++){
sum = sum + matA[i][k] * matB[k][j];
}
matC[i][j] = sum;
}
}
printf("Answer: ");
for (i = 0; i<n1; i++){
for (j = 0; j<m2; j++){
printf("%d ",matC[i][j]);
}
printf(" ");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.