Hello dear programmers! Do you think it's hard to multiply matrices in c++? I don't think so. In this post, I will explain 3x3 matrix multiplication in c++ in a very simple way. It will help you enhance your understanding of arrays and loops. These concepts seem too scary to most programming students but after understanding matrix multiplication, they will enjoy arrays and loops. Now let's understand matrix multiplication in c++.
Steps to writing 3x3 matrix multiplication program in c++:
- Declare three 2d arrays with an order of 3x3.
- Input all elements of the first matrix.
- Input all the elements of the second matrix.
- Display the first and second matrices to the user.
- Multiply and calculate the product of the two matrices.
- Display the product to the user.
Let's discuss each of these steps in detail now.
How to declare 2d arrays with an order of 3x3?
- Declare "first" 2d array to store the first matrix.
- Declare "second" 2d array to store the second matrix.
- Declare "result" 2d array to store the result of multiplication.
- The order of all the 2d arrays will be 3x3.
//declaring arrays int first[3][3]; int second[3][3]; int result[3][3];
How to take input of the first matrix?
- To take input in the "first" 2d array we use nested for loops with loop counter variables "i" and "j".
- The outer "i" loop will represent the row number in the order of the matrix.
- And the inner "j" loop will represent the column number.
- Use cin statement to take input at a specific index "first[i][j]" inside the nested loop.
// input first matrix. cout << endl << "|Input Matrix a|" << "\n\n"; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { cout << "Enter a" << i + 1 << j + 1 << " :\t "; cin >> first[i][j]; } }
How to take input of the second matrix?
- Now we will take input in the "second" 2d array again using nested loops.
- This time take input in the "second[i][j]" 2d array using the same method.
// input second matrix second. cout << endl << "|Input Matrix b|" << "\n\n"; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { cout << "Enter b " << i + 1 << j + 1 << " :\t "; cin >> second[i][j]; } }
How to display entered elements of first and second 2d arrays?
- We will again use nested loops to display elements.
- Write an outer for loop with loop counter "i". It is responsible for the traversal of rows.
- Now write one inner nested for loop with loop counter "j". This loop will be responsible to print elements of the "first" 2d array.
- After this loop, write an escape sequence statement to print a tab.
- Now write another inner nested loop to display the elements of the "second" 2d array.
- At the end of this loop, write the end line statement.
//displaying matrix first and second cout << "|Entered Matrix a and b|" << "\n\n"; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { cout << first[i][j] << " "; } cout << "\t"; for (int j = 0; j < 3; ++j) { cout << second[i][j] << " "; } cout << endl; }
Also read: Complete detail of loops in c++ .
Also read: Complete detail of Arrays in c++.
How to multiply two matrices in c++?
- We need three nested loops to multiply two matrices.
- The outer loop with counter i is responsible to traverse rows.
- The inner nested loop with counter j is responsible to traverse columns.
- We need a third nested loop with counter k to multiply rows of the first matrix with columns of the second.
- We need a sum variable to store the sum of multiplication.
- After the execution of "k" loop, we can store the value of sum variable in the result 2d array.
- After completion of all three loops, we will have our answer of multiplication in the result array.
// Multiplying matrix first and second for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { int sum = 0; for (int k = 0; k < 3; ++k) { sum += first[i][k] * second[k][j]; } result[i][j] = sum; } }
How to display multiplication results to the user?
- Traverse the "result" 2d array using nested for loops.
- As we already explained the outer loop will represent the row number and the inner nested loop will represent the column number. i.e. result[i][j].
- Print the result using the cout statement.
// Displaying the result array. cout << endl << "|Resultant Matrix| " << "\n\n"; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { cout << " " << result[i][j]; } cout << endl; }
C++ program to multiply two 3x3 matrices:
Now let's see the complete code of all the steps to develop a better
understanding.
#include <iostream> using namespace std; int main() { //declaring arrays int first[3][3]; int second[3][3]; int result[3][3]; // input first matrix. cout << endl << "|Input Matrix a|" << "\n\n"; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { cout << "Enter a" << i + 1 << j + 1 << " :\t "; cin >> first[i][j]; } } // input second matrix. cout << endl << "|Input Matrix b|" << "\n\n"; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { cout << "Enter b " << i + 1 << j + 1 << " :\t "; cin >> second[i][j]; } } //displaying matrix first and second cout << "|Entered Matrix a and b|" << "\n\n"; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { cout << first[i][j] << " "; } cout << "\t"; for (int j = 0; j < 3; ++j) { cout << second[i][j] << " "; } cout << endl; } // Multiplying matrix first and second for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { int sum = 0; for (int k = 0; k < 3; ++k) { sum += first[i][k] * second[k][j]; } result[i][j] = sum; } } // Displaying the result array. cout << endl << "|Resultant Matrix| " << "\n\n"; for (int i = 0; i < 3; ++i) { for (int j = 0; j < 3; ++j) { cout << " " << result[i][j]; } cout << endl; } return 0; }
The output of the program:
Here is the output of 3x3 matrix multiplication in c++.
Recommended: Fibonacci series in c++.
Recommended: Transpose of matrix in c++.
Now let's conclude the article here. We will meet with another interesting c++ program soon. Thanks for visiting our site and reading our content.
0 Comments