Reputation: 41
Here is the function I am to implement
string* deleteEntry(string* dynamicArray, int& size, string entryToDelete);
// Precondition: dynamicArray point to a array of strings with give size,
// newEntry is a string
// Postcondition: The function should search dynamicArray for entryToDelete.
// If not found, the request should be ignored and the
// unmodified dynamicArray returned. If found, create a new
// dynamic array one element smaller than dynamicArray. Copy
// all element except entryToDelete into the new array, delete
// dynamicArray, decrement size, and return the new dynamic
// array
string* deleteEntry(string* dynamicArray, int& size, string entryToDelete)
{
for (int i=0;i<size,i++);
{
if (entryToDelete==dynamicArray[i])
{
delete[] entryToDelete;
}
}
}
Clearly I am a beginner, and I'm not looking for you to write my code, but to give me advice on how to delete the entry. I cant find it in my textbook, and all examples I've found online include a separate function entirely to complete this, which I feel can be done within just the single function.
Upvotes: 0
Views: 634
Reputation: 41
//Function to remove a specified entry from the array string* deleteEntry(string* dynamicArray, int& size, string entryToDelete) { string *result = dynamicArray;
int indexofEntry = -1;
for(int i=0;i<size; i++)
{
if(dynamicArray[i] == entryToDelete)
{
indexofEntry = i;
break;
}
}
if (indexofEntry == -1)
return result;
else
{
result = new string[size-1];
int indexNewArray = 0;
for(int i=0; i<size; i++)
{
if(i != indexofEntry)
{
result[indexNewArray] = dynamicArray[i];
indexNewArray++;
}
else
{
//i=indexofEntry
}
}
size--;
delete[] dynamicArray;
}
return result;
}
Upvotes: 0
Reputation: 596352
First, your for
loop is malformed. You need a semicolon where you have a comma, and you need to remove the trailing semicolon.
Change
for (int i=0;i<size,i++);
To
for (int i=0;i<size;i++)
Second, you can't delete[]
individual entries in the array, because they are not individually allocated with new[]
to begin with (only the array as a whole is).
As for your question, the answer is right there in the comments in your code. You are just not following the steps that are outlined:
// Postcondition: The function should search dynamicArray for entryToDelete.
// If not found, the request should be ignored and the
// unmodified dynamicArray returned. If found, create a new
// dynamic array one element smaller than dynamicArray. Copy
// all element except entryToDelete into the new array, delete
// dynamicArray, decrement size, and return the new dynamic
// array
You need to play attention to your requirements and do what they say, eg:
string* deleteEntry(string* dynamicArray, int& size, string entryToDelete)
{
// The function should search dynamicArray for entryToDelete...
for (int i = 0; i < size; ++i);
{
if (entryToDelete == dynamicArray[i]) // If found...
{
// create a new dynamic array one element smaller than dynamicArray...
string *newArray = new string[size-1];
// Copy all element except entryToDelete into the new array...
for(int j = 0; j < i; ++j)
newArray[j] = dynamicArray[j];
for(int j = i+1; j < size; ++j)
newArray[j-1] = dynamicArray[j];
// delete dynamicArray...
delete[] dynamicArray;
// decrement size...
--size;
// return the new dynamic array...
return newArray;
}
}
// If not found, return the unmodified dynamicArray...
return dynamicArray;
}
Upvotes: 1
Reputation: 745
I would strongly suggest using the various data structures and algorithms provided by the STL. The STL was created for many reasons, and one of them was to practically eliminate the need to create your own static and dynamic arrays, thereby avoiding all the problems that come with them. The following code demonstrates how to use the STL to accomplish your task.
vector<string> myStrings = {"erase this string"};
string stringToErase = "erase this string";
auto it = find(myStrings.begin(), myStrings.end(), stringToErase);
if (it != myStrings.end())
myStrings.erase(it);
If you are interested in becoming proficient in modern C++, a book or two by the likes of Stroustrup, Meyers, and Sutter would prove quite helpful in that regard.
Upvotes: 0
Reputation: 1
If by deleting an array element do you mean deleting an object pointed by an element in an array of pointers:
delete myarray[elemen];
But if it is not an array of pointers and, by deleting an array element, you mean to shrink an array by deleting some element in the middle, that cannot be done in C++.
Upvotes: 0