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 convert this java code to C language. There is no string can be used...
I need convert this java code to C language. There is no string can be used in C. Thank you! import java.util.Scanner; public class Nthword { public static void main( String args[] ) { String line; int word; Scanner stdin = new Scanner(System.in); while ( stdin.hasNextLine() ) { line = stdin.nextLine(); word = stdin.nextInt(); stdin.nextLine(); // get rid of the newline after the int System.out.println( "Read line: \"" + line + "\", extracting word [" + word + "]" );...
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 =...
Hello, I have created the following code in C++. The goal of this code is to...
Hello, I have created the following code in C++. The goal of this code is to read 3 types of employee information(String/*Name*/, String/*Phone number*/,Int/*Office number*/, ) from a .txt file and store that information into an array which can be accessed by various functions. The code below is within a header file, and im trying to define some functions (within a .cpp file) of my class to carry out the following tasks. I have already managed to define a couple...
C++ Programming Convert problems 6 in to template classes, of week 5 and week 4. Test...
C++ Programming Convert problems 6 in to template classes, of week 5 and week 4. Test each with Implicit int, float, double, long int. Test each with explicit int, float, double, long int. Problem 6: //Box.h #include<iostream> using namespace std; class Box { public:    double Length;    double Width;    double Height; public:    //Default constructor    Box();    //Parameterized constructor    Box(double ,double,double);    //Setter methods    void setWidth ( double n ) ;    void setDepth ( double n ) ;    void setHeight ( double n );...
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 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 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 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 <...
Hello, I need to divide my code in this different parts. Use a standard approach to...
Hello, I need to divide my code in this different parts. Use a standard approach to source files in this assignment: a .cpp for each function (you have at least two – main and ageCalc) a header file for the student struct, properly guarded Code: #include <iostream> #include <string> #include <fstream> #include <algorithm> #include <vector> #include <iomanip> using namespace std; #define nullptr NULL #define MAX_SIZE 100 struct Student {     string firstName;     char middleName;     string lastName;     char collegeCode;     int locCode;     int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT