Download Our Beta Android App And Help Us Build Awesome Stuff!  Download Now.

Matrix Multiplication (using Functions) - C++

Matrix_Multiplication.txt
File size: 1.99 KB

File content type: text/plain

Category: Notes and Practical Files

Course: B.Tech.

Semester: 4

College: Govind Ballabh Pant Engineering College(GBPEC)

#include <iostream>

using namespace std ;


void input( int mat[][100], int row, int col )
{
      for(int i=0; i<row; i++)
      {
            for(int j=0; j<col; j++)
            {
                  cout<<"Enter the ["<<i+1<<"]["<<j+1<<"] element  : " ;
                  cin>>mat[i][j] ;
            }
      }
}


void mult( int a[][100],int b[][100],int c[][100], int row, int col, int same )
{
      for(int i=0; i<row; i++)
      {
            for(int j=0; j<col; j++)
            {
                  c[i][j] = 0 ;
                  for(int k=0; k<same; k++)
                        c[i][j] = c[i][j] + (a[i][k]*b[k][j]) ;
            }
      }
}


void print( int mat[][100], int row, int col )
{
      for(int i=0; i<row; i++)
      {
            for(int j=0; j<col; j++)
            {
                  cout<<mat[i][j]<<" " ;
            }
            cout<<endl ;
      }
}


int main()
{
      //Declarations
      int a[100][100], b[100][100], c[100][100] ;
      int row1, col1, row2, col2 ;

      //Input Size
      cout<<"\n Enter Row and Column for first matrix : \n" ;
      cin>>row1>>col1 ;
      cout<<"\n Enter Row and Column for second matrix : \n" ;
      cin>>row2>>col2 ;

      //Multiplication NOT POSSIBLE
      if( col1 != row2 )
      {
            cout<<"\n Matrix Multiplication NOT POSSIBLE \n" ;
            return 0 ;
      }

      //Input Data of Matrices
      cout<<"\n Enter Data for first matrix - \n" ;
      input( a, row1, col1 ) ;

      cout<<"\n Enter Data for second matrix - \n" ;
      input( b, row2, col2 ) ;

      
      //Multiplying Matrices to get Matrix C
      mult( a, b, c, row1, col2, row2 ) ;

      //Ouput original matrices
      cout<<"\n First matrix :- \n" ;
      print( a, row1, col1 ) ;
      cout<<"\n Second matrix :- \n" ;
      print( b, row2, col2 ) ;

      //Output Final Matrix
      cout<<"\n Resultant Matrix :- \n" ;
      print( c, row1, col2 ) ;

      return 0;
}

Added by StAn

Comments