Question

In: Computer Science

C++ Hello .I need to convert this code into template and then test the template with...

C++

Hello .I need to convert this code into template and then test the template with dynamic array of strings also if you can help me move the function out of the class that would be great.also There is a bug where the memory was being freed without using new operator. I cant seem to find it thanks in advance

#include
using namespace std;

class DynamicStringArray
{
   private:
       string *dynamicArray;
       int size;
   public:
       DynamicStringArray(DynamicStringArray &A)
       {
           dynamicArray=A.dynamicArray;
           size=A.size;
       }
       ~DynamicStringArray()
       {
           delete []dynamicArray;
       }
       DynamicStringArray &operator =(DynamicStringArray &A)
       {
           dynamicArray=new string[A.size];
           for(int i=0; i            {
               dynamicArray[i]=A.getEntry(i);
           }
       }
       DynamicStringArray()
           {
               dynamicArray=NULL;
               size=0;
           }
       void addEntry(string input)
       {
           string *temp=new string[size+1];
           for(int i=0; i            {
               temp[i]=dynamicArray[i];
           }
           temp[size]=input;
           size++;
           delete []dynamicArray;
           dynamicArray=temp;
          
       }
       bool deleteEntry(string input)
       {
           bool found=false;
           for(int i=0; i            {
               if(dynamicArray[i]==input)
               {
                   found=true;
                   break;
               }
           }
           if(!found)
           {
               return false;
           }
           else
           {
           string *temp=new string[size-1];
           int j=0;
           for(int i=0; i            {
               if(dynamicArray[i]!=input)
               {
                   temp[j]=dynamicArray[i];
                   j++;
               }
           }
           size--;
           delete []dynamicArray;
           dynamicArray=temp;
           }
       }
       string getEntry(int index)
       {
           if(index<0 || index>=size)
           {
               cout<<"Index out of range"<                return "";
           }
           return dynamicArray[index];
       }
       int getSize()
       {
           return size;
       }
      
  
};
int main()
{
DynamicStringArray names;

// List of names
names.addEntry("Frank");
names.addEntry("Wiggum");
names.addEntry("Nahasapeemapetilon");
names.addEntry("Quimby");
names.addEntry("Flanders");
// Output list
cout << "List of names:" << endl;
for (int i = 0; i < names.getSize(); i++)
cout << names.getEntry(i) << endl;
cout << endl;

// Add and remove some names
names.addEntry("Spuckler");
cout << "After adding a name:" << endl;
for (int i = 0; i < names.getSize(); i++)
cout << names.getEntry(i) << endl;
cout << endl;

names.deleteEntry("Nahasapeemapetilon");
cout << "After removing a name:" << endl;
for (int i = 0; i < names.getSize(); i++)
cout << names.getEntry(i) << endl;
cout << endl;

names.deleteEntry("Skinner");
cout << "After removing a name that isn't on the list:" << endl;
for (int i = 0; i < names.getSize(); i++)
cout << names.getEntry(i) << endl;
cout << endl;

names.addEntry("Muntz");
cout << "After adding another name:" << endl;
for (int i = 0; i < names.getSize(); i++)
cout << names.getEntry(i) << endl;
cout << endl;

// Remove all of the names by repeatedly deleting the last one
while (names.getSize() > 0) {
names.deleteEntry(names.getEntry(names.getSize() - 1));
}

cout << "After removing all of the names:" << endl;
for (int i = 0; i < names.getSize(); i++)
cout << names.getEntry(i) << endl;
cout << endl;

names.addEntry("Olivia");
cout << "After adding a name:" << endl;
for (int i = 0; i < names.getSize(); i++)
cout << names.getEntry(i) << endl;
cout << endl;

cout << "Testing copy constructor" << endl;
DynamicStringArray names2(names);
// Remove Olivia from names
names.deleteEntry("Olivia");
cout << "Copied names:" << endl;
for (int i = 0; i < names2.getSize(); i++)
cout << names2.getEntry(i) << endl;
cout << endl;

cout << "Testing assignment" << endl;
DynamicStringArray names3 = names2;
// Remove Olivia from names2
names2.deleteEntry("Olivia");
cout << "Copied names:" << endl;
for (int i = 0; i < names3.getSize(); i++)
cout << names3.getEntry(i) << endl;
cout << endl;

cout << "Enter a character to exit." << endl;
char wait;
cin >> wait;
return 0;
}

Solutions

Expert Solution

If you have any doubts, please give me comment...

#include <iostream>

using namespace std;

template <class T>

class DynamicStringArray

{

private:

    T *dynamicArray;

    int size;

public:

    DynamicStringArray(DynamicStringArray<T> &A);

    ~DynamicStringArray();

    DynamicStringArray &operator=(DynamicStringArray &A);

    DynamicStringArray();

    void addEntry(T input);

    bool deleteEntry(T input);

    T getEntry(int index);

    int getSize();

};

template <class T>

DynamicStringArray<T>::DynamicStringArray(DynamicStringArray<T> &A)

{

    size = A.size;

    dynamicArray = new T[size];

    for (int i = 0; i < size; i++)

    {

        dynamicArray[i] = A.dynamicArray[i];

    }

}

template <class T>

DynamicStringArray<T>::~DynamicStringArray()

{

    delete[] dynamicArray;

}

template <class T>

DynamicStringArray<T> &DynamicStringArray<T>::operator=(DynamicStringArray &A)

{

    dynamicArray = new T[A.size];

    for (int i = 0; i < size; i++)

    {

        dynamicArray[i] = A.getEntry(i);

    }

}

template <class T>

DynamicStringArray<T>::DynamicStringArray()

{

    dynamicArray = NULL;

    size = 0;

}

template <class T>

void DynamicStringArray<T>::addEntry(T input)

{

    T *temp = new T[size + 1];

    for (int i = 0; i < size; i++)

    {

        temp[i] = dynamicArray[i];

    }

    temp[size] = input;

    size++;

    delete[] dynamicArray;

    dynamicArray = temp;

}

template <class T>

bool DynamicStringArray<T>::deleteEntry(T input)

{

    bool found = false;

    for (int i = 0; i < size; i++)

    {

        if (dynamicArray[i] == input)

        {

            found = true;

            break;

        }

    }

    if (!found)

    {

        return false;

    }

    else

    {

        T *temp = new T[size - 1];

        int j = 0;

        for (int i = 0; i < size; i++)

        {

            if (dynamicArray[i] != input)

            {

                temp[j] = dynamicArray[i];

                j++;

            }

        }

        size--;

        delete[] dynamicArray;

        dynamicArray = temp;

    }

}

template <class T>

T DynamicStringArray<T>::getEntry(int index)

{

    if (index < 0 || index >= size)

    {

        cout << "Index out of range" << endl;

        return "";

    }

    return dynamicArray[index];

}

template <class T>

int DynamicStringArray<T>::getSize()

{

    return size;

}

int main()

{

    DynamicStringArray<string> names;

    // List of names

    names.addEntry("Frank");

    names.addEntry("Wiggum");

    names.addEntry("Nahasapeemapetilon");

    names.addEntry("Quimby");

    names.addEntry("Flanders");

    // Output list

    cout << "List of names:" << endl;

    for (int i = 0; i < names.getSize(); i++)

        cout << names.getEntry(i) << endl;

    cout << endl;

    // Add and remove some names

    names.addEntry("Spuckler");

    cout << "After adding a name:" << endl;

    for (int i = 0; i < names.getSize(); i++)

        cout << names.getEntry(i) << endl;

    cout << endl;

    names.deleteEntry("Nahasapeemapetilon");

    cout << "After removing a name:" << endl;

    for (int i = 0; i < names.getSize(); i++)

        cout << names.getEntry(i) << endl;

    cout << endl;

    names.deleteEntry("Skinner");

    cout << "After removing a name that isn't on the list:" << endl;

    for (int i = 0; i < names.getSize(); i++)

        cout << names.getEntry(i) << endl;

    cout << endl;

    names.addEntry("Muntz");

    cout << "After adding another name:" << endl;

    for (int i = 0; i < names.getSize(); i++)

        cout << names.getEntry(i) << endl;

    cout << endl;

    // Remove all of the names by repeatedly deleting the last one

    while (names.getSize() > 0)

    {

        names.deleteEntry(names.getEntry(names.getSize() - 1));

    }

    cout << "After removing all of the names:" << endl;

    for (int i = 0; i < names.getSize(); i++)

        cout << names.getEntry(i) << endl;

    cout << endl;

    names.addEntry("Olivia");

    cout << "After adding a name:" << endl;

    for (int i = 0; i < names.getSize(); i++)

        cout << names.getEntry(i) << endl;

    cout << endl;

    cout << "Testing copy constructor" << endl;

    DynamicStringArray<string> names2(names);

    // Remove Olivia from names

    names.deleteEntry("Olivia");

    cout << "Copied names:" << endl;

    for (int i = 0; i < names2.getSize(); i++)

        cout << names2.getEntry(i) << endl;

    cout << endl;

    cout << "Testing assignment" << endl;

    DynamicStringArray<string> names3 = names2;

    // Remove Olivia from names2

    names2.deleteEntry("Olivia");

    cout << "Copied names:" << endl;

    for (int i = 0; i < names3.getSize(); i++)

        cout << names3.getEntry(i) << endl;

    cout << endl;

    cout << "Enter a character to exit." << endl;

    char wait;

    cin >> wait;

    return 0;

}


Related Solutions

I need an idea of Java code that will convert an integer (1 to 3,999) into...
I need an idea of Java code that will convert an integer (1 to 3,999) into roman numerals using if statements; arrays and loops sadly aren't allowed and that's all I can come up with.
this is a python code that i need to covert to C++ code...is this possible? if...
this is a python code that i need to covert to C++ code...is this possible? if so, can you please convert this pythin code to C++? def main(): endProgram = 'no' print while endProgram == 'no': print # declare variables notGreenCost = [0] * 12 goneGreenCost = [0] * 12 savings = [0] * 12 months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] getNotGreen(notGreenCost, months) getGoneGreen(goneGreenCost, months) energySaved(notGreenCost, goneGreenCost, savings) displayInfo(notGreenCost, goneGreenCost, savings, months)...
i need code in javascript or htmlt convert 0 to 999 numbers into word
i need code in javascript or htmlt convert 0 to 999 numbers into word
Convert the following C++ code into MIPS assembely. For testing I will be change the values...
Convert the following C++ code into MIPS assembely. For testing I will be change the values for q,y,x with few different values. //q -> $s0 //y -> $s1 //x -> $s2 int main(){ int q = 5; int y = 17; int x = 77; if ( q < 10){ cout << "inside if"; } elseif ( x > 0 || y < 10) { cout << "inside elseif"; } else { cout << "inside else"; } }
Convert C code to MIPS assembly language 1) sum = 0; for(i=0; I < 1000; i++)...
Convert C code to MIPS assembly language 1) sum = 0; for(i=0; I < 1000; i++) sum = sum + I; printf(“sum=%d”, sum); 2) int i, v[10]; int min, k; for(i=0; i < 10; i++) scanf(“%d”, &v[i]); min = v[0] for(i=1; I < 10; i++) if(min > v[i]){ min = v[i] k = I; } printf(“%d=>%d”, k, min);
C Code! I need all of the \*TODO*\ sections of this code completed. For this dictionary.c...
C Code! I need all of the \*TODO*\ sections of this code completed. For this dictionary.c program, you should end up with a simple English-French and French-English dictionary with a couple of about 350 words(I've provided 5 of each don't worry about this part). I just need a way to look up a word in a sorted array. You simply need to find a way to identify at which index i a certain word appears in an array of words...
Hello, I need the Matlab code of the Fourier Transform without using the Matlab functions fft...
Hello, I need the Matlab code of the Fourier Transform without using the Matlab functions fft and dft. Applied to discrete signals. If you can with an example.Thank you!!
This problem needs to be solved with source code. I need a C++ program that will...
This problem needs to be solved with source code. I need a C++ program that will help me solve this question. I need it in C++, please. Writing with comments so it maybe cleared. 1.2. We received the following ciphertext which was encoded with a shift cipher: xultpaajcxitltlxaarpjhtiwtgxktghidhipxciwtvgtpilpit ghlxiwiwtxgqadds. 1. Perform an attack against the cipher based on a letter frequency count: How many letters do you have to identify through a frequency count to recover the key? What is...
I need to complete this C++ program. The instructions are in the comments inside the code...
I need to complete this C++ program. The instructions are in the comments inside the code below: ------------------------------------------------------------------------- Original string is: this is a secret! Encypted string is: uijt!jt!b!tfdsfu" Decrypted string is: this is a secret! //Encoding program //Pre-_____? //other necessary stuff here int main() { //create a string to encrypt using a char array cout<< "Original string is: "<<string<<endl; encrypt(string); cout<< "Encrypted string is: "<<string<<endl; decrypt(string); cout<<"Decrypted string is: "<<string<<endl; return 0; } void encrypt(char e[]) { //Write implementation...
c++ I need a code that will fill an array size of 1000, an array of...
c++ I need a code that will fill an array size of 1000, an array of size 2000, and an array size of 10000, with random int values. Basically like this: array1[1000] = filled all with random numbers array2[2000] = filled all with random numbers array3[10000] = filled all with random numbers C++ no need for print
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT