السلام عليكم ورحمة الله وبركاته
انا بدأت في عمل مشروع والمشروع عباره عن كلاس يضم عدد من العمليات ع المصفوفات والـ main فقط مناداة لما في داخل الكلاس
لكن الاخطاء تلازمني حاولت فيها كثيرا لكن لم اجد حل
فـ ياليت تساعدوني في ايجاد الاخطاء وتنبيهي عليها
وهذه هي تجربتي
#include <iostream>
#include<vector>
using namespace std;
class myarray{
public:
vector<int> diffrentsize;{
int x;
int y;
int z;
int w;
cout<<"enter 4 size of array"<<endl;
cin>>x;
cin>>y;
cin>>z;
cin>>w;
int *p[4];
p[0]=new int[x];
p[1]=new int[y];
p[2]=new int[z];
p[3]=new int[w];
}
vector<void>check {
if(p2.length()>x){
cout<<"the length of array is bigger than size"<<endl;
}
else
{cout<<"the length of array is fit the size"<<endl;}
}
vector<void> length {
int y=0;
for(int i=0;i<x;i++){
cin>>p;}
while(p2[0]!='\0')
{y+=1;
p2++;}
cout<<y;
}
vector<void> det{
cout<<endl<<"TO FIND DETERMINANT"<<endl;
int n = 0,val;
cout<<"enter no. of columns/rows of square matrix"<<endl;
cin>>n;
int **p;
p = new int*[n]; //make an array of ints, p points to them
for (int i = 0; i< n; i++)
{
p = new int[n]; //each pointer now points to 'n' amount of ints
}
int a = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
p[j] = a++; //assign some values
}
}
cout<<"enter the values of matrix row-wise"<<endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin>>p[j];
if (n==1)
val=p[j];
}
}
//using the formulae
if (n==2)
{val=p[0][0]*p[1][1]-p[0][1]*p[1][0];}
if (n==3)
{val=(p[0][0]*((p[1][1]*p[2][2])-(p[1][2]*p[2][1])))-
(p[0][1]*((p[1][0]*p[2][2])-(p[1][2]*p[2][0])))+
(p[0][2]*((p[1][0]*p[2][1])-(p[1][1]*p[2][0])));}
for (int i = 0; i < m; i++)
{
delete((int*)p);//free up the array of pointers, must delete each one otherwise memory will leak
}
delete [] p;
cout<<"determinant value is:";
cout<<val<<endl;
}
vector<void> transpose{
int n = 0,m=0;
cout<<"TO FIND TRANSPOSE"<<endl;
cout<<"enter no. of rows and columns of matrix"<<endl;
cin>>m>>n;
int **p;
p = new int*[m];
for (int i = 0; i< m; i++)
{
p = new int[n];
}
int a = 0;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
p[j] = a++;
}
}
cout<<"enter the numbers of matrix row-wise"<<endl;
for (int i = 0; i < m; i++) //input values
{
for (int j = 0; j < n; j++)
{
cin>>p[j];
}
}
cout<<"matrix you have entered is"<<endl;
for (int i = 0; i < m; i++) //output matrix entered
{
for (int j = 0; j < n; j++)
{
cout << p[j] << "\t";
}
cout << "\n";
}
int **q;
q = new int*[m];
cout<<endl;
for (int i = 0; i< m; i++) //create the matrix(to be transpose)
{
q = new int[n];
}
a = 0;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
q[j] = a++;
}
}
for (int i = 0; i < n; i++)//assigning corresponding values to make a transpose
{
for (int j = 0; j < m; j++)
{
q[j] = p[j];
}
}
cout<<"transpose is"<<endl;
for (int i = 0; i < n; i++) //output transpose
{
for (int j = 0; j < m; j++)
{
cout << q[j] << "\t";
}
cout << "\n";
}
for (int i = 0; i < m; i++)
{
delete((int*)q);
}
delete [] q;
}
vector<void> add{
int n = 0,m=0;
cout<<"TO FIND THE SUM"<<endl;
cout<<"enter no. of rows and columns of matrix"<<endl;
cin>>m>>n;
int **t,**q;
t = new int*[m];
for (int i = 0; i< m; i++)
{
t = new int[n];
}
int a = 0;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
t[j] = 0;
}
}
char chk='y'; //any number of matrices can be added
while (chk=='y'||chk=='Y'){
q = new int*[m];
for (int i = 0; i< m; i++)
{
q = new int[n];
}
a = 0;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
q[j] = a++;
}
}
cout<<"enter the numbers of matrix to be added"<<endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cin>>q[j];
}
}
cout<<"matrix you have entered"<<endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cout << q[j] << "\t";
}
cout << "\n";
}
for (int i = 0; i < m; i++) //addition
{
for (int j = 0; j < n; j++)
{
t[j] = t[j]+q[j];
}
}
for (int i = 0; i < m; i++)
{
delete((int*)q);
}
delete [] q;
cout<<"wish to add another matrix to this? y/n"<<endl;
cin>>chk;
}
cout<<"sum is "<<endl;
for (int i = 0; i < m; i++) //output sum
{
for (int j = 0; j < n; j++)
{
cout << t[j] << "\t";
}
cout << "\n";
}
for (int i = 0; i < m; i++)
{
delete((int*)t);
}
delete [] t;
}
vector<void> subtract{
cout<<"TO SUBTRACT"<<endl;
int n = 0,m=0;
cout<<"enter no. of rows and columns of matrix"<<endl;
cin>>m>>n;
int **t,**q;
char chk;
t = new int*[m];
for (int i = 0; i< m; i++)
{
t = new int[n];
}
int a = 0;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
t[j] = 0;
}
}
cout<<"enter the numbers of matrix"<<endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cin>>t[j];
}
}
cout<<"matrix you have entered"<<endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cout << t[j] << "\t";
}
cout << "\n";
}
chk='y';
while (chk=='y'||chk=='Y'){
q = new int*[n];
for (int i = 0; i< m; i++)
{
q = new int[n];
}
a = 0;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
q[j] = a++;
}
}
cout<<"enter the numbers of matrix to be subtracted"<<endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cin>>q[j];
}
}
cout<<"matrix you have entered"<<endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cout << q[j] << "\t";
}
cout << "\n";
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
t[j] = t[j]-q[j];
}
}
for (int i = 0; i < m; i++)
{
delete((int*)q);
}
delete [] q;
cout<<"wish to subtract another matrix from the result? y/n"<<endl;
cin>>chk;
}
cout<<"difference is "<<endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cout << t[j] << "\t";
}
cout << "\n";
}
for (int i = 0; i < n; i++)
{
delete((int*)t);
}
delete [] t;
}
vector<void> multiply{
int n = 0,m=0,l=0;
cout<<"TO FIND THE PRODUCT"<<endl;
cout<<"enter no. of rows and columns of matrix"<<endl;
cin>>m>>n;
int **t,**q;
t = new int*[m];
for (int i = 0; i< m; i++)
{
t = new int[n];
}
int a = 0;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
t[j] = 0;
}
}
cout<<"enter the numbers of matrix "<<endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cin>>t[j];
}
}
cout<<"matrix you have entered"<<endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cout << t[j] << "\t";
}
cout << "\n";
}
cout<<"enter no. of columns of 2nd matrix"<<endl;//no.of rows of 2nd column=no.of columns of 1st matrix(rule)
cin>>l;
q = new int*[n];
for (int i = 0; i< n; i++)
{
q = new int[l];
}
a = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < l; j++)
{
q[j] = a++;
}
}
cout<<"enter the numbers of matrix to be multiplied"<<endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < l; j++)
{
cin>>q[j];
}
}
cout<<"matrix you have entered"<<endl;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < l; j++)
{
cout << q[j] << "\t";
}
cout << "\n";
}
cout<<"product is"<<endl;
int k=0;
for (int i=0;i<m;i++)
{
for (int j=0;j<l;j++)
{
for (int r=1;r<=n;r++)
{
k=k+(t[r-1]*q[r-1][j]);
}
cout<<k<<"\t";
k=0;
}
cout<<endl;
}
}
vector<void> inverse3{ //inverse for 3x3 matrix
int p[3][3];// the matrix that is entered by user
int q[3][3];//the transpose of a matrix p
int C[3][3];//the adjunct matrix of transpose of a matrix p not adjunct of p
double X[3][3];//the inverse
int i,j;
float x,n=0;
cout<<"enter matrix"<<endl;
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
cin>>p[j];
q[j]=0;
C[j]=0;
}
}
for (i=0,j=0;j<3;j++)
{
if (j==2)
n+=p[j]*p[i+1][0]*p[i+2][1];
else if (j==1)
n+=p[j]*p[i+1][j+1]*p[i+2][0];
else
n+=p[j]*p[i+1][j+1]*p[i+2][j+2];
}
for (i=2,j=0;j<3;j++)
{
if (j==2)
n-=p[j]*p[i-1][0]*p[i-2][1];
else if (j==1)
n-=p[j]*p[i-1][j+1]*p[i-2][0];
else
n-=p[j]*p[i-1][j+1]*p[i-2][j+2];
}
cout<<"matrix is"<<endl;
for (i=0;i<3;i++)
{
cout<<"\n";
for (j=0;j<3;j++)
{
cout<<p[j]<<"\t";
}
}
if (n!=0) x=1.0/n;
else
{
cout<<"Division by 0!\n";
}
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
q[j]=p[j];
}
}
C[0][0]=q[1][1]*q[2][2]-(q[2][1]*q[1][2]);
C[0][1]=(-1)*(q[1][0]*q[2][2]-(q[2][0]*q[1][2]));
C[0][2]=q[1][0]*q[2][1]-(q[2][0]*q[1][1]);
C[1][0]=(-1)*(q[0][1]*q[2][2]-q[2][1]*q[0][2]);
C[1][1]=q[0][0]*q[2][2]-q[2][0]*q[0][2];
C[1][2]=(-1)*(q[0][0]*q[2][1]-q[2][0]*q[0][1]);
C[2][0]=q[0][1]*q[1][2]-q[1][1]*q[0][2];
C[2][1]=(-1)*(q[0][0]*q[1][2]-q[1][0]*q[0][2]);
C[2][2]=q[0][0]*q[1][1]-q[1][0]*q[0][1];
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
X[j]=C[j]*x;
}
}
cout<<endl;
cout<<" The inverse matrix of the matrix \n";
for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
cout<<X[j]<<"\t";
}
cout<<endl;
}
cout<<"\n\n";
}
vector<void> inverse2{ //inverse for 2x2 matrix
int p[2][2];
float q[2][2];
int d,k;
cout<<"enter the matrix"<<endl;
for (int i=0;i<2;i++)
{
for (int j=0;j<2;j++)
{
cin>>p[j];
}
}
cout<<"matrix is"<<endl;
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
cout<<p[j]<<"\t";
}
cout<<endl;
}
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
q[j] = p[j];
}
}
k=q[0][0];
q[0][0]=q[1][1];
q[1][1]=k;
k=q[0][1];
q[0][1]=q[1][0];
q[1][0]=k;
q[0][1]=(-1)*q[0][1];
q[1][0]=(-1)*q[1][0];
d=q[0][0]*q[1][1]-q[1][0]*q[0][1];
cout<<"inverse of matrix is"<<endl;
for (int i=0;i<2;i++)
{
for (int j=0;j<2;j++)
{
q[j]=q[j]/d;
cout<<q[j]<<"\t";
}
cout<<endl;
}
}
};
int main()
{
int x;
cin>>x;
vector<int> p[][4];
vector< int> p2[100];
myarray n1;
n1.diffrentsize;
n1.check;
n1.length;
n1.det;
if(x==4){
n1.add;
n1.subtract;
n1.multiply;
if(x==2&&y==2){
n1.inverse2;}
if(x==3&&y==3){
n1.inverse3;}
n1.transpose;}
return 0;
}وشكرا لكم




