# Sparse 2 dimensional array

• February 26th, 2012, 01:23 PM
toky25
Sparse 2 dimensional array
Hello

i created a program which uses Sparse 2 dimensional array, but i am not sure if i did it in the right way .

this is the instruction i have:
Create a constructor and a destructor. The constructor should take as input the size of the array (consider only square NxN arrays, so only one dimension is needed) and the thickness of the ribbon. To make this precise, if supplied with a thickness parameter t, you may assume that the element [0,t] (i.e. the (t+1)-th element of the first row) is where the useless area begins on the right. Similarly, the element [t,0] is where the useless area begins on the left. The border of the useless areas moves diagonally down and to the right, i.e. it consists of [1,t+1],[2,t+2],... and [t+1,1],[t+2,2],... The above example has thickness 3.
The space for the 2-d array should be dynamically allocated and must be large enough to fit the useful data only.
Create methods for random read and write access to the array as in the case of 1-d arrays.
Overload the [] and << operators, as in the case of 1-d arrays. Think carefully about what the [] operator should return and how it should work. Ideally we would like this to behave in a manner similar to standard 2-d arrays (i.e. accessing elements in the normal way, like x[5][6]).

thank you
• February 26th, 2012, 05:47 PM
Paul McKenzie
Re: Sparse 2 dimensional array
Quote:

Originally Posted by toky25
Hello

i created a program which uses Sparse 2 dimensional array, but i am not sure if i did it in the right way .

The one thing that is wrong or ill-advised is your assignment operator.

The assignment operator should work exactly the same way as the copy constructor, except it should return a reference to the current object. Your assignment operator does not work the same as the copy constructor.

For example, this program will just confuse the daylights of any programmer using your class:
Code:

```int main() {     My2dArray  a1(10,10);     My2dArray  a2 = a1;  // copy constructor     My2dArray a3(5, 5);     a3 = a1;  // assignment operator? But a3 is not 10x10??!! }```
A programmer would expect a3 to be logically equivalent to a1, but it isn't. I would expect a3 to be resized and also have the internal array to be 10 x 10, along with the copy of the data.

Making bogus assignment operators like this will lead to difficult bugs to diagnose. When you make one object equal to another, the program must behave exactly the same if I used object 1, and then replaced it with object 2.
This is even more confusing given that your copy constructor does different things than the assignment operator. Also remember that the compiler can use the assignment operator without you having to do anything explicitly. So now you have a program that has the potential to run differently depending on compiler and compiler options used.

Make the assignment operator do exactly as the copy constructor -- delete the memory from the current object, resize the matrix, and then copy the data. If you need a function that actually takes a 10x10 matrix, and stuffs it in a 5x5 matrix, then write a different function, call it Normalize() or something like that. Do not make the assignment operator have this ability -- the purpose of the assignment operator is just that -- assign.

Regards,

Paul McKenzie
• February 26th, 2012, 06:30 PM
toky25
Re: Sparse 2 dimensional array
Thank you Paul McKenzie
• February 27th, 2012, 10:54 AM
monarch_dodra
Re: Sparse 2 dimensional array
Unless I'm mistaken, the code you sent us is for a plain old N*M matrix.

Either you attached the wrong code, or you are waiting for us to do your homework?

Your assignment, BTW, is not about doing a Sparse matrix, but a Band matrix/ That wikipedia article should provide clues as to how you should write your program. Either your instructor has them confused, or he is giving out the strangest instructions ever.

PS: "Overload the [] and << operators, as in the case of 1-d arrays" Last time I checked, 1D arrays do not overload the << operator.

Where did you get this assignment???