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
Akash Electrical Drives 2016-2017
Category: Question PapersJuly 17, 2022
Advance Control System Unit 4 Part 1
Category: NotesJuly 17, 2022
Applied Maths - I (Beta Gamma Function)
Category: NotesJuly 17, 2022
Thevenin's Theorem Experiment
Category: Practical FilesJuly 16, 2022
Sensor and Transducers
Category: NotesJuly 17, 2022
Electrical drives file
Category: Practical FilesJuly 17, 2022
BBA Entrepreneurship Development Notes Unit 3
Category: NotesJuly 17, 2022
Management Accounting Notes PDF Unit 4
Category: NotesJuly 17, 2022
C program Boundary Value Analysis - Nature of roots of a quadratic equation
Category: Practical FilesJuly 16, 2022
IPU B.Tech First Year Syllabus
Category: SyllabusJuly 17, 2022