// Online C compiler to run C program online
#include <stdio.h>
int add(int matrixA, int matrixB);
int sub(int matrixA, int matrixB);
int product(int matrixA, int matrixB);
int trans(int matrixA, int matrixB);
int main() {
int matA [3] [3];
//taking input from user
printf("Enter values for a matrix A(3x3) :\n");
for (int i = 0; i < 3 ; i++) {
for(int j = 0 ; j < 3 ; j++) {
printf("row %d and column %d: ", i + 1 , j+1);
scanf("%d", &matA[i][j]);
}
}
int matB [3] [3];
printf("\nEnter values for matrix B(3x3) :\n");
for (int i = 0 ; i < 3 ; i++) {
for(int j = 0 ; j<3; j++) {
printf("row %d and column %d: ", i+1, j+1);
scanf("%d", &matB[i][j]);
}
}
printf("Select operation to perform on above matrices:\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Transpose of A or B");
int ch;
scanf("%d", &ch);
switch(ch){
case 1: add(matA[3][3], matB[3][3]);
break;
case 2: sub(matA[3][3], matB[3][3]);
break;
case 3: product(matA[3][3], matB[3][3]);
break;
case 4: trans(matA[3][3], matB[3][3]);
break;
default: printf("This is not a valid operation!");
}
return 0;
}
int add(int matrixA, int matrixB){
int sum[3][3];
printf("The sum of matrix A and B is: \n");
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
sum[i][j] = matrixA[i][j] + matrixB[i][j];
printf("%d ", sum[i][j]);
}
printf("\n");
}
}
int sub(int matrixA, int matrixB){
int sub[3][3];
printf("The diffrence of matrices A and B (A-B) is: \n");
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
sub[i][j] = matrixA[i][j] - matrixB[i][j];
printf("%d ", sub[i][j]);
}
printf("\n");
}
}
int product(int matrixA, int matrixB){
int multi[3][3];
printf("The product of matrix A and B is: \n");
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
for(int k=0; k<3; k++){
multi[i][j] = matrixA[i][j] * matrixB[j][k];
printf("%d ", multi[i][j]);
}
}
printf("\n");
}
}
int trans(int matrixA, int matrixB){
printf("Transpose of matrix A: \n");
int temp1 = matrixA;
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
matrixA[i][j]=temp1[j][i];
printf("%d ", matrixA[i][j]);
}
printf("\n");
}
printf("Transpose of matrix B: \n");
int temp2 = matrixB;
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
matrixB[i][j]=temp2[j][i];
printf("%d ", matrixB[i][j]);
}
printf("\n");
}
}