/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <iostream>
using namespace std;
// تعريف الصنف مصفوفة
class Matrix {
// تعريف المتغيرات الأعضاء
public:
int row_size, col_size;
public:
int **array2d;
// constructor 1
public:
Matrix() {
cout << "Constructor 1 is called" << endl;
this->row_size = 3;
this->col_size = 3;
this->array2d = new int *[this->row_size];
for (int i = 0; i < this->row_size; i++) {
this->array2d[i] = new int[this->col_size];
}
}
// constructor 2
public:
Matrix(int row_size, int col_size) {
cout << "Constructor 2 is called" << endl;
this->row_size = row_size;
this->col_size = col_size;
this->array2d = new int *[this->row_size];
for (int i = 0; i < this->row_size; i++) {
this->array2d[i] = new int[this->col_size];
}
}
// destructor
public:
~Matrix() {
cout << "\nDestructor is called" << endl;
for (int i = 0; i < this->row_size; ++i)
delete[] this->array2d[i];
delete[] this->array2d;
}
// دالة مساعدة لقراءة عناصر المصفوفة من المستخدم
void read_els() {
for (int i = 0; i < this->row_size; ++i) {
for (int j = 0; j < this->col_size; ++j) {
cout << "Enter El: [" << i << "][" << j << "]: " << endl;
cin >> this->array2d[i][j];
}
}
}
// دالة مساعدة لطباعة عناصر المصفوفة
void print_els() {
for (int i = 0; i < this->row_size; ++i) {
for (int j = 0; j < this->col_size; ++j) {
printf("%d ", this->array2d[i][j]);
}
printf("\n");
}
};
// دالة لجمع مصفوفتين و ترجع الناتج في مصفوفة
Matrix add(Matrix *mat) {
Matrix m(this->row_size, this->col_size);
for (int i = 0; i < m.row_size; ++i) {
for (int j = 0; j < m.col_size; ++j) {
m.array2d[i][j] = this->array2d[i][j] + mat->array2d[i][j];
}
}
return m;
}
// دالة لطرح مصفوفتين و ترجع الناتج في مصفوفة
Matrix sub(Matrix *mat) {
Matrix m(this->row_size, this->col_size);
for (int i = 0; i < m.row_size; ++i) {
for (int j = 0; j < m.col_size; ++j) {
m.array2d[i][j] = this->array2d[i][j] - mat->array2d[i][j];
}
}
return m;
}
// دالة لضرب مصفوفتين و ترجع الناتج في مصفوفة
Matrix mul(Matrix *mat) {
Matrix m(this->row_size, mat->col_size);
int tmp = 0;
for(int i = 0; i < m.row_size; ++i) {
for (int j = 0; j < m.col_size; ++j) {
tmp = 0;
for (int k = 0; k < this->col_size; ++k) {
tmp += this->array2d[i][k] * mat->array2d[k][j];
}
m.array2d[i][j] = tmp;
}
}
return m;
}
// دالة لضرب مصفوفة في مُعامل سنستعملها في المضاعفة أو التكعيب أو اي كان و ترجع الناتج في مصفوفة
Matrix mul_k(int k) {
Matrix m(this->row_size, this->col_size);
for (int i = 0; i < m.row_size; ++i) {
for (int j = 0; j < m.col_size; ++j) {
m.array2d[i][j] = k * this->array2d[i][j];
}
}
return m;
}
};
int main() {
// تعريف مؤشرين لمصفوفتين
Matrix *m1 = new Matrix(2, 2);
Matrix *m2 = new Matrix(2, 2);
cout << "Enter m1 elements: " << endl;
m1->read_els(); // قراءة عناصر المصفوفة الأولى
cout << "Enter m2 elements: " << endl;
m2->read_els(); // قراءة عناصر المصفوفة الثانية
if (m1->row_size != m2->row_size || m1->col_size != m2->col_size) { // في حالة عدم تساوي حجم المصفوفتين لا يُمكننا عمل عمليتي الجمع و الطرح
cout << "can not make addition or subtraction";
} else {
cout << "m1 + m2 = " << endl;
m1->add(m2).print_els(); // القيام بعملية الجمع ثم عرض الناتج
cout << "m1 - m2 = " << endl;
m1->sub(m2).print_els(); // القيام بعملية الطرح ثم عرض الناتج
}
if (m1->col_size != m2->row_size) { // عرض رسالة في حالة عدم إمكانية عمل عملية الضرب
cout << "Error! column of first matrix not equal to row of second.";
} else {
cout << "m1 X m2 = " << endl;
m1->mul(m2).print_els(); // القيام بعملية الضرب ثم عرض الناتج
}
cout << "2 X m1 = " << endl;
m1->mul_k(2).print_els(); // القيام بعملية المضاعفة ثم عرض الناتج
delete m1; // إستدعاء المدمر لتنظيف الذاكرة و مسح الحيز المحجوز من طرف المؤشر الأول
delete m2; // إستدعاء المدمر لتنظيف الذاكرة و مسح الحيز المحجوز من طرف المؤشر الثاني
}