CPP Program multiply the two matrices

 In this program, discussion about the multiple the two matrices in the cpp programming language. But you have enough knowledge to understand the whole program. If you do not have knowledge then visit

c++ 2D arrays

c++ loops

c++ pointers


Code:


#include<iostream>

using namespace std;

int main()

{


int firstmat[10][10], secondmat[10][10], multi[10][10],i, j,firstrow,firstcol,secondrow,secondcol;


cout << "Enter rows and column for first matrix: ";

cin >> firstrow >> firstcol;


cout << "Enter rows and column for second matrix: ";

cin >> secondrow >> secondcol;

cout<< "Enter elements of matrix 1:" << endl;

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

{

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

{

cout << "Enter elements a"<< i + 1 << j + 1 << ": ";

cin >> firstmat[i][j];

}

}


cout << endl << "Enter elements of matrix 2:" << endl;

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

{

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

{

cout << "Enter elements b" << i + 1 << j + 1 << ": ";

cin >> secondmat[i][j];

}

}



int  k;


// Initializing elements of matrix multi to 0.

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

{

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

{

multi[i][j] = 0;

}

}


// multiplying matrix firstmat and secondmat and storing in array multi.

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

{

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

{

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

{

multi[i][j] += firstmat[i][k] * secondmat[k][j];

}

}

}

cout << "Output Matrix" << endl;

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

{

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

{

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

if(j == secondcol - 1)

cout << endl << endl;

}

}

}

Output:

Enter rows and column for first matrix: 2

2

Enter rows and column for second matrix:

2

2

Enter elements of matrix 1:

Enter elements a11: 3

Enter elements a12: 3

Enter elements a21: 3

Enter elements a22: 3


Enter elements of matrix 2:

Enter elements b11: 3

Enter elements b12: 3

Enter elements b21: 3

Enter elements b22: 3

Output Matrix

18 18


18 18


Comments

Popular posts from this blog

how to cout in cpp

Flutter layout

input by cin in c++