Home > OS >  Issue Deleting an entry from an array C
Issue Deleting an entry from an array C

Time:01-27

I have created a program that creates a list, adds to the list, etc. and I am having trouble removing an item from the created list. I just can't quite figure out why my deleteEntry function isn't working. Everything else works great, I just feel I have made a mistake with the deleteEntry function. Any advice?

string* addEntry(string* dynamicArray, int& size, string newEntry);
// Precondition: dynamicArray point to a array of strings with give size,
//               newEntry is a string
// Postcondition: A new dynamic array is created, which is one larger than
//                dynamicArray All elements from dynamicArray are copied to
//                new array, the new entry is added to new array, the size
//                is increased, the dynamicArray is deleted, new dynamic
//                array is returned.

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

void print(const string* dynamicArray, int size);
// Precondition: dynamicArray point to an array of strings with given size,
// Postcondition: The elements in dynamic array will be print out. One
//                element per line followed by its index


int main(){
      /* ADD/DELETE ENTRIES FROM AN ARRAY*/

    string newEntry, removeEntry;
    int amount = 10;
    typedef string* arrayPointer;
    arrayPointer listOfFoods;

    listOfFoods = new string[amount]; // Size is 10 entries,

    cout << "Now enter 10 foods." << endl;
    for(int index = 0; index < amount; index  ) {
        cout << "Enter a food item, " << index << "/10 entered so far: ";
        cin >> listOfFoods[index];
    }
    cout << "You entered: ";
    for(int index = 0; index < amount; index  ){
        if(index != amount - 1) {
            cout << listOfFoods[index] << ", ";
        }
        else{
            cout << listOfFoods[index] << endl;
        }
    }

    /* Adds two names */
    cout << "Add a food item, 0/2 entered so far: ";
    cin >> newEntry;
    listOfFoods = addEntry(listOfFoods, amount, newEntry);

    cout << "Add one last food item, 1/2 entered so far: ";
    cin >> newEntry;
    listOfFoods = addEntry(listOfFoods, amount, newEntry);

    print(listOfFoods, amount);


    /* Removes a name from the list */
    cout << "Of the list above, choose one that you would like to remove." << endl;
    cin >> removeEntry;
    cout << "You removed " << removeEntry << " from the list." << endl;

    deleteEntry(listOfFoods, amount, removeEntry);

    listOfFoods = print(listOfFoods, amount);

    return 0;
}

string* deleteEntry(string* dynamicArray, int& size, string entryToDelete) {
    // you implement this. Please read Programming Project 6 on page 536
    // you may watch video notes to get the idea

    string *newArray;
    newArray = new string[size];
    for (int i = 0; i < size; i  ) {
        if (dynamicArray[i] == entryToDelete) {
            string *newArray = new string[size - 1];

            for (int k = 0; k < size; k  ) {
                dynamicArray[k] != entryToDelete;
                newArray[k] = dynamicArray[k];
            }
        }
    }
    delete[] dynamicArray;
    return newArray;
}

void print(const string* dynamicArray, int size){
    // you implement this.
    // you may watch video notes to get the idea

    cout << "The list of the entries are as follows.\n";
    for(int index = 0; index < size; index   ){
        cout << dynamicArray[index] << " is index " << index << " and the "<< index 1 << " item in the list.\n";
    }
}

string* addEntry(string* dynamicArray, int& size, string newEntry){
    string *mostRecentArray;
    mostRecentArray = new string[size   1]; //Increases the array by one
    for (int i = 0; i < size; i  ){
        mostRecentArray[size] = newEntry;
        mostRecentArray[i] = dynamicArray[i];
    }
    size  ;

    return mostRecentArray;
}

CodePudding user response:

There are a number of problems with this code.

  • This statement:

    deleteEntry(listOfFoods, amount, removeEntry);
    

    should be this instead:

    listOfFoods = deleteEntry(listOfFoods, amount, removeEntry);
    

    Just like with your other function calls. This is explained in the function's Postcondition.

  • This statement:

    listOfFoods = print(listOfFoods, amount);
    

    will not compile, as print() returns void. It should be just this:

    print(listOfFoods, amount);
    
  • you don't have a function to clear the entire dynamicArray, so main() is leaking the entire list upon exit. Granted, the OS will reclaim all of the memory anyway when the process exits, but it is good practice to always clean up after yourself and free any memory you allocate.

  • you are not very good at following instructions.

    • deleteEntry() does not conform to its PostConditions:

      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

      deleteEntry() is creating and returning a new array every time, regardless of whether entryToDelete is found or not. And worse, if entryToDelete is not found, that new array contains empty strings! And if entryToDelete is found, you are creating another new array and leaking the previously created array.

      And, you are not actually omitting entryToDelete from the new array. The statement dynamicArray[k] != entryToDelete; has no effect, and so the statement newArray[k] = dynamicArray[k]; is always executed, thus copying every string from dynamicArray to newArray.

    • addEntry() also does not conform to its Postcondition:

      A new dynamic array is created, which is one larger than dynamicArray All elements from dynamicArray are copied to new array, the new entry is added to new array, the size is increased, the dynamicArray is deleted, new dynamic array is returned.

      And, addEntry() also has a small (but harmless) logic bug - it is repeatedly executing mostRecentArray[size] = newEntry; on every loop iteration. It should be performing that statement only one time, outside of the loop.

With all of that said, try this instead:

#include <iostream>
#include <string> 
using namespace std;

string* addEntry(string* dynamicArray, int& size, string newEntry);
// Precondition: dynamicArray point to a array of strings with give size,
//               newEntry is a string
// Postcondition: A new dynamic array is created, which is one larger than
//                dynamicArray All elements from dynamicArray are copied to
//                new array, the new entry is added to new array, the size
//                is increased, the dynamicArray is deleted, new dynamic
//                array is returned.

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* clearEntries(string* dynamicArray, int& size);
// Precondition: dynamicArray point to a array of strings with give size,
// Postcondition: The function will delete dynamicArray, set size to 0,
//                and return null

void print(const string* dynamicArray, int size);
// Precondition: dynamicArray point to an array of strings with given size,
// Postcondition: The elements in dynamic array will be print out. One
//                element per line followed by its index

int main(){
    /* ADD/DELETE ENTRIES FROM AN ARRAY*/

    string newEntry, removeEntry;
    int amount = 10;
    typedef string* arrayPointer;
    arrayPointer listOfFoods;

    listOfFoods = new string[amount]; // Size is 10 entries,

    cout << "Now enter 10 foods." << endl;
    for(int index = 0; index < amount;   index) {
        cout << "Enter a food item, " << index << "/10 entered so far: ";
        cin >> listOfFoods[index];
    }
    cout << "You entered: " << listOfFoods[0];
    for(int index = 1; index < amount;   index){
        cout << ", " << listOfFoods[index];
    }
    cout << endl;

    /* Adds two names */
    cout << "Add a food item, 0/2 entered so far: ";
    cin >> newEntry;
    listOfFoods = addEntry(listOfFoods, amount, newEntry);

    cout << "Add one last food item, 1/2 entered so far: ";
    cin >> newEntry;
    listOfFoods = addEntry(listOfFoods, amount, newEntry);

    print(listOfFoods, amount);

    /* Removes a name from the list */
    cout << "Of the list above, choose one that you would like to remove." << endl;
    cin >> removeEntry;
    listOfFoods = deleteEntry(listOfFoods, amount, removeEntry);
    cout << "You removed " << removeEntry << " from the list." << endl;

    print(listOfFoods, amount);

    listOfFoods = clearEntries(listOfFoods, amount);
    return 0;
}

string* deleteEntry(string* dynamicArray, int& size, string entryToDelete) {
    // you implement this. Please read Programming Project 6 on page 536
    // you may watch video notes to get the idea

    for (int i = 0; i < size;   i) {
        if (dynamicArray[i] == entryToDelete) {
            string *newArray = new string[size - 1];

            for (int k = 0; k < i;   k) {
                newArray[k] = dynamicArray[k];
            }

            for (int k = i   1; k < size;   k) {
                newArray[k-1] = dynamicArray[k];
            }

            delete[] dynamicArray;
            --size;

            return newArray;
        }
    }

    return dynamicArray;
}

void print(const string* dynamicArray, int size){
    // you implement this.
    // you may watch video notes to get the idea

    cout << "The list of the entries are as follows.\n";
    for(int index = 0; index < size;   index){
        cout << dynamicArray[index] << " is index " << index << " and the "<< index 1 << " item in the list.\n";
    }
}

string* addEntry(string* dynamicArray, int& size, string newEntry){
    string *mostRecentArray = new string[size   1]; //Increases the array by one

    for (int i = 0; i < size;   i){
        mostRecentArray[i] = dynamicArray[i];
    }

    mostRecentArray[size] = newEntry;
    size  ;

    delete[] dynamicArray;
    return mostRecentArray;
}

string* clearEntries(string* dynamicArray, int& size) {
    delete[] dynamicArray;
    size = 0;
    return nullptr;
}

Now, I understand that this is likely just a learning exercise for you, and that is fine, but just know that this is not how you should write real-world code in production. You really should be using std::vector instead, let it do all of the hard work of managing memory for you, eg:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

void print(const vector<string>& vec);

int main(){
    /* ADD/DELETE ENTRIES FROM AN ARRAY*/

    string newEntry, removeEntry;
    vector<string> listOfFoods(10); // Size is 10 entries

    cout << "Now enter 10 foods." << endl;
    for(int index = 0; index < amount;   index) {
        cout << "Enter a food item, " << index << "/10 entered so far: ";
        cin >> listOfFoods[index];
    }
    cout << "You entered: " << listOfFoods[0];
    for(int index = 1; index < amount;   index){
        cout << ", " << listOfFoods[index];
    }
    cout << endl;

    /* Adds two names */
    cout << "Add a food item, 0/2 entered so far: ";
    cin >> newEntry;
    listOfFoods.push_back(newEntry);

    cout << "Add one last food item, 1/2 entered so far: ";
    cin >> newEntry;
    listOfFoods.push_back(newEntry);

    print(listOfFoods);

    /* Removes a name from the list */
    cout << "Of the list above, choose one that you would like to remove." << endl;
    cin >> removeEntry;
    auto iter = find(listOfFoods.begin(), listOfFoods.end(), removeEntry);
    if (iter != listOfFoods.end()) {
        listOfFoods.erase(iter);
        cout << "You removed " << removeEntry << " from the list." << endl;
    }
    else {
        cout << removeEntry << " was not found in the list." << endl;
    }

    print(listOfFoods);

    return 0;
}

void print(const vector<string>& vec){
    // you implement this.
    // you may watch video notes to get the idea

    cout << "The list of the entries are as follows.\n";
    for (size_t index = 0; index < dynamicArray.size();   index){
        cout << dynamicArray[index] << " is index " << index << " and the "<< index 1 << " item in the list.\n";
    }
}

CodePudding user response:

Figured it out!

void print(const string* dynamicArray, int size){
    // you implement this.
    // you may watch video notes to get the idea

    cout << "The list of the entries are as follows.\n";
    for(int index = 0; index < size; index   ){
        cout << dynamicArray[index] << " is index " << index << " and the "<< index 1 << " item in the list.\n";
    }
}
  •  Tags:  
  • Related