Home Blog Feed

GOC-C: OOP Matrix maniluation

Matrix is a 2D array of values. Every value has row index and column index. Two SET or RETRIEVE data or value into/from the matrix indices are the only way to do.

ITERATING OVER THE MATRIX

Iterating over the matrix means accessing the values of matrix one by one. This is done as first the values are accessed column wise thus element in every colum of first row is accessed and the process is repeated for every row. Thus every element in the matrix is accessed and can be manipulated.

READ THE VALUES OF MATRIX

Algorithm :


- READ the max no. Of rows - MAX_ROW and columns – MAX_COLUMN
- LOOP  ROW_INDEX from ZERO to MAX_ROW
  + LOOP COLUMN_INDEX from ZERO to MAX_COLUMN
    - SET MATRIX[ROW_INDEX][COLUMN_INDEX] = INPUT
  + END LOOP
- END LOOP

DISPLAY THE MATRIX

Algorithm:


- LOOP  ROW_INDEX from ZERO to MAX_ROW
  + LOOP COLUMN_INDEX from ZERO to MAX_COLUMN
    - Display MATRIX[ROW_INDEX][COLUMN_INDEX]
  + END LOOP
- END LOOP

CodeListing of Matrix Container

#include
#include
#include


class matrix
{
    int mat[10][10];
      int m,n;

      public:
         void display();
         void read();
         friend matrix add(matrix,matrix);
         friend matrix mul(matrix,matrix);



      };

void matrix::display()
{
   cout<<"\nContent of matrix:"<
     for(int i=0;i
     {   for(int j=0;j
         {   cout<<<"  ";
  }
         cout<<"\n\n";
         }//i-loop

         }

void matrix::read()
{
     cout<<"Enter the order of the matrix\t:";
     cin>>m>>n;

     cout<<"Enter the elements:"<
     for(int i=0;i
     {   for(int j=0;j
         {  cin>>mat[i][j]; }
            }//i-loop

         }


ADDING TWO MATRIX

Adding two matrix means adding the elements of matrix with corressponding indices

Algorithm:

  • LOOP ROW from ZERO to MAXROW
    • LOOP COL from ZERO to MAXCOLUMN
      • SUMMAT[ ROW ][ COL ] equal to MAT1[ ROW ][ COL ]+MAT2[ ROW ][ COL ]
    • END LOOP
  • END LOOP

CodeListing of Matrix Addition


matrix add (matrix A, matrix B)
{

       matrix temp ;

       temp.m=A.m;
       temp.n=A.n;
       for(int i=0;i
       {   for(int j=0;j
           {
    temp.mat[i][j]=A.mat[i][j]+B.mat[i][j];  }
                   }//i-loop
       return(temp);
       }

MULTIPLYING TWO MATRIX

Algorithm:

  • IF MAXROW is not equal to MAXCOLUMN,
    • THEN print matrix cannot be multiplied to output screen
  • IF MAXROW is equal MAXCOLUMN, THEN MAX = MAXROW
    • LOOP ROW from ZERO to MAX
      • LOOP COL from ZERO to MAX
        • SET MAT[ROW][COL] equal to ZERO
        • LOOP CUR from ZERO to MAX
          • MAT[ROW][COL] = MAT[ROW][COL] + MAT1[ROW][CUR]*MAT2[CUR][COL]
        • END LOOP
      • END LOOP
    • END LOOP

CodeListing of Matrix Multiplication

matrix mul (matrix A, matrix B)
{
       matrix temp ;
       temp.m=A.m;
       temp.n=A.n;
       for(int i=0;i
       {   for(int j=0;j
           {    temp.mat[i][j]=0;
                for(int k=0;k
                   temp.mat[i][j]+=A.mat[i][k]*B.mat[k][j]; } }
                   }//i-loop
       return(temp);
       }

TRANSPOSE OF A MATRIX

Transposing a matrix means making the elements of row of matrix into column elements and vice versa

Algorithm:

  • LOOP ROW from ZERO to MAXROW
    • LOOP COL from ZERO to MAXCOLUMN
      • OUTPUTMAT[ROW][COL] = INPUTMAT[COL][ROW]
    • END LOOP
  • END LOOP

CodeListing for Matrix Transposing

matrix trans(matrix A)
{
       matrix temp ;
       temp.m=A.m;
       temp.n=A.n;
       for(int i=0;i
       {   for(int j=0;j
           {   temp.mat[i][j]=A.mat[j][i];  }
                   }//i-loop
       return(temp);
       }