Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in C++ for Matrix Addition, Matrix Subtraction, and Matrix Multi

ID: 3768701 • Letter: W

Question

Write a program in C++ for Matrix Addition, Matrix Subtraction, and Matrix Multiplication.
The program should first display a MENU of available operations such as: 1. MATRIX ADDITION 2. MATRIX SUBTRACTION 3. MATRIX MULTIPLICATION Please enter your choice number: __
After the user has entered his/her choice of operation, your program should ask user about the size of each matrix, such as: How many rows are in the first matrix: _ How many columns are in the second matrix: _ How many rows are in the second matrix: _ How many columns are in the second matrix: _
Note that for matrix addition/subtraction, number of rows and columns of first matrix must be equal to number of rows/columns of the second matrix. Make sure that the user has entered correct numbers of rows/columns, otherwise give an error message. For Matrix Multiplication, number of columns of first matrix must be equal to number of rows of the second matrix. Check for this condition, otherwise give an error message. After checking the above conditions, ask the user to enter data for matrices, such as: Row#? Data for first matrix: _ (separated by commas) . (figure out data in each column automatically from the row data) Row#? Data for second matrix: _ (separated by commas) . (figure out data in each column automatically from the row data)
If the user enter incorrect amount of data in any row, generate error message and ask user for correct amount of data. After data for all rows/columns for the two matrices has been entered correctly, calculate the resultant matrix and display it as follows (depending on selected choice): Row#? of First Matrix + Second Matrix = ?,??,???,… . Row#? of First Matrix - Second Matrix = ?,??,???,… . Row#? of First Matrix x Second Matrix = ?,??,???... .

Explanation / Answer

# include<stdio.h>
       void display(int [][3]);
       void main()
       {
           int c;
           void func1();
           void func2();
           void func3();
           clrscr();
             printf(" - : Matrix Manipulation Functions (for 3 X 3 Matrix) : -");
           printf(" -------------------------------------");
           printf(" Matrix Addition            : 1");
           printf(" Matrix Subtraction         : 2");
           printf(" Matrix Multiplication      : 3");
           printf(" Enter Your Choice          : ");
           scanf("%d",&c);
           switch(c)
           {
               case 1:
                   func1();
                   break;
               case 2:
                   func2();
                   break;
               case 3:
                   func3();
                   break;
                   default:
                   printf(" Invalid Choice");
           }
           getch();
       }


       void func1()
       {
           int x[3][3],y[3][3],z[3][3];
           void getmatrix(int [][3]);
           void addition(int [][3],int [][3],int [][3]);
           clrscr();
           getmatrix(x);
           getmatrix(y);
           addition(x,y,z);
           printf(" - : Matrix 1: - ");
           display(x);
           printf(" - : Matrix 2: - ");
           display(y);
           printf(" - : Matrix Addition (Result): - ");
           display(z);
       }
       void getmatrix(int t[][3])
       {
           int i,j;
           for(i=0;i<3;i++)
           {
               for(j=0;j<3;j++)
               {
                   printf("Enter element [%d][%d] : ",i,j);
                   scanf("%d",&t[i][j]);
               }
           }
       }
       void addition(int p[][3],int q[][3],int r[][3])
       {     int i,j;
           for(i=0;i<3;i++)
           {      for(j=0;j<3;j++)
                   r[i][j]=p[i][j]+q[i][j];
           }
       }
       void func2()
       {
           int x[3][3],y[3][3],z[3][3];
           void getmatrix(int [][3]);
           void subtraction(int [][3],int [][3],int [][3]);
           clrscr();
           getmatrix(x);
           getmatrix(y);
           subtraction(x,y,z);
           printf(" - : Matrix 1: - ");
           display(x);
           printf(" - : Matrix 2: - ");
           display(y);
           printf(" - : Matrix Subtraction (Result): - ");
           display(z);
       }
       void subtraction(int p[3][3],int q[3][3],int r[3][3])
       {
           int i,j;
           for(i=0;i<3;i++)
           {
               for(j=0;j<3;j++)
                   r[i][j]=p[i][j]-q[i][j];
           }
       }
       void func3()
       {
           int x[3][3],y[3][3],z[3][3];
           void getmatrix(int [][3]);
           void multiplication(int [][3],int [][3],int [][3]);
           clrscr();
           getmatrix(x);
           getmatrix(y);
           multiplication(x,y,z);
           printf(" - : Matrix 1: - ");
           display(x);
           printf(" - : Matrix 2: - ");
           display(y);
           printf(" - : Matrix Multiplication (Result): - ");
           display(z);
       }
       void multiplication(int p[][3],int q[3][3],int r[3][3])
       {
           int i,j,k;
           for(i=0;i<3;i++)                
//condition i< total row of matrix1
           {
               for(j=0;j<3;j++)        
//condition i< total col of matrix1 or//condition i< total row of matrix2
{
                   r[i][j]=0;
                   for(k=0;k<3;k++) //condition i< total col of matrix2
                       r[i][j]=r[i][j]+(p[i][j]*q[j][k]);
               }
           }
       }
       void display(int m[][3])
       {
           int i,j;
           printf(" ");
           for(i=0;i<3;i++)
           {
               for(j=0;j<3;j++)
                   printf("%d ",m[i][j]);
               printf(" ");
           }
       }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote