222k views
3 votes
make 2 dimensional arryas, ask for the rows and columns, then enter the values and then multiply both arrays.

1 Answer

1 vote

Answer:

The program of this question can be given as:

Program:

#include <iostream> //header file.

using namespace std;

int main() //define main method.

{

int First_matrix[10][10], Second_matrix[10][10], multiplication[10][10], rows, columns, i, j, k; //define variable.

cout<<"Enter the rows of matrix:";

cin>>rows; //insert rows.

cout<<"Enter the columns of matrix:";

cin>>columns; //insert columns

cout<<"Enter elements of first matrix:"<< endl; //inserting elements of second matrix

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

{

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

{

cin >> First_matrix[i][j];

}

}

cout<< "Enter elements of second matrix:"<< endl; //inserting elements of second matrix

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

{

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

{

cin >> Second_matrix[i][j];

}

}

// multiplication of matrix.

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

{

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

{

multiplication[i][j]=0;

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

{

multiplication[i][j] =multiplication[i][j] +First_matrix[i][k] * Second_matrix[k][j];

}

}

}

// Displaying matrix.

cout<< "First Matrix: " << endl;

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

{

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

{

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

}

cout<<endl;

}

cout<< "Second Matrix: "<<endl;

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

{

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

{

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

}

cout<<endl;

}

//Displaying multiplication of matrix.

cout << endl << "multiplication of Matrix: " << endl;

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

{

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

{

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

}

cout<<endl;

}

return 0;

}

Output:

Enter the rows of matrix: 2

Enter the columns of matrix:2

Enter elements of first matrix: 1

2

4

3

Enter elements of second matrix:5

4

3

1

First Matrix:

1 2

4 3

Second Matrix:

5 4

3 1

multiplication of Matrix:

11 6

29 19

Step-by-step explanation:

In the above matrix (2-dimensional array) program firstly we insert the rows and columns for creating a user-defined matrix. Then we insert the first matrix by using a loop. In this loop, we used row and column that is inserted by the user. Similarly, we insert the second matrix. then we multiply the matrix. In the multiplication, we define 3 loop that is (i,j,k). first loop(i) work on the rows and the second and third loop(j,k) work on the columns. In the last first we print matrix (first, second). Then we print there multiplication.

User Derwin
by
4.9k points