Homework help

EvanB

Senior member
Nov 3, 2001
268
0
0
There is a problem on my homework assignment that I am just drawing blanks for. It seems easy, but I just dont see what it is asking. Anyone have any idea how to do this following problem?

The following C function removes structures from an array. It has three parameters:
the array of structs, the array of those to be removed, and the size of the two arrays.
The function does not remove any that should not be removed, but sometimes does
not remove all the structs that it should. Fix the function, and write up what the
problem was.
void RemoveUnused(Component *cmp, char *rlist, int count){
int i, j;
for(i = 0; i < count; i++){
if(rlist){
for(j = i + 1; j < count; j++){
cmp[j-1] = cmp[j];
rlist[j-1] = rlist[j];
}
count--;
}
}
}
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: EvanB
There is a problem on my homework assignment that I am just drawing blanks for. It seems easy, but I just dont see what it is asking. Anyone have any idea how to do this following problem?

The following C function removes structures from an array. It has three parameters:
the array of structs, the array of those to be removed, and the size of the two arrays.
The function does not remove any that should not be removed, but sometimes does
not remove all the structs that it should. Fix the function, and write up what the
problem was.
void RemoveUnused(Component *cmp, char *rlist, int count){
int i, j;
for(i = 0; i < count; i++){
if(rlist){
for(j = i + 1; j < count; j++){
cmp[j-1] = cmp[j];
rlist[j-1] = rlist[j];
}
count--;
}
}
}


It seems you need three loops

First: Cycle through the main array
Second : Cycle through the remove array
Third : Move things down


See if that works.
 

Cerebus451

Golden Member
Nov 30, 2000
1,425
0
76
The problem is, after you remove an element, you are now effectively at element i+1 in the array. However, when you leave the loop, i gets incremented again, putting you at i+2. If you have consecutive elements to be removed, only the first will get removed. The correct solution would be:
 

AFB

Lifer
Jan 10, 2004
10,718
3
0
Originally posted by: Cerebus451
The problem is, after you remove an element, you are now effectively at element i+1 in the array. However, when you leave the loop, i gets incremented again, putting you at i+2. If you have consecutive elements to be removed, only the first will get removed. The correct solution would be:

I looked at that, but I didn't have a compiler to test it and didn't have paper to work it out. Disregard mine.
 

EvanB

Senior member
Nov 3, 2001
268
0
0
Originally posted by: Cerebus451
The problem is, after you remove an element, you are now effectively at element i+1 in the array. However, when you leave the loop, i gets incremented again, putting you at i+2. If you have consecutive elements to be removed, only the first will get removed. The correct solution would be:

Ok, that makes sense. Thanks for pointing me in the right direction. :)

Evan