C++ Matrix Multiplication using OOP
matrix_multiplication_oop.txt
File size: 1.35 KB
File content type: text/plain
Category: Practical Files
Subject: Object Oriented Programming
#include<iostream>
using namespace std;
class Matrix {
private:
int a[10][10], row, col;
public:
Matrix(int row = 10, int col = 10) {
this->row = row;
this->col = col;
}
void getData() {
cout << "Enter " << (row * col) << " elements:\n";
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cin >> a[i][j];
}
}
}
void print() {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
cout << a[i][j] << "\t";
}
cout << endl;
}
}
// Multiply matrix of current instance with the argument matrix
Matrix multiply(Matrix b) {
if (col != b.row) {
cout << "Matrices cannot be multiplied. Argument Matrix returned\n";
return b;
}
Matrix ans(row, b.col);
for (int i = 0; i < row; i++) {
for (int j = 0; j < b.col; j++) {
ans.a[i][j] = 0;
for (int k = 0; k < b.row; k++) {
ans.a[i][j] += a[i][k] * b.a[k][j];
}
}
}
return ans;
}
};
int main() {
Matrix a(2, 3);
a.getData();
a.print();
Matrix b(3, 2);
b.getData();
b.print();
Matrix c = a.multiply(b);
c.print();
return 0;
}
Related searchs: Matrix Operations in C++ using class
Last Updated: July 16, 2022
Related
Business Environment Notes Unit 2
Category: NotesJuly 17, 2022
IPU Sales and Distribution Management Notes Unit 4
Category: NotesJuly 17, 2022
EHVACDC Unit 2
Category: NotesJuly 17, 2022
Paging and Cordless Systems Notes PDF
Category: NotesJuly 16, 2022
Unit 2 Phase Rule Handwritten Notes PDF
Category: NotesJuly 16, 2022
SNT Unit 2
Category: NotesJuly 17, 2022
Intro Technical Communication notes
Category: NotesJuly 17, 2022
Fundamentals of Computing Question Papers Akash PDF
Category: Question PapersJuly 16, 2022
FAA Notes BBA Unit 2
Category: NotesJuly 16, 2022
[MPMC] Microprocessor and Microcontrollers Aakash
Category: Question PapersJuly 16, 2022