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

Matrix operations -Please add comments for functions besides main -Every local v

ID: 3686097 • Letter: M

Question

Matrix operations

-Please add comments for functions besides main

-Every local variable and every passed parameter must have a comment on its use

-Use arrays that are at most 6 x 6

-use doubles as matrix element type

write a C++ program procedural code not object oriented!!, that reads two matrices and: 1) adds them (if compatible) andprints their sum, 2) subtracts them (if compatible) and prints their difference, and3) multiplies them (if compatible) and prints their product. Prompt the user for thenames of two matrix input files (see format below) and place the output in a file ofthe user’s choosing.The format of the input files should contain the number of rows, followed by thenumber of columns, followed by the matrix data; here is an example of a 2 by 3matrix input file:2   31 -1   02    0   1
Although the examples here use integers as elements of matrices, your codeshould allow matrices to contain doubles.

Here is an example of the output expected:

Matrix A =    1   -1    0    2    1    1    0   -1    1

Matrix B =    1   -2    1    0    1    0    1   -1    1A + B =    2   -3    1    2    2    1    1   -2    2A - B =    0    1   -1    2    0    1   -1    0    0A * B =    1   -3    1    3   -4    3    1   -2    1

Please add comments if you can even to functions and as stated in the main text needs to be C++, I will be using visual studio to use the code also no c language!!

Thanks

Explanation / Answer

#include<iostream>

using namespace std;

main()

{

   int m, n, c, d, p,q,first[6][6], second[6][6], sum[6][6],sub[6][6],mul[6][6];

cout << "Enter the number of rows and columns of matrix1";

   cin >> m >> n;

cout << "Enter the number of rows and columns of matrix2";

   cin >> p >> q;

//reading matrixA

cout << "MatrixA: ";

for ( c = 0 ; c < m ; c++ )

      for ( d = 0 ; d < n ; d++ )

         cin >> first[c][d];

//reading matrixB

cout << "MatrixB: ";

for ( c = 0 ; c < m ;c++ )

      for ( d = 0 ; d < n ; d++ )

            cin >> second[c][d];

//checking number of rows and columns are equal or not

if((m==p)&&(n==q))

{

for ( c = 0 ; c < m ; c++ )

      for ( d = 0 ; d < n ; d++ )

         sum[c][d] = first[c][d] + second[c][d];

//subtraction of two matrixes

   for ( c = 0 ; c < m ; c++ )

      for ( d = 0 ; d < n ; d++ )

         sub[c][d] = first[c][d] -second[c][d];

cout << "A+B:";

for ( c = 0 ; c < m ; c++ )

   {

      for ( d = 0 ; d < n ; d++ )

         cout << sum[c][d] << " ";

   }

cout << "A-B:";

for ( c = 0 ; c < m ; c++ )

   {

      for ( d = 0 ; d < n ; d++ )

         cout << sub[c][d] << " ";

   }

}

else

{

cout << "A+B is not possible";

cout << "A-B is not possible";

}

//checking columns of A equals to rows of B

if(n==p)

    {

       

cout<<"A*B";

        for(i=0;i<m;++i)

        {

            for(j=0;j<q;++j)

            {

                c[i][j]=0;

                    for(k=0;k<n;++k)

                    c[i][j]=c[i][j]+(a[i][k]*b[k][j]);

                cout<<c[i][j]<<" ";

        }

           }

    }

    else

        cout<<"A*B is not possibile";

return 0;

}