cs and c
This commit is contained in:
@@ -16,14 +16,13 @@ int addAnimal(const Animal* animalPtr, Animal* animalArray, size_t animalArrayLe
|
||||
}
|
||||
|
||||
int removeAnimal(int animalId, Animal* animalArray, size_t numberOfAnimalsPresent, size_t* newNumberOfAnimalsPresent){
|
||||
int fault = -1;
|
||||
int fault = 0;
|
||||
size_t removedCount = 0;
|
||||
|
||||
if (animalArray == NULL || numberOfAnimalsPresent == 0) {
|
||||
fault = -1; // Invalid
|
||||
return -1; // Invalid
|
||||
} else {
|
||||
size_t writeIndex = 0;
|
||||
|
||||
// Copy only animals that don't match the ID
|
||||
for (size_t readIndex = 0; readIndex < numberOfAnimalsPresent; ++readIndex) {
|
||||
if (animalArray[readIndex].Id != animalId) {
|
||||
@@ -33,14 +32,11 @@ int removeAnimal(int animalId, Animal* animalArray, size_t numberOfAnimalsPresen
|
||||
writeIndex++;
|
||||
} else {
|
||||
removedCount++;
|
||||
fault = 0; // Found some animal(s) to remove
|
||||
}
|
||||
}
|
||||
// Update the number of animals present
|
||||
numberOfAnimalsPresent -= removedCount;
|
||||
}
|
||||
|
||||
*newNumberOfAnimalsPresent = numberOfAnimalsPresent;
|
||||
fault = removedCount;
|
||||
*newNumberOfAnimalsPresent = (numberOfAnimalsPresent - removedCount);
|
||||
return fault;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user