This repository has been archived by the owner on Oct 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathProgram to Multiply two Matrices by Passing Matrix to Function
121 lines (99 loc) · 3.48 KB
/
Program to Multiply two Matrices by Passing Matrix to Function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
To understand this example, you should have the knowledge of following C++ programming topics:
C++ Arrays
C++ Multidimensional Arrays
Passing Array to a Function in C++ Programming
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
Example: Multiply Matrix by Passing it to a Function
#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;
}
}
}