views:

45

answers:

3

I have an assignment where I have questions that ask for the following implementations:

insertAtRanks(Integer x, Integer y, Object o): insert a new element to be stored at position (x,y) 

and

Object removeAtRanks(Integer x, Integer y): remove and return the element at position (x,y)

It already asked for the implementation of replaceAtRanks where I had to replace the element inside a position with a parameter.

So what I assume when inserting and removing elements, the matrix will increase and decrease in size, but my question is how?

For example

| 3    6|
| 2    5|

If I had to do an insert number 8 at position (1,1) will the following happen?

| 3    6|
| 2    8|
| null 5|

And if I had to remove the element at (1,1) afterwards will it go back to?

| 3   6|
| 2   5|

Edit:

I am using Java for the implementation, and I am using a 2 dimensional array of classes to represent the matrix.

+1  A: 
|3    6|
|null 8|
|2    5|

or

|3    6|
|0    8|
|2    5|

or

|3    6|
|2    8|

or many other forms suggest themselves as alternatives. I think you have to decide what you have been asked to implement, and then implement it.

High Performance Mark
A: 

I don't know which language are you using, but it should be something like:

  • Insert a new row with empty values
  • for i = newSizeOnX until i reach x (your row), with i decreasing, copy the row (m[i+1]=[i])
  • then you just insert object in the position m[x][y] and fill the rest m[x][y'] (all the y' != y)
fern17
A: 

Up to a certain point, this is a question of specifications.

In a normal system, you could either:

  • decide to treat those cases as forbidden exceptions, and return an error or a warning if it happens
  • define a specific predetermined behavior, that is consistent for all functions you implement, and clearly documented

For instance, pre-filling the matrix with zeroes or not-a-number is one typical way of handling your first problem. How is Matlab handling this by default?

It also depends on the application. For instance, if you are working with images, chances are you don't want to exceed the original image size in most cases. If you are working with sound, the default case is probably to insert silence when extending a data sequence. What makes the most sense for YOUR application?

Kena