السلام عليكم ورحمة الله وبركاته
هدا البرنامج وشامل لبرنامج المصفوفات لي حجم تريد والعمليات الموجوده فيه
جمع و ضرب و محددة المصفوفة
ويجب مراعاة عدد الاعمدة والصفوف في الضرب
وان تكون مصفوفة مربعة للحصول على المحددة
اللهم انفعنا بما علمتنا
#include <iostream.h>
#include <math.h>
const int N=9;
class Matrix
{
private:
int n; //number of rows
int m; //number of columns
double a[N][N];
double det(double a[N][N],int n);
public:
Matrix(int n_, int m_);
~Matrix() {}
int getRows() {return n;}
int getColumns() {return m;}
void setMatrix();
void show();
double determ();
Matrix add(Matrix &m2);
Matrix mult(Matrix &m2);
};
Matrix::Matrix(int n_, int m_)
{
n=n_; m=m_;
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
a[j]=0;
}
}
}
void Matrix::setMatrix()
{
cout <<"Give the elements of the matrix!\n\n";
int i, j;
for(i=0;i<n;i++)
{
cout << i << ". row\n";
for(j=0;j<m;j++)
{
cout << "a["<<i<<"]["<<j<<"]=";
cin >> a[j];
}
}
}
void Matrix::show()
{
cout <<"\nThe matrix:\n";
int i, j;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
cout << a[j] << '\t';
}
cout << endl;
}
}
double Matrix::determ()
{
if(n==m) return det(a,n);
else
{
cout<<"It's not a square matrix!\n";
return 0;
}
}
double Matrix::det(double a[N][N], int n)
{
if(n==1) return a[0][0];
int k, i, j, x, y;
double m[N][N];
double s=0;
for(k=0;k<n;k++)
{
x=0;
for(i=1;i<n;i++)
{
y=0;
for(j=0;j<n;j++)
{
if(j!=k)
{
m[x][y]=a[j];
y++;
}
}
x++;
}
s+=pow(-1,k)*a[0][k]*det(m,n-1);
}
return s;
}
Matrix Matrix::add(Matrix &m2)
{
Matrix m3(n,m);
if((n!=m2.n)||(m!=m2.m))
{
cout << "The matrices are not the same size!\n";
return m3;
}
else
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
m3.a[j]=a[j]+m2.a[j];
}
}
}
return m3;
}
Matrix Matrix::mult(Matrix &m2)
{
int w=m2.getColumns();
Matrix m3(n,w);
if((m!=m2.getRows()))
{
cout << "These matrices cannot be multiplied!\n";
return m3;
}
else
{
int i, j, k, s;
for(i=0;i<n;i++)
{
for(j=0;j<w;j++)
{
s=0;
for(k=0;k<m;k++)
{
s+=a[k]*m2.a[k][j];
}
m3.a[j]=s;
}
}
}
return m3;
}
void main()
{
int n,m;
cout<<"\n\nInitialization matrixe:MA(n,m)\n";
cout<<"Matrix MA(n,m)\n";
cout<<"n="; cin>>n;
cout<<"m="; cin>>m;
Matrix ma(n,m);
ma.setMatrix();
ma.show();
cout<<"\n\nThe summation of two matrices:X(n,m)+Y(n,m)\n";
cout<<"Matrix X(n,m)\n";
cout<<"n="; cin>>n;
cout<<"m="; cin>>m;
Matrix x(n,m);
x.setMatrix();
x.show();
cout<<"\nMatrix Y(n,m)\n";
cout<<"n="; cin>>n;
cout<<"m="; cin>>m;
Matrix y(n,m);
y.setMatrix();
y.show();
cout<<"The sum of matrices is \n";
x.show();
cout<<"and\n";
y.show();
cout<<"The result is\n";
x.add(y).show();
cout<<"\n\nThe multiplication of two matrices:H(n,m)*Z(n,m)\n";
cout<<"Matrix H(n,m)\n";
cout<<"n="; cin>>n;
cout<<"m="; cin>>m;
Matrix h(n,m);
h.setMatrix();
h.show();
cout<<"Matrix Z(n,m)\n";
cout<<"n="; cin>>n;
cout<<"m="; cin>>m;
//cout<<"w="; cin>>w;
Matrix z(n,m);
z.setMatrix();
z.show();
cout<<"The multiple of matrices is\n";
h.show();
cout<<"and\n";
z.show();
cout<<"The result is\n";
h.mult(z).show();
cout<<"\n\nThe determianenate of matrix B(n,m)\n";
cout<<"n="; cin>>n;
cout<<"m="; cin>>m;
Matrix b(n,m);
b.show();
b.setMatrix();
b.show();
cout <<"\det(b)="<< b.determ()<<"\n";
cout<<"\n\nThe determianenate of matrix A(n,n)\n";
cout<<"n="; cin>>n;
Matrix a(n,n);
a.show();
a.setMatrix();
a.show();
cout << "\ndet(a)="<<a.determ()<<"\n";
}