C++ (Matrices)
C++ (Matrices)
in C++
1 Addition (➕)
2 Subtraction (➖)
3 Multiplication ( ✖ )
4 Division (➗)
This template
Document
hascreated
been created
by Sawab.S
by Slidesgo
Matrix
A Sample
using namespace std;
#include <iostream>
int main() {
int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
};
return 0;
}
“ Document created by Sawab.S ”
1
Addition
#include<iostream>
using namespace std;
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
cout << "Enter number of rows (between 1 and 100): ";
cin >> r;
cout << "Enter number of columns (between 1 and 100): ";
cin >> c;
cout << endl << "Enter elements of 1st matrix: " << endl;
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
}
cout << endl << "Enter elements of 2nd matrix: " << endl;
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> b[i][j];
}
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
sum[i][j] = a[i][j] + b[i][j];
cout << endl << "Sum of two matrix is: " << endl;
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
cout << sum[i][j] << " ";
if (j == c - 1)
cout << endl;
}
return 0; }
int main() {
int r, c, a[100][100], b[100][100], difference[100][100], i, j;
cout << endl << "Enter elements of 1st matrix: " << endl;
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
}
cout << endl << "Enter elements of 2nd matrix: " << endl;
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> b[i][j];
}
// Subtracting matrices
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
difference[i][j] = a[i][j] - b[i][j];
cout << endl << "Difference of two matrices is: " << endl;
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
cout << difference[i][j] << " ";
if (j == c - 1)
cout << endl;
}
return 0;
}
int main() {
int r1, c1, r2, c2, a[100][100], b[100][100], product[100][100], i, j, k;
cout << "Enter number of rows for the first matrix: ";
cin >> r1;
cout << "Enter number of columns for the first matrix: ";
cin >> c1;
cout << "Enter number of rows for the second matrix: ";
cin >> r2;
cout << "Enter number of columns for the second matrix: ";
cin >> c2;
cout << endl << "Enter elements of the first matrix: " << endl;
for (i = 0; i < r1; ++i)
for (j = 0; j < c1; ++j) {
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
}
cout << endl << "Enter elements of the second matrix: " << endl;
for (i = 0; i < r2; ++i)
for (j = 0; j < c2; ++j) {
cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> b[i][j];
}
// Multiply matrices
for (i = 0; i < r1; ++i)
for (j = 0; j < c2; ++j)
for (k = 0; k < c1; ++k)
product[i][j] += a[i][k] * b[k][j];
cout << endl << "Product of two matrices is: " << endl;
for (i = 0; i < r1; ++i)
for (j = 0; j < c2; ++j) {
cout << product[i][j] << " ";
if (j == c2 - 1)
cout << endl;
}
return 0;
}
return result;
}
int main() {
int size;
return 0;}