Im creating a code in C to remove all the negatives when given a double pointer array with some values, as well as a constant int size. My code behaves strange for the 3rd case, but works for the others. Can someone help point me in the right direction?
short remove_negatives(long** array, const int size){
if (!*array || !array || !**array){
printf("Error(remove_negatives): invalid array\n");
return -1;
}
if (size <= 0){
printf("Error(remove_negatives): invalid size\n");
return -1;
}
short count = 0;
int i;
for (i=0;i<size;i++){
if (*(*array+i) < 0){
*(*array + i) = *(*array + i + 1);
count += 1;
}
}
return count;
}
the output:
------------------------------
Start: Testing remove_negatives:
Case 1:
Before: {10,20,-10,30,40}
after: {10,20,30,30}
# removed items = 1
Case 2:
Before: {-10,20,-30,40,50,60,70}
after: {20,20,40,40,50}
# removed items = 2
Case 3:
Before: {10,-20,-30,-40,50}
after: {10,-30}
# removed items = 3
Case 4:
Before: {10,-20,-30,-40}
after: {10}
# removed items = 3
Case 5:
Before: {-10,-20,-30}
after: {}
# removed items = 3
Case 6:
Error(remove_negatives): invalid size