Create a program that asks for the user to choose either to subtract two matrices or exit the program. If the user inputs a choice that is not among the choices given, then the program’s screen will clear and display the main menu again.
For the first menu item, the program must be able to get the number of rows and columns of the two matrices. The dimensions (row x column) of the two matrices must be of the same size before asking the user for the values of each matrices.
If the dimensions do not match, then the program will display this:
WHEN SUBTRACTING MATRICES, THE DIMENSIONS OF BOTH MATRICES MUST MATCH.
MATRIX 1 - MATRIX 2 = DIFFERENCE MATRIX. THE MATRIX DIMENSIONS ARE VARIABLE, MEANING THE USER WILL INPUT THE DIMENSIONS THEY WANT FOR THE TWO MATRICES.
Once the program gets the values of each matrix, it will display the two matrices and it’s resulting Difference Matrix:
using namespace std;
#define MAX_SIZE 3
main(void)
{
int c,r,Flag=1;
int A[MAX_SIZE][MAX_SIZE] = {{1,2,3},{4,5,6},{7,8,9}};
int B[MAX_SIZE][MAX_SIZE] = {{1,3,11},{10,1,2},{3,1,5}};
int Y[MAX_SIZE][MAX_SIZE], r1, c1, r2, c2, i, j, k;
while(Flag)
{
cout<<"\n\tPress-1 to display matrix substraction.";
cout<<"\n\tPress-0 to QUIT";
Flag=2;
while(Flag<0 || Flag>1) {cout<<"\nEnter option (0/1); "; cin>>Flag;}
if(Flag)
{
cout<<"\nMatrix-A: \n";
  for(r=0;r<MAX_SIZE;r++)
  {
  for(c=0;c<MAX_SIZE;c++)
  {
cout<<"\t"<<A[r][c];
}
cout<<"\n";
}
cout<<"\n\nMatrix-B: \n";
  for(r=0;r<MAX_SIZE;r++)
  {
  for(c=0;c<MAX_SIZE;c++)
  {
cout<<"\t"<<B[r][c];
}
cout<<"\n";
}
  // Initializing elements of matrix mult to 0.
  for(i = 0; i < MAX_SIZE; ++i) for(j = 0; j < MAX_SIZE; ++j) Y[i][j]=0;
  for(i = 0; i < MAX_SIZE; ++i)
  {
    for(j = 0; j < MAX_SIZE; ++j)
    {
        Y[i][j] = A[i][k] - B[i][j];
}
}
cout<<"\n\nDisplaying the Substraction of Matrices: A - B: \n";
  for(r=0;r<MAX_SIZE;r++)
  {
  for(c=0;c<MAX_SIZE;c++)
  {
cout<<"\t"<<Y[r][c];
}
cout<<"\n";
}
}
else exit(0);
}
  return (0);
}
Comments
Leave a comment