Solved

How do I delete a row (one array) from a two dimensional matrix, in C++ code

Posted on 2003-03-27
Medium Priority
294 Views
void Marble::find_marble()
{
//  while(num_marbles > 0)
//  {
for(int i = 1; i <= num_marbles; i++)
{
for(int j = 0; j < 20; j++)
{
if(sphere[i][j] == color)
{
delete [] sphere[i];
}
}
}

I am writing the marble game where players choose a color and if they are correct the marble(array in the matrix) is removed.  Each marble has twenty possible colors in it chosen at random with a random number generator.
The sample code above does not work to remove an array from the dynamically allocated two dimensional array called "sphere".
How do I delete a row(array) from a two dimensional matrix??
0
Question by:ap39769
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions
• 4
• 3
• 2
• +2

LVL 4

Accepted Solution

n_fortynine earned 100 total points
ID: 8220745
you don't need all that complication. just create a "lock" (integer) to let the program know if a "row" should/should not be accessible. Initially keep all locks open, if you've decided that a row is no longer needed, "lock" it (of course this adds a little overhead to your program by having to check this "lock" everytime a row is to be accessed, but I guess it won't cost too much). does this sound doable?
0

LVL 3

Expert Comment

ID: 8220943
I think you'll have to create a new array with one less row, then copy all the data from the old array to the new one.
0

LVL 4

Expert Comment

ID: 8221053
wide_awake, that's more or less inefficient. what he's doing does not require the row to be *physically* removed from the array (if the marbles match, the row is "removed" and that's all).

the reasons why i suggest such a solution can be named: first, deleting the array like he did does not automatically "correct" the column indexes i.e. there would be one invalid index so the deleting really does not contribute anything to the concept of removal after all. Secondly, overwriting data from latter rows onwards is obviously a bad choice (you can see why) - copying data to a *new* array is even worse. And so on.

So what I was suggesting is to either keep another array of "locks" running separately from the "sphere," or maybe just allocate one more position in each row (use [0] for example - so instead of size 20, size 21). there might eventually be a better solution than this, but I'm certainly not in favor of copying "the old array to the new one."
0

LVL 3

Expert Comment

ID: 8221273
Copying the whole array each time is certainly not the most efficient way to do it, but it is the only way to *really* delete the row.

If just "hiding" a row is good enough, then your method will work, and will work much faster :)

Since it looks like your row width is fixed, you could create a struct that contains an array of ints (a row), as well as a flag for valid.  Then your sphere[] array would just be a 1-dimensional array of row structs.

You could set rows to be valid by setting
sphere[i].valid = 1;

or access the "colors" value by using
sphere[i].colors[j] = color;

-Mark.
0

LVL 4

Expert Comment

ID: 8221635
>> but it is the only way to *really* delete the row
even if the row really has to be removed from the array, it's still not good to copy the entire array. What you'll do then is "rippling" the rows. e.g.

[0] ...
[1] ...
[2] ...
...
[n] ...

to *delete* row like you said, would only require copying row 3 onto 2, 4 onto 3 and so on, there is no point to even create anything else copy to. That's simply out of the question.

>> create a struct
well, it's one way to go about it. Using an array to do this would simplify the notation and coding (if he uses a struct he'll have to go back to the start, otherwise he'll only have to modify the existing code to check this array)
Either way is fine.
0

LVL 4

Expert Comment

ID: 8221639
>> to *delete* row like you said
0

LVL 3

Expert Comment

ID: 8222855
I was thinking that the array would be the wrong size if you just "rippled" the entries up, but I guess that doesn't matter as long as you keep track of how many entries are in the array.

0

LVL 30

Assisted Solution

Mayank S earned 100 total points
ID: 8223124
ap39769,

You cannot use delete [] to delete an array which was not allocated using new, but simply declared as 'int sphere[10][10] ;', if that's the case here.

The best thing to do is to move/ shift the rows up by 1 so that the row to be deleted is over-written by the one below it, and keep doing it till the last row, and then decrement the variable holding the number of rows by 1.

for ( i = 0 ; i < num_marbles ; i ++ )
for ( j = 0 ; j < 20 ; j ++ )
if ( sphere[i][j] == color )
{
for ( k = i ; k < num_marbles - 1 ; k ++ )
for ( l = 0 ; l < 20 ; l ++ )
sphere[k][l] = sphere[k+1][l] ; // end if, fors

num_marbles -- ; // decrement the number of rows by 1

} // end if, outer fors

How-ever, if the 2-D array was actually a pointer-to-a-pointer, and allocated as:

int ** sphere ;

sphere = new (int *)[num_marbles] ;

for ( i = 0 ; i < num_marbles ; i ++ )
sphere[i] = new int[20] ; // end for

Then, I would suggest that you modify the pointers such the first ( num_values - 1 ) hold the relevant rows, and the last one holds the row to be deleted, and then deallocate it using 'delete[]'.

int * temp ;

for ( i = 0 ; i < num_marbles ; i ++ )
for ( j = 0 ; j < 20 ; j ++ )
if ( sphere[i][j] == color )
{
temp = sphere[i] ;

for ( int k = i ; k < num_marbles - 1 ; k ++ )
sphere[k] = sphere[k+1] ; // end if, fors

sphere[k] = temp ;
delete [] sphere[k] ;
num_marbles -- ; // decrement the number of rows by 1

} // end if, outer fors

Hope that helps!

Mayank.
0

LVL 8

Expert Comment

ID: 8227557
Why not use c++ instead:

typedef std::vector< std::vector< int > > int_matrix;
int_matrix sphere;

Now you can easily remove any given row by first getting an iterator and then applying

sphere.erase( it );

Even though not strictly necessary you can avoid memory reallocations by calling reserve( num_marbles ) on the inner vectors to. Access can be achieved much like in the c-like pointer-to-pointer implementation by writing

if( sphere[i][j] == color ) ...

.f
0

Expert Comment

ID: 9446716
ap39769:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

LVL 30

Expert Comment

ID: 9451841
Split points between mayankeagle, fl0yd and n_fortynine.
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and â€¦
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to goâ€¦
Suggested Courses
Course of the Month12 days, 7 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.