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

Hello, I need to convert this java array into an array list as I am having...
Hello, I need to convert this java array into an array list as I am having trouble please. import java.util.Random; import java.util.Scanner; public class TestCode { public static void main(String[] args) { String choice = "Yes"; Random random = new Random(); Scanner scanner = new Scanner(System.in); int[] data = new int[1000]; int count = 0; while (!choice.equals("No")) { int randomInt = 2 * (random.nextInt(5) + 1); System.out.println(randomInt); data[count++] = randomInt; System.out.print("Want another random number (Yes / No)? "); choice =...
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
i need the whole html code Make a layout template that contains a header and two...
i need the whole html code Make a layout template that contains a header and two paragraphs. Use float to line up the two paragraphs as columns side by side. Give the header and two paragraphs a border and/or a background color so you can see where they are.
I need to convert the following into C++. The general idea is to create an application...
I need to convert the following into C++. The general idea is to create an application that can support 10 ID's and 10 grades. It needs to calculate the grade average of said ID, and determine if it is an below or above a certain average, ergo A or C. string[10] studentIDArray int[10] gradeArray int averageGrade for(int i = 0; i < gradeArray; i++) { averageGrade += gradeArray[i] } averageGrade = averageGrade / sizeof(gradeArray) for(int i = 0; i <...
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!!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT