Program to multiply to matrices
Multiplying matrices is bit trickier. Firstly, you will just increase matrices if the dimensions “ set ”; the second concept ( columns) of the opening matrix has to face the initial concept ( rows) of the second matrix, or you can’ ’t multiply them. Remember it like that internal dimensions have to face, and the leading dimensions of the original array are the outer dimensions. Today, get’s do a real-life case to see how this multiplication works. Get’s say we need to get the last grades for 3 daughters, and we know what their averages are for exams, tasks, homework, and quizzes. We also know that tests represent 40 percent of the level, projects 15 percent, homework 25 percent, and quizzes 20% .
Q) write a program to multiply two matrices?
sol)
code:-
code:-
#include <iostream> | |
using namespace std; | |
void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond); | |
void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int multResult[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond); | |
void display(int mult[][10], int rowFirst, int columnSecond); | |
int main() | |
{ | |
int firstMatrix[10][10], secondMatrix[10][10], mult[10][10], rowFirst, columnFirst, rowSecond, columnSecond, i, 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[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond) | |
{ | |
int i, j; | |
cout << endl << "Enter elements of matrix 1:" << endl; | |
for(i = 0; i < rowFirst; ++i) | |
{ | |
for(j = 0; j < columnFirst; ++j) | |
{ | |
cout << "Enter elements a"<< i + 1 << j + 1 << ": "; | |
cin >> firstMatrix[i][j]; | |
} | |
} | |
cout << endl << "Enter elements of matrix 2:" << endl; | |
for(i = 0; i < rowSecond; ++i) | |
{ | |
for(j = 0; j < columnSecond; ++j) | |
{ | |
cout << "Enter elements b" << i + 1 << j + 1 << ": "; | |
cin >> secondMatrix[i][j]; | |
} | |
} | |
} | |
void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int mult[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond) | |
{ | |
int i, j, k; | |
// Initializing elements of matrix mult to 0. | |
for(i = 0; i < rowFirst; ++i) | |
{ | |
for(j = 0; j < columnSecond; ++j) | |
{ | |
mult[i][j] = 0; | |
} | |
} | |
// Multiplying matrix firstMatrix and secondMatrix and storing in array mult. | |
for(i = 0; i < rowFirst; ++i) | |
{ | |
for(j = 0; j < columnSecond; ++j) | |
{ | |
for(k=0; k<columnFirst; ++k) | |
{ | |
mult[i][j] += firstMatrix[i][k] * secondMatrix[k][j]; | |
} | |
} | |
} | |
} | |
void display(int mult[][10], int rowFirst, int columnSecond) | |
{ | |
int i, j; | |
cout << "Output Matrix:" << endl; | |
for(i = 0; i < rowFirst; ++i) | |
{ | |
for(j = 0; j < columnSecond; ++j) | |
{ | |
cout << mult[i][j] << " "; | |
if(j == columnSecond - 1) | |
cout << endl << endl; | |
} | |
} | |
} |
Other related post:-
Shutdown someone pc using notepad trick
Let window speak to you
Create quiz game using notepad
Program to check planidrome
Find factorial using program
Program to check leap year
Program to sort an array
Program to multiply two matrixes
Program to find root of quadratic equations
Array coding interview questions
Program creating basic calculator
Program to convert Fahrenheit to Celsius or Celsius to Fahrenheit
Program to implement distance formula
Top featured feeds
Top pattern problems
Array coding interview questions
0 Comments
if you have any doubt let me know in comment