0% found this document useful (0 votes)
10 views5 pages

Bai 04

The document defines a Matrix class with private data members to store the matrix elements in a 2D array. It implements various constructor, destructor and member functions to initialize, input, output, add, subtract and multiply matrices. The main() function demonstrates creating Matrix objects, performing operations on them and accessing individual elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Bai 04

The document defines a Matrix class with private data members to store the matrix elements in a 2D array. It implements various constructor, destructor and member functions to initialize, input, output, add, subtract and multiply matrices. The main() function demonstrates creating Matrix objects, performing operations on them and accessing individual elements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include <iostream>

#include <cstdlib>
using namespace std;

/*
class Matrix {
private:
float ** Matran;
int row; // so hang
int col; // so cot
public:
Matrix ()
{
do{
cout << "Nhap so hang : " ; cin >> row;
cout << "Nhap so cot : " ; cin >> col;
}
while(row == 0 && col == 0);
Matran = new float *[row];
for (int i = 0 ; i < row ; ++i)
{
Matran[i] = new float [col];
for (int j = 0; j < col; ++j) {
Matran[i][j] = rand() % 100; // Random value between 0 and 99
}
}
}
Matrix(int &m , int &n) // chi dung While
{
while (m <= 0 && n <= 0)
{
cout << "Nhap so dong :" ;
cin >> m ;
cout << "Nhap so cot :" ;
cin >> n ;
}
row = m ;
col = n ;
Matran = new float *[row];
for (int i = 0 ; i < row ; ++i)
{
Matran[i] = new float [col];
for (int j = 0; j < col; ++j) {
Matran[i][j] = rand() % 100; // Random value between 0 and 99
}
}
}

~Matrix ()
{
for (int i = 0 ; i < row ; ++i)
{
delete [] Matran[i];
}
delete [] Matran;
}
void Nhap()
{
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
cin >> Matran[i][j];
}
}
}
void Xuat()
{
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
cout << Matran[i][j] << " ";
}
cout << endl;
}
}
friend Matrix operator + (const Matrix& other)
{
Matrix result(row, col);
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
result.Matran[i][j] = Matran[i][j] + other.Matran[i][j];
}
}
return result;
}
friend Matrix operator - (const Matrix& other)
{
Matrix result(row, col);
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
result.Matran[i][j] = Matran[i][j] - other.Matran[i][j];
}
}
return result;
}
friend Matrix operator * (const Matrix& other)
{
Matrix result(row, other.col);
for (int i = 0; i < row; ++i) {
for (int j = 0; j < other.col; ++j) {
result.Matran[i][j] = 0;
for (int k = 0; k < col; ++k) {
result.Matran[i][j] += Matran[i][k] * other.Matran[k][j];
}
}
}
return result;
}
float operator()(int r, int c) const {
return Matran[r][c];
}
};
*/
#include <iostream>
#include <cstdlib> // For rand() and srand()

using namespace std;

class Matrix {
private:
float **Matran;
int row; // số hàng
int col; // số cột
public:
Matrix() {
do {
cout << "Nhập số hàng: "; cin >> row;
cout << "Nhập số cột: "; cin >> col;
} while (row <= 0 || col <= 0);

Matran = new float *[row];


for (int i = 0; i < row; ++i) {
Matran[i] = new float[col];
for (int j = 0; j < col; ++j) {
Matran[i][j] = rand() % 100; // Giá trị ngẫu nhiên trong khoảng từ
0 đến 99
}
}
}

Matrix(int m, int n) {
while (m <= 0 || n <= 0) {
cout << "Nhập số hàng: "; cin >> m;
cout << "Nhập số cột: "; cin >> n;
}
row = m;
col = n;
Matran = new float *[row];
for (int i = 0; i < row; ++i) {
Matran[i] = new float[col];
for (int j = 0; j < col; ++j) {
Matran[i][j] = rand() % 100; // Giá trị ngẫu nhiên trong khoảng từ
0 đến 99
}
}
}

~Matrix() {
for (int i = 0; i < row; ++i) {
delete[] Matran[i];
}
delete[] Matran;
}

void Nhap() {
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
cout << "Nhập phần tử [" << i +1 << "][" << j +1 << "]: ";
cin >> Matran[i][j];
}
}
}

void Xuat() {
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
cout << Matran[i][j] << " ";
}
cout << endl;
}
}

Matrix operator+(const Matrix &other) {


Matrix result(row, col);
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
result.Matran[i][j] = Matran[i][j] + other.Matran[i][j];
}
}
return result;
}

Matrix operator-(const Matrix &other) {


Matrix result(row, col);
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
result.Matran[i][j] = Matran[i][j] - other.Matran[i][j];
}
}
return result;
}

Matrix operator*(const Matrix &other) {


Matrix result(row, other.col);
for (int i = 0; i < row; ++i) {
for (int j = 0; j < other.col; ++j) {
result.Matran[i][j] = 0;
for (int k = 0; k < col; ++k) {
result.Matran[i][j] += Matran[i][k] * other.Matran[k][j];
}
}
}
return result;
}

float operator()(int r, int c) const {


return Matran[r][c];
}
};
int main ()
{
int dong ,cot ;
cout << "-------------Ma tran ngau nhien -------------------" << endl;
Matrix A;
A.Xuat();

cout << "-------------Ma tran tuy chinh --------------------" << endl;


cout << "Nhap so dong: " ; cin >> dong ;
cout << "Nhap so cot " ; cin >> cot ;
cout << "Nhap gia tri tai vi tri a[i][j]\n" ;
Matrix B(dong , cot);
B.Nhap();
B.Xuat();

cout << "Nhap gia tri tai vi tri b[i][j]\n" ;


Matrix C(dong , cot);
C.Nhap();
C.Xuat();
cout << "---------------Ket qua phep cong ------------------\n";
Matrix KQ1 = B + C;
KQ1.Xuat();
cout << "---------------Ket qua phep tru ------------------\n";
Matrix KQ2 = B - C;
KQ2.Xuat();

cout << "Truy xuat den phan tu [i][j] nào? " ;


cin >> dong >> cot ;
cout << KQ2(dong -1 ,cot-1) ;
return 0;
}

You might also like