Matrix Arithmetic
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() {
// Declaring and initializing a 3x4 matrix
int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
// Outputting the matrix
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 4; ++j) {
cout << matrix[i][j] << " ";
cout << endl;
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; }
Document created by Sawab.S
2
Subtraction
#include<iostream>
using namespace std;
int main() {
int r, c, a[100][100], b[100][100], difference[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];
}
// 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;
}
Document created by Sawab.S
3
Multiplication
#include<iostream>
using namespace std;
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;
// Check if multiplication is possible
if (c1 != r2) {
cout << "Error: The number of columns in the first matrix must be equal
to the number of rows in the second matrix for multiplication." << endl;
return 1;
}
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];
}
// Initialize product matrix elements to 0
for (i = 0; i < r1; ++i)
for (j = 0; j < c2; ++j)
product[i][j] = 0;
// 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;
}
Document created by Sawab.S
4
Division
#include <iostream>
#include <vector>
// Function to perform element-wise division of two matrices
std::vector<std::vector<double>> matrixDivision(const std::vector<std::vector<double>>& A, const
std::vector<std::vector<double>>& B) {
int rows = A.size();
int cols = A[0].size();
std::vector<std::vector<double>> result(rows, std::vector<double>(cols, 0.0));
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
result[i][j] = A[i][j] / B[i][j];
}
}
return result;
}
// Function to print a matrix
void printMatrix(const std::vector<std::vector<double>>& matrix) {
for (const auto& row : matrix) {
for (double element : row) {
std::cout << element << " ";
}
std::cout << std::endl;
}
}
int main() {
int size;
std::cout << "Enter the size of the square matrices: ";
std::cin >> size;
// Input for Matrix A
std::cout << "Enter elements for Matrix A:" << std::endl;
std::vector<std::vector<double>> A(size, std::vector<double>(size, 0.0));
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
std::cout << "A[" << i << "][" << j << "]: ";
std::cin >> A[i][j];
}
}
// Input for Matrix B
std::cout << "Enter elements for Matrix B:" << std::endl;
std::vector<std::vector<double>> B(size, std::vector<double>(size, 0.0));
for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
std::cout << "B[" << i << "][" << j << "]: ";
std::cin >> B[i][j];
}
}
// Perform matrix division
std::vector<std::vector<double>> result = matrixDivision(A, B);
// Display the result
std::cout << "Result of A / B:" << std::endl;
printMatrix(result);
return 0;}
Document created by Sawab.S