 # C++ Programming Code Examples

## C++ > Arrays and Matrices Code Examples

### Program to Multiply two Matrices by Passing Matrix to Function

``` Program to Multiply two Matrices by Passing Matrix to Function In this example, you'll learn to multiply two matrices and display it using user defined function. This program asks user to enter the size of the matrix (rows and columns). Then, it asks the user to enter the elements of two matrices and finally it multiplies two matrix and displays the result. To perform this task three functions are made: To take matrix elements from user To multiply two matrix To display the resultant matrix after multiplication #include <iostream> using namespace std; void enterData(int firstMatrix[], int secondMatrix[], int rowFirst, int columnFirst, int rowSecond, int columnSecond); void multiplyMatrices(int firstMatrix[], int secondMatrix[], int multResult[], int rowFirst, int columnFirst, int rowSecond, int columnSecond); void display(int mult[], int rowFirst, int columnSecond); int main() { int firstMatrix, secondMatrix, mult, rowFirst, columnFirst, rowSecond, columnSecond, x, j, k; cout << "Enter rows and column for first matrix: "; cin >> rowFirst >> columnFirst; cout << "Enter rows and column for second matrix: "; cin >> rowSecond >> columnSecond; // If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again. while (columnFirst != rowSecond) { cout << "Error! column of first matrix not equal to row of second." << endl; cout << "Enter rows and column for first matrix: "; cin >> rowFirst >> columnFirst; cout << "Enter rows and column for second matrix: "; cin >> rowSecond >> columnSecond; } // Function to take matrices data enterData(firstMatrix, secondMatrix, rowFirst, columnFirst, rowSecond, columnSecond); // Function to multiply two matrices. multiplyMatrices(firstMatrix, secondMatrix, mult, rowFirst, columnFirst, rowSecond, columnSecond); // Function to display resultant matrix after multiplication. display(mult, rowFirst, columnSecond); return 0; } void enterData(int firstMatrix[], int secondMatrix[], int rowFirst, int columnFirst, int rowSecond, int columnSecond) { int x, j; cout << endl << "Enter elements of matrix 1:" << endl; for(x = 0; x < rowFirst; ++x) { for(j = 0; j < columnFirst; ++j) { cout << "Enter elements a"<< x + 1 << j + 1 << ": "; cin >> firstMatrix[x][j]; } } cout << endl << "Enter elements of matrix 2:" << endl; for(x = 0; x < rowSecond; ++x) { for(j = 0; j < columnSecond; ++j) { cout << "Enter elements b" << x + 1 << j + 1 << ": "; cin >> secondMatrix[x][j]; } } } void multiplyMatrices(int firstMatrix[], int secondMatrix[], int mult[], int rowFirst, int columnFirst, int rowSecond, int columnSecond) { int x, j, k; // Initializing elements of matrix mult to 0. for(x = 0; x < rowFirst; ++x) { for(j = 0; j < columnSecond; ++j) { mult[x][j] = 0; } } // Multiplying matrix firstMatrix and secondMatrix and storing in array mult. for(x = 0; x < rowFirst; ++x) { for(j = 0; j < columnSecond; ++j) { for(k=0; k<columnFirst; ++k) { mult[x][j] += firstMatrix[x][k] * secondMatrix[k][j]; } } } } void display(int mult[], int rowFirst, int columnSecond) { int x, j; cout << "Output Matrix:" << endl; for(x = 0; x < rowFirst; ++x) { for(j = 0; j < columnSecond; ++j) { cout << mult[x][j] << " "; if(j == columnSecond - 1) cout << endl << endl; } } } ``` 