Hello,
The concept you have is pretty simple, but arrays are not fit to do this job. The problem with arrays is that they have constant index limit, especially if you're talking about c-based languages like C/C++, C#, Java, ActionScript, ect... LinkLists or ArrayLists were made to do this job because they have a limitless bounds, so you can actually delete, add, even insert objects into certain positions of the list endlessly.
In any case, if you still persist on using arrays I suggest that you create an array that very large like 255 and make sure your inputs don't touch this limit. Next, create a used index bool array that corresponds with the indexices of the string array. Create a function that allows you to remove and add to the array, everytime you add to the array, set the index value of your bool array to true for that position, then change the value of your array at that position. Below is an example of how i would do it. (code not tested)
// note: im assuming you're coding in C++
class Cats
{
public bool isEmptyPos[255];
public string NameOfCats[255];
public Cats()
{
for(int i=0; i < 255; i++)
isEmptyPos[i] = true;
}
public int FindLowestEmptyIndex()
{
for(int i=0; i < 255; i++)
{
if( isEmptyPos[i] ) return i;
}
return -1;
}
public bool addCatName(string CatName)
{
if(name.length > 0)
{
if(FindLowestEmptyIndex() > -1)
{
int localIdx=FindLowestEmptyIndex();
NameOfCats[localIdx] = CatName;
isEmptyPos[localIdx] = false;
return true;
}
}
return false;
}
}
public void RemoveCatByName(string CatName)
{
for(int i=0; i<255; i++)
{
if(!isEmptyPos[i])
{
if(NameOfCats[i] == CatName)
{
isEmptyPos = true;
//note: clearing the actual string array
//at this point is pointless because isEmptyPos
//is what we use to determine if an array element
//is empty or not
}
}
}
public string printAllCatNames()
{
string catNames = "The cats include:\n";
for(int i=0; i<255; i++)
{
if(isEmptyPos[i])
catNames+=(NameOfCats[i]+"\n");
}
if(catNames=="The cats include:\n")
return "No cats have been added";
return catNames;
}
}
I hope that helps. Again, this code is not tested, but you should get the general idea.