0% found this document useful (0 votes)
16 views

Oops15.pdf

The document describes a C++ program that implements a matrix class capable of handling integer matrices of various dimensions. It includes functionalities for inserting elements, retrieving specific elements, and overloading operators for addition, multiplication, and comparison of matrices. The main function demonstrates the creation of two matrices, their operations, and outputs the results.

Uploaded by

Akashdeep Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Oops15.pdf

The document describes a C++ program that implements a matrix class capable of handling integer matrices of various dimensions. It includes functionalities for inserting elements, retrieving specific elements, and overloading operators for addition, multiplication, and comparison of matrices. The main function demonstrates the creation of two matrices, their operations, and outputs the results.

Uploaded by

Akashdeep Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Assignment 15

Program - Write a C++ program for developing a matrix class which can handle
integer matrices of different dimensions. Also overload the operator for
addition, multiplication & comparison of matrices.

Code
#include <iostream>
#include <vector>
#include <stdexcept>
using namespace std;
class Matrix {
private:
vector<int> elements;
int rows;
int cols;
public:
Matrix(int r, int c) {
this -> rows = r;
this -> cols = c;
elements.resize(rows * cols);
}
void insertElements() {
int x;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << "Enter the element at Index (" << i << "," << j << ") : ";
cin >> x;
elements[i * cols + j] = x; // Set the value at computed index
}
}
}

void retrieval(int r, int c) const {


cout << "The Element at given index (" << r << "," << c << ") is : " << elements[r * cols + c] <<
endl;
}
Matrix operator+(const Matrix& m) const {
if (this->rows != m.rows || this->cols != m.cols) {
throw invalid_argument("Matrix dimensions must match for addition.");
}
Matrix result(this->rows, this->cols);
for (int i = 0; i < this->rows; i++) {
for (int j = 0; j < this->cols; j++) {
result.elements[i * cols + j] = this->elements[i * cols + j] + m.elements[i * cols + j];
}
}
return result;
}

Akashdeep Singh 23124009


Matrix operator*(const Matrix& m) const {
if (this->cols != m.rows) {
throw invalid_argument("Matrix dimensions must match for multiplication.");
}
Matrix result(this->rows, m.cols);
for (int i = 0; i < this->rows; i++) {
for (int j = 0; j < m.cols; j++) {
result.elements[i * m.cols + j] = 0;
for (int k = 0; k < this->cols; k++) {
result.elements[i * m.cols + j] += this->elements[i * this->cols + k] * m.elements[k *
m.cols + j];
}
}
}
return result;
}
bool operator==(const Matrix& m) const {
if (this->rows != m.rows || this->cols != m.cols) {
return false;
}
for (int i = 0; i < this->rows * this->cols; i++) {
if (this->elements[i] != m.elements[i]) {
return false;
}
}
return true;
}
void print() const {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << elements[i * cols + j] << " ";
}
cout << endl;
}
}
};
int main() {
int r, c;
cout << "Enter the number of rows: ";
cin >> r;
cout << "Enter the number of columns: ";
cin >> c;

Matrix m1(r, c);


cout << "Enter elements for the first matrix:" << endl;
m1.insertElements();
Matrix m2(r, c);
cout << "Enter elements for the second matrix:" << endl;
m2.insertElements();
cout << "First Matrix:" << endl;
m1.print();

Akashdeep Singh 23124009


cout << "Second Matrix:" << endl;
m2.print();
Matrix sum = m1 + m2;
cout << "Sum of the two matrices:" << endl;
sum.print();

Matrix product = m1 * m2; // This will work only if cols of m1 == rows of m2


cout << "Product of the two matrices:" << endl;
product.print();

if (m1 == m2) {
cout << "The matrices are equal." << endl;
} else {
cout << "The matrices are not equal." << endl;
}
return 0;
}

Output

Akashdeep Singh 23124009

You might also like