dimanche 6 octobre 2019

the instruction at x referenced memory at y.The memory could not read

when i run this code.computer show memory error (the instruction at x reference memory at y.The memory could not be read)

#include<cstdlib>
using namespace std;
class Matrix{
private:
    int row,col,**ptr;
public:
    Matrix();
    void create(int,int);
    void show();
    Matrix multiply(Matrix);
  };
Matrix::Matrix()
{
    row=0;
    col=0;
    ptr=NULL;
}
void Matrix::create(int r,int c)
{
    row=r;
    col=c;
    ptr=new int* [row];
    static srand(time(0));
    for(int i=0;i<row;++i)
    {
        ptr[i]=new int[i+1];
    }
    for(int i=0;i<row;i++)
    {
        for(int j=0;j<col;j++)
        {
            ptr[i][j]=rand()%6+1;
        }
    }
}
Matrix Matrix::multiply(Matrix obj2)
{
    if(col==obj2.row)
    {
    Matrix temp;
    for(int i=0; i<row; ++i)
    {
        for(int j=0; j<obj2.col; ++j)
        {
            temp.ptr[i][j]=0;
        }
    }
    for(int i=0;i<row;i++)
    {
        for(int j=0;j<obj2.col;j++)
        {
            for(int k=0;k<col;k++)
            {
            temp.ptr[i][j]+=ptr[i][k]*obj2.ptr[k][j];
            }
        }
    }
    return temp;
    }
    else
    {
        cout<<"Conditions are NOT fulfill for Multiplication"<<endl;
    }
}
void Matrix::show()
{
    cout<<"Matrix: "<<endl;
    for(int i=0;i<row;i++)
    {
        for(int j=0;j<col;j++)
        {
            cout<<ptr[i][j]<<"\t";
        }
        cout<<endl;
    }
}
int main()
{
Matrix obj1,obj2,obj3;
obj1.create(2,2);
obj1.show();
obj2.create(2,2);
obj2.show();
obj3=obj1.multiply(obj2);
obj3.show();
}

program should multiply matrix and store in 3rd object.but error ocurrs.i dont know where is prblm.thats why i sent whole code.hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

Aucun commentaire:

Enregistrer un commentaire