- #1
D.Z
- 2
- 0
i tried to debug the following code and it crashes in the end at the curly bracket }
i think i have a problem in the destructor .. Can anybody tell me why this happens ??
knowing that the Matrix extends and shrinks as i wish and all the functions work perfectly..
thanks in Advance
i think i have a problem in the destructor .. Can anybody tell me why this happens ??
knowing that the Matrix extends and shrinks as i wish and all the functions work perfectly..
Code:
//-------------------------------Matrix.h-----------------------------------//
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
#ifndef MATRIX_H
#define MATRIX_H
class Matrix {
friend ostream& operator <<( ostream&, const Matrix& ) ; //insertion operator
friend istream& operator >>( istream&, Matrix& ) ; //extraction operator
public:
Matrix(int r=2, int c=3); //Default constructor
Matrix( const Matrix& ); //copy constructor
~Matrix( ); //Destructor
//int getRowNum( ) const; //gets how many rows are there in the matrix
//int getColNum( ) const; //gets how many colunms are there is the matrix
//int * getRow( int ) const; //gets a whole row
//int * getCol( int ) const; //gets a whole column
void setRowNum( int ); //changes the number of rows in the matrix and deletes the excess rows
void setColNum( int ); //changes the number of columns in the matrix and deletes the excess columns
void setRow( int*, int, int ); //sets a whole row to values
void setCol( int*, int, int ); //sets a whole column to values
private:
int row;
int col;
int ** arr;
};
#endif
//--------------------------------Matrix.cpp---------------------------//
#include "Matrix1.h"
ostream& operator <<( ostream& out, const Matrix & m ) //insertion operator
{
for (int i=0; i<m.row; i++)
{
for ( int j=0; j<m.col; j++)
out<<m.arr[i][j]<<" ";
out << endl;
}
out << endl;
return out;
}
istream& operator >>( istream& in, Matrix & m ) //extraction operator
{
for (int i=0; i<m.row; i++)
{
for ( int j=0; j<m.col; j++)
in>>m.arr[i][j];
}
return in;
}
Matrix::Matrix(int r, int c) //Default constructor
{
r>0 ? (row=r): exit(1);
c>0 ? (col=c): exit(1);
arr=new int* [row];
for ( int i=0; i<row; i++)
arr[i]=new int [col] ;
for ( int i=0; i<row; i++)
{
for (int j=0; j<col; j++)
arr[i][j]=0;
}
}
Matrix::Matrix( const Matrix& m ) //copy constructor
{
row=m.row;
col=m.col;
arr=new int* [row];
for ( int i=0; i<row; i++)
arr[i]=new int [col] ;
for ( int i=0; i<row; i++)
{
for (int j=0; j<col; j++)
arr[i][j]=m.arr[i][j];
}
}
Matrix::~Matrix( ) //Destructor
{
for ( int i=0; i<row; i++)
delete[] arr[i];
delete arr;
}
void Matrix::setRowNum( int r ) //changes the number of rows in the matrix and deletes the excess rows
{
if ( r>row)
{
for ( int i=row; i<r; i++)
{
arr[i]=new int [col];
for ( int j=0; j<col; j++)
arr[i][j]=0;
}
}else if ( r<row)
{
for ( int i=r; i<row; i++)
delete [] arr[i];
}
else
cout<<"Invalid!\n";
row=r;
}
void Matrix::setColNum( int c ) //changes the number of columns in the matrix and deletes the excess columns
{
if ( c>col)
{
int * temp=new int [c];
for (int i=0; i<row; i++)
{
for ( int j=0; j<col; j++)
temp[j]=arr[i][j];
for ( int j=col; j<c; j++)
temp[j]=0;
delete [] arr[i];
arr[i]=new int [c];
for ( int j=0; j<c; j++)
arr[i][j]=temp[j];
}
delete [] temp;
}else if ( c<col )
{
int * temp=new int [c];
for (int i=0; i<row; i++)
{
for ( int j=0; j<c; j++)
temp[j]=arr[i][j];
delete [] arr[i];
arr[i]=new int [c];
for ( int j=0; j<c; j++)
arr[i][j]=temp[j];
}
delete [] temp;
}
col=c;
}
void Matrix::setRow( int* newRaw, int size , int r ) //sets a whole row to values
{
if ( size== col && r<row)
{
for ( int i=0; i<row; i++)
{
if ( i==r)
{
for ( int j=0; j<col; j++)
arr[i][j]=newRaw[j];
}
}
}else
{
cout<<"The new row size must match the columns number\n";
exit(0);
}
}
void Matrix::setCol( int* newCol , int size, int c ) //sets a whole column to values
{
if ( size == row && c<col )
{
for ( int i=0; i<row; i++)
{
arr[i][c]=newCol[i];
}
}else
{
cout<<"The new row size must match the columns number\n";
exit(0);
}
}
//--------------------------Driver.cpp-----------------------
#include "Matrix1.h"
void main()
{
Matrix X;
cout<<"X:"<< endl<<X;
cout<<"Setting X to 5 by 3 matrix \n"<<"X:"<<endl;
X.setRowNum(5);
cout<<X;
cout<<"Setting X to 2 by 3 matrix \n"<<"X:"<<endl;
X.setRowNum(2);
cout<<X;
int X_row[]={1,2,3};
X.setRow( X_row, 3, 0);
cout<<"Setting the first row in X to values:\n"<<"X:"<<endl;
cout<<X;
cout<<"Setting X with only one column\nX:\n";
X.setColNum(1);
cout<<X;
cout<<"Adding three more columns to X\nX:\n";
X.setColNum(4);
cout<<X;
}