10 Replies - 1081 Views - Last Post: 15 September 2010 - 03:08 PM Rate Topic: -----

#1 Guest_Evgeniy*


Reputation:

class "matrix"

Posted 15 September 2010 - 12:50 PM

Mistake at end of work of a code.

class matrix
{ 
private:
        int m,n;
        int **matrica;
public: 
         matrix();
	 matrix(int);
        ~matrix(); 
	 matrix(const matrix &A1);
        int get(int ,int);
        void print();
		void setSize();
};
matrix::matrix()
{
	matrica=NULL;
	n=m=0;
}
void matrix::setSize()
{
    int first;
    int second;
    cout<<"Enter first size: ";
    cin>>first;
    cout<<"Enter second size: ";
    cin>>second;
    n=first;
    m=second;
	system("cls");
 }

matrix::matrix(int B)/>
{  
	setSize();

int **matrica2=(int**)malloc(n*sizeof(int*));
for(int i=0;i<n;i++)
    matrica2[i]=(int*)malloc(m*sizeof(int));

        for (int i = 0; i < n; i++)
               for (int j = 0; j < m; j++)
                        matrica2[i][j] = rand() % 10;
matrica=matrica2;

 }
void matrix::print()
{
     for(int i = 0; i < n; i++)
     {
         for(int j = 0; j < m; j++)
               cout<<matrica[i][j]<<' ';
         cout<<"\n";
     }
}

 matrix::~matrix()
{
	if(matrica!=NULL)
	{
	  for (int i = 0; i < n; i++)
     free(matrica[i]);
free(matrica);
	}
}

int matrix::get(int i,int j)
{
         if(i>=n||j>=m)
            return 0;
         cout<<matrica[i][j]<<'\n';

}

matrix::matrix(const matrix &A1)
{
	//n=A1.n;

	int **matrica=(int**)malloc(n*sizeof(int*));
	for(int i=0;i<n;i++)
		matrica[i]=(int*)malloc(m*sizeof(int));
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			matrica[i][j]=A1.matrica[i][j];
	cout<<"fewfwewfeger";
}

int main ()
{  
        system("cls");
        matrix object;
		matrix ob(5);
        object=ob;
        object.print();
        int i, j;
        cout<<"\n get (i.j):"<<endl;
        cout<<"Enter i: ";
        cin>>i;
        cout<<"Enter j: ";
        cin>>j;
        if(object.get(i,j)==0)
           cout<<"i or j more or equal than n or m!\n";
        system("PAUSE");
        return 0;
}


Is This A Good Question/Topic? 0

Replies To: class "matrix"

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14091
  • View blog
  • Posts: 56,456
  • Joined: 12-June 08

Re: class "matrix"

Posted 15 September 2010 - 12:58 PM

Where - what line? What is the error doing and/or the error message, and what *should* it be doing?
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: class "matrix"

Posted 15 September 2010 - 12:58 PM

Would you like to tell us what the problem is? If you're getting an error, please copy and paste it here. If the output is not what's expected, please tell us what you expect to happen, and what you're seeing happen.

EDIT: I don't get why I'm seeing so many C++ programs using malloc instead of new lately...what's up with that?

EDIT 2: I see this in your copy constructor:
for(int i=0;i<n;i++)
    for(int j=0;j<n;j++)
        matrica[i][j]=A1.matrica[i][j];


you're using n for both loops, instead of m and n.

This post has been edited by JackOfAllTrades: 15 September 2010 - 01:03 PM

Was This Post Helpful? 0
  • +
  • -

#4 Guest_Guest*


Reputation:

Re: class "matrix"

Posted 15 September 2010 - 01:25 PM

Having executed all actions the program gives out a mistake and opens dbgheap.c
what incorrect?

class matrix
{ 
private:
        int m,n;
        int **matrica;
public: 
         matrix();
		 matrix(int);
        ~matrix(); 
		 matrix(const matrix &);
        int get(int ,int);
        void print();
		void setSize();
};
matrix::matrix()
{
	matrica=NULL;
	n=m=0;
}
void matrix::setSize()
{
    int first;
    int second;
    cout<<"Enter first size: ";
    cin>>first;
    cout<<"Enter second size: ";
    cin>>second;
    n=first;
    m=second;
	system("cls");
 }

matrix::matrix(int B)/>
{  
	setSize();

int **matrica2=(int**)malloc(n*sizeof(int*));
for(int i=0;i<n;i++)
    matrica2[i]=(int*)malloc(m*sizeof(int));

        for (int i = 0; i < n; i++)
               for (int j = 0; j < m; j++)
                        matrica2[i][j] = rand() % 10;
matrica=matrica2;

 }
void matrix::print()
{
     for(int i = 0; i < n; i++)
     {
         for(int j = 0; j < m; j++)
               cout<<matrica[i][j]<<' ';
         cout<<"\n";
     }
}

 matrix::~matrix()
{
	if(matrica!=NULL)
	{
	  for (int i = 0; i < n; i++)
     free(matrica[i]);
free(matrica);
	}
}

int matrix::get(int i,int j)
{
         if(i>=n||j>=m)
            return 0;
         cout<<matrica[i][j]<<'\n';

}

matrix::matrix(const matrix &A1)
{
	

	int **matrica=(int**)malloc(n*sizeof(int*));
	for(int i=0;i<n;i++)
		matrica[i]=(int*)malloc(m*sizeof(int));

	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			matrica[i][j]=A1.matrica[i][j];
	cout<<"fewfwewfeger";
}

int main ()
{  
        system("cls");
      matrix object;
		matrix ob(5);
        object=ob;
        object.print();
        int i, j;
        cout<<"\n get (i.j):"<<endl;
        cout<<"Enter i: ";
        cin>>i;
        cout<<"Enter j: ";
        cin>>j;
        if(object.get(i,j)==0)
           cout<<"i or j more or equal than n or m!\n";
        system("PAUSE");
        return 0;
}

Was This Post Helpful? 0

#5 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: class "matrix"

Posted 15 September 2010 - 01:30 PM

Please read my post again, particularly EDIT 2.

Oh, I see you fixed that.

There are a number of other issues. You didn't define an assignment operator. Your copy constructor is not setting the value of the member variable matrica, instead using a local variable. You don't assign the value of m and n in the copy constructor. You don't delete the existing memory for the member variable in the copy constructor.

EDIT: Oops, copy constructor doesn't require deleting existing memory!

This post has been edited by JackOfAllTrades: 15 September 2010 - 02:25 PM

Was This Post Helpful? 1
  • +
  • -

#6 Guest_Guest*


Reputation:

Re: class "matrix"

Posted 15 September 2010 - 01:48 PM

How correctly to organize the copying designer
How to initialize martica??
Was This Post Helpful? 0

#7 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: class "matrix"

Posted 15 September 2010 - 02:03 PM

Overloading assignment
Copy constructors
Was This Post Helpful? 0
  • +
  • -

#8 Guest_Guest*


Reputation:

Re: class "matrix"

Posted 15 September 2010 - 02:13 PM

What incorrect?

matrix::matrix(const matrix &a)
{
	int **matrica=(int**)malloc(n*sizeof(int*));
	for(int i=0;i<n;i++)
		matrica[i]=(int*)malloc(m*sizeof(int));

	for(int i=0;i<n;i++)
		for(int j=0;j<m;j++)
			matrica[i][j]=a.matrica[i][j];		 
}

Was This Post Helpful? 0

#9 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: class "matrix"

Posted 15 September 2010 - 02:26 PM

The matrica you're operating on is a LOCAL variable that goes away when you leave the copy contructor; it is NOT the member variable matrica. And you're not assigning to m and n the variables contained in the passed-in matrix.
Was This Post Helpful? 0
  • +
  • -

#10 Guest_Guest*


Reputation:

Re: class "matrix"

Posted 15 September 2010 - 02:44 PM

matrix::matrix(const matrix &a)
{
        matrica = (int**)malloc( n * sizeof(int*) );
        for( int i = 0; i < n; ++i )
                matrica[i] = (int*)malloc( m * sizeof(int) );
        for( int i = 0; i < n; ++i )
                for( int j = 0; j < m; ++j )
                        matrica[i][j] = a.matrica[i][j];
}


How to apply it in main() ???

May be:
int main ()
{  
        system("cls");
		matrix abc;
		matrix ob(5);
		matrix object(ob);
        object.print();
...

Was This Post Helpful? 0

#11 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: class "matrix"

Posted 15 September 2010 - 03:08 PM

This line:
matrix object(ob);

invokes the copy constructor.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1