I have to write a program that created 2d array but store all data in a dynamically allocated 1d array. I have function definition and I know how to write up other functions but I need to overload operator() to aceess the array.
Here is the definiton :

public:
	Array(double,double);
	Array( const Array &);
	~Array();
	int getSize() const;

	const Array &operator = (const Array &);
	bool operator ==(const Array &) const;

	bool operator!=(const Array &right) const
	{
		return!(*this == right);
	}

	double &operator() (double,double);

	double operator() (double, double) const;
 private:
	 int size;
	 int *ptr;
};

Here is the constructor I have defined :-

Array::Array(double row, double column)
{
	size = row * column;
	//int index = 4*row + column;
	ptr = new double[size];

	for(int i = 0; i<size; i++)
	{
		ptr[i] = 0;
	}
}

I don't know how to write up the two functions
double &operator() (double,double);
double operator() (double, double) const;


thanks

For starters, your private members should include the row and column sizes. Thus, your overloaded ( ) operator would compute the actual index, using something similar to what you commented out in your constructor above index = desired_row * columns + desired_column;

how can I return the value in the end after I write down function like this :-

double Array::operator()(int row1, int col1)
{
    int index;
    index = columns *row1 + col1;

return ptr[index]; ??

thanks, I have another question..how do I write this function for 2d array( I am accessing 2d array as 1d array so I know how to write in 1d)

const Array &operator = (const Array &);

for 1d it is :

const Array &Array::operator=( const Array &right )
53   {
54      if ( &right != this ) // avoid self-assignment
55      {
56         
58         if ( size != right.size )
59         {
60            delete [] ptr; 
61            size = right.size; 
62            ptr = new int[ size ]; 
63         } // end inner if
64
65         for ( int i = 0; i < size; i++ )
66            ptr[ i ] = right.ptr[ i ]; // copy array into object
67      } // end outer if
68
69      return *this; 
70   } // end function operator=

somebody told me that I can just modify it but I am confused, any help???

First, get your terms straight. You are accessing a 1D array as a 2D array (you're working in a row/column manner, but your data is in a 1D array.)

The only change you need for the code above to suit the problem we've been discussing is to calculate the size based on the row & column values that make up the size of right

so I need to replace right with row * columns??

In my last reply, I'd forgotten that Array right already had the size computed and stored, which makes line 61 above OK as is. But you should store right's row and column dimensions to this's row and column members.

I am sorry I didn't get it. which line should I make changes? can u please show me?

Thanks,

anyone????

Here's the several snippets you've posted, with just some minor additions that fill them out. What I've done has comment //added noted.

class Array
{
public:
   Array(double,double);
   Array( const Array &);
   ~Array();
   int getSize() const;

   const Array &operator = (const Array &);
   bool operator ==(const Array &) const;

   bool operator!=(const Array &right) const
   {
      return!(*this == right);
   }

   double &operator() (double,double);

   double operator() (double, double) const;
private:
   int size;
   int *ptr;
   int rows; //added
   int cols; //added
};



Array::Array(double row, double column)
{
	size = row * column;
   rows = row; //added
   cols = col; //added
	ptr = new double[size];

	for(int i = 0; i<size; i++)
	{
		ptr[i] = 0;
	}
}

double Array::operator()(int row1, int col1)
{
   int index;
   index = (cols * row1) + col1;
   return ptr[index]; //added
}


const Array &Array::operator=( const Array &right )
{
   if ( &right != this ) // avoid self-assignment
   {
      
      if ( size != right.size )
      {
         delete [] ptr; 
         size = right.size; 
         ptr = new int[ size ]; 
         rows = right.rows; //added
         cols = right.cols; //added
      } // end inner if

      for ( int i = 0; i < size; i++ )
         ptr[ i ] = right.ptr[ i ]; // copy array into object
   } // end outer if

   return *this; 
} // end function operator=

here is my copy constructor..anyone has idea how can I make it for 2d arrays( this copy constructor is for 1d array)

I have private varivales : column, row, size and double *ptr

Array::Array(const Array &arrayToCopy)
:size(arrayToCopy.size)
{
	size = arrayToCopy.getSize();
	ptr = new double[size];
	for( int i = 0; i<size;i++)
	{
		ptr[i] = arrayToCopy[i];
	}
}

this works for 1d array but I don't know how can I make it for 2d array?

I think all it needs is to also copy the rows and cols values from the source (based on my previous addition of data members):

Array::Array(const Array &arrayToCopy)
:size(arrayToCopy.size)
{
	size = arrayToCopy.getSize();
	ptr = new double[size];
        rows = arrayToCopy.rows,
        cols = arrayToCopy.cols
	for( int i = 0; i<size;i++)
	{
		ptr[i] = arrayToCopy.ptr[i];
	}
}

The actual copying will be fine as is, you copy element for element size times. Note that you omitted the ptr member of the arrayToCopy in the loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.