Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Matrix multiplication.cpp #351

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions C++/Matrix multiplication.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <bits/stdc++.h>
using namespace std;

// mat1[R1][C1] and mat2[R2][C2]
#define R1 2 // number of rows in Matrix-1
#define C1 2 // number of columns in Matrix-1
#define R2 2 // number of rows in Matrix-2
#define C2 2 // number of columns in Matrix-2

void mulMat(int mat1[][C1], int mat2[][C2])
{
int rslt[R1][C2];

cout << "Multiplication of given two matrices is:\n";

for (int i = 0; i < R1; i++) {
for (int j = 0; j < C2; j++) {
rslt[i][j] = 0;

for (int k = 0; k < R2; k++) {
rslt[i][j] += mat1[i][k] * mat2[k][j];
}
cout << rslt[i][j] << "\t";
}
cout << endl;
}
}

// Driver code
int main()
{
int mat1[R1][C1] = { { 1, 3 },
{ 2, 4 } };

int mat2[R2][C2] = { { 1, 2 },
{ 3, 4 } };

if (C1 != R2) {
cout << "The number of columns in Matrix-1 must "
"be equal to the number of rows in "
"Matrix-2"
<< endl;
cout << "Please update MACROs according to your "
"array dimension in #define section"
<< endl;

exit(EXIT_FAILURE);
}
// Function call
mulMat(mat1, mat2);
return 0;
}
Loading