Thread: Help with creating a dynamic array that can add and delete elements

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    14

    Help with creating a dynamic array that can add and delete elements

    Hey Im working on this program for school, pretty much I have to create an array of five strings and create and use two functions, addEntry and deleteEntry. These should allow you to change the size and add elements to the array or emulate the functions of a vector as the book says. So far I have the addEntry working but when I tried to put in my deleteEntry I now get an error saying the .exe has stopped working. Can you help me out? I muct have made the deleteEntry Function wrong or something.

    Here's my code:

    Code:
    //COT 3002
    //The purpose of this program is to create a function that can add or remove elements to an array
    
    #include <iostream>
    #include <cstring>
    #include <string>
    using namespace std;
    
    //Function Prototypes
    string* addEntry(string *dynamicArray, int &size, string newEntry);
    string* deleteEntry(string *dynamicArray, int &size, string entryToDelete);
    
    int main()
    {
    	int size=5;
    	string *strPtr=new string[size];
    	int i=0;
    	strPtr[i++] = "Frink";
    	strPtr[i++] = "Wiggum";
    	strPtr[i++] = "Nahasapeemapetilon";
    	strPtr[i++] = "Quimby";
    	strPtr[i++] = "Flanders";
    	cout<<"Initial List:"<<endl;
    	for(i=0; i < size; i++) 
    		cout << i << ": " << strPtr[i] << endl;
    	cout<<"\nAfter Adding an Entry:"<<endl;
    	strPtr=addEntry(strPtr, size,"Spuckler");
    	for(i=0; i < size; i++) 
    		cout << i << ": " << strPtr[i] << endl;
    	strPtr=deleteEntry(strPtr, size, "Nahasapeemapetilon");
    	for(i=0; i < size; i++) 
    		cout << i << ": " << strPtr[i] << endl;
    
    
    	return 0;
    }
    
    string *deleteEntry(string *dynamicArray, int &size, string entryToDelete)
    {// create a new dynamic array 1 element larger than dynamicArray
       string *newArray = new string[size - 1];
    
       // copy all elements from dynamicArray into new array
       for(int i = 0; i < size; i++)
       {
           if(dynamicArray[i]!=entryToDelete)
    		   newArray[i] = dynamicArray[i];
       }
    
       size--;
    
       // delete dynamicArray
       delete [] dynamicArray;
    
       // and return the new array
       return newArray;
    }
    
       
    
    string *addEntry(string *dynamicArray, int &size, string newEntry)
    {
       // create a new dynamic array 1 element larger than dynamicArray
       string *newArray = new string[size + 1];
    
       // copy all elements from dynamicArray into new array
       for(int i = 0; i < size; i++)
       {
           newArray[i] = dynamicArray[i];
       }
    
       // add the new entry onto the end of the new array
       newArray[size] = newEntry;
    
       // increment size
       size++;
    
       // delete dynamicArray
       delete [] dynamicArray;
    
       // and return the new array
       return newArray;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You're overflowing your array.

    > if(dynamicArray[i]!=entryToDelete)
    > newArray[i] = dynamicArray[i];
    Once you've gone past the deleted element, the index to newArray is one LESS than the index subscripting dynamicArray
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    14
    Thank you, I now changed the code of that function to this:
    Code:
    string *deleteEntry(string *dynamicArray, int &size, string entryToDelete)
    {	int stop;
    	// create a new dynamic array 1 element smaller than dynamicArray
       string *newArray = new string[size - 1];
    
       // copy all elements from dynamicArray into new array
       for(int i = 0; i < size - 1; i++)
       {
           if(dynamicArray[i]!=entryToDelete)
    		   newArray[i] = dynamicArray[i];
    	   else 
    	   {stop=i;
    	   for(int i = stop; i < size-1; i++)
    		   newArray[i] = dynamicArray[i+1];}
       }
    
       size--;
    
       // delete dynamicArray
       delete [] dynamicArray;
    
       // and return the new array
       return newArray;
    }
    I no longer get the error, but I now have a new problem. It deletes the entry and put the next entry in place of it like I want it to, but then prints that same entry one more time, like if had i had 12345 before and want to delete 2, I have 1334, when I want 1345. Please help me out.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    14
    Nevermind I fixed it, I used this code:
    Code:
    string *deleteEntry(string *dynamicArray, int &size, string entryToDelete)
    {	int stop;
    	// create a new dynamic array 1 element smaller than dynamicArray
       string *newArray = new string[size - 1];
    
       // copy all elements from dynamicArray into new array
       for(int i = 0; i < size - 1; i++)
       {
           if(dynamicArray[i]!=entryToDelete)
    		   newArray[i] = dynamicArray[i];
    	   else 
    	   {stop=i;
    	   break;}}
    	 for(int i = stop; i < size-1; i++)
    		   newArray[i] = dynamicArray[i+1];
    
    
       size--;
    
       // delete dynamicArray
       delete [] dynamicArray;
    
       // and return the new array
       return newArray;
    }
    Thank you for your help

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Sorry, but your indentation needs a lot of work. For example, it would have been easier to read if you had written the function like this:
    Code:
    string* deleteEntry(string* dynamicArray, int& size, string entryToDelete)
    {
        int stop;
        // create a new dynamic array 1 element smaller than dynamicArray
        string* newArray = new string[size - 1];
    
        // copy all elements from dynamicArray into new array
        for (int i = 0; i < size - 1; i++)
        {
            if (dynamicArray[i] != entryToDelete)
                newArray[i] = dynamicArray[i];
            else
            {
                stop=i;
                break;
            }
        }
    
        for (int i = stop; i < size - 1; i++)
            newArray[i] = dynamicArray[i + 1];
    
        size--;
    
        // delete dynamicArray
        delete [] dynamicArray;
    
        // and return the new array
        return newArray;
    }
    However, there is one major problem with your implementation of deleteEntry: what happens if none of the strings in the dynamic array are equal to entryToDelete?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    14
    Yeah I know thats true, I realized that and tried to fix it by moving the size-- into the else statement but that messed everything else up, I'm not to sure how to fix it.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Unless your assignment gives you specific instructions about what to do when the entry to delete is not present, my guess would be to do nothing at all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a dynamic array of char
    By Sharke in forum C++ Programming
    Replies: 12
    Last Post: 06-23-2009, 12:25 AM
  2. Vertical Scroller laser cannon problem
    By Swarvy in forum Game Programming
    Replies: 5
    Last Post: 05-02-2009, 06:30 PM
  3. Resource Management question..
    By Raigne in forum C++ Programming
    Replies: 37
    Last Post: 03-08-2008, 09:36 AM
  4. Dynamic Array Resizing
    By dld333 in forum C++ Programming
    Replies: 13
    Last Post: 11-04-2005, 12:13 AM
  5. Add and delete functions
    By Ana Val sazi in forum C++ Programming
    Replies: 5
    Last Post: 06-18-2002, 09:59 PM