animal shelter + adidas v1 .zip
This commit is contained in:
@@ -7,34 +7,39 @@
|
||||
#include "animal.h"
|
||||
|
||||
int addAnimal(const Animal* animalPtr, Animal* animalArray, size_t animalArrayLength, size_t numberOfAnimalsPresent, size_t* newNumberOfAnimalsPresent){
|
||||
int fault = 0;
|
||||
if (animalArray == NULL || animalPtr == NULL || numberOfAnimalsPresent >= animalArrayLength) {
|
||||
fault = -1; // Invalid
|
||||
} else {
|
||||
animalArray[numberOfAnimalsPresent] = *animalPtr;
|
||||
++numberOfAnimalsPresent;
|
||||
if (animalArray == NULL || animalPtr == NULL || newNumberOfAnimalsPresent == NULL || numberOfAnimalsPresent >= animalArrayLength) {
|
||||
return -1; // Invalid
|
||||
}
|
||||
*newNumberOfAnimalsPresent = numberOfAnimalsPresent;
|
||||
return fault;
|
||||
animalArray[numberOfAnimalsPresent] = *animalPtr;
|
||||
*newNumberOfAnimalsPresent = numberOfAnimalsPresent + 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int removeAnimal(int animalId, Animal* animalArray, size_t numberOfAnimalsPresent, size_t* newNumberOfAnimalsPresent){
|
||||
int fault = -1;
|
||||
size_t removedCount = 0;
|
||||
|
||||
if (animalArray == NULL || numberOfAnimalsPresent == 0) {
|
||||
fault = -1; // Invalid
|
||||
} else {
|
||||
for (size_t i = 0; i < numberOfAnimalsPresent; ++i) {
|
||||
if (animalArray[i].Id == animalId) {
|
||||
// move the array elements to the left to remove the animal
|
||||
for (size_t j = i; j < numberOfAnimalsPresent - 1; ++j) {
|
||||
animalArray[j] = animalArray[j + 1];
|
||||
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) {
|
||||
if (writeIndex != readIndex) {
|
||||
animalArray[writeIndex] = animalArray[readIndex];
|
||||
}
|
||||
--numberOfAnimalsPresent;
|
||||
fault = 0;
|
||||
break;
|
||||
writeIndex++;
|
||||
} else {
|
||||
removedCount++;
|
||||
fault = 0; // Found some animal(s) to remove
|
||||
}
|
||||
}
|
||||
// Update the number of animals present
|
||||
numberOfAnimalsPresent -= removedCount;
|
||||
}
|
||||
|
||||
*newNumberOfAnimalsPresent = numberOfAnimalsPresent;
|
||||
return fault;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user