Question

In: Computer Science

Description: One problem with dynamic arrays is that once the array is created using the new...

Description: One problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector. This assignment asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings.

The class should have the following:

• A private member variable called dynamicArray that references a dynamic array of type string. • A private member variable called size that holds the number of entries in the array.

• A default constructor that sets the dynamic array to nullptr and sets size to 0.

• A function that returns size.

• A function named addEntry that takes a string as input. The function should create a new dynamic array one element larger than dynamicArray, copy all elements from dynamicArray into the new array, add the new string onto the end of the new array, increment size, delete the old dynamicArray, and then set dynamicArray to the new array.

• A function named deleteEntry that takes a string as input. The function should search dynamicArray for the string. If not found, it returns false. If found, it creates a new dynamic array one element smaller than dynamicArray. It should copy all elements except the input string into the new array, delete dynamicArray, decrement size, and return true.

• A function named getEntry that takes an integer as input and returns the string at that index in dynamicArray. It should return nullptr if the index is out of dynamicArray’s bounds. • A copy constructor that makes a copy of the input object’s dynamic array.

• Overload the copy assignment operator so that the dynamic array is properly copied to the target object.

• A destructor that frees up the memory allocated to the dynamic array.

Solutions

Expert Solution

#include<bits/stdc++.h>
using namespace std;

class DynamicStringArray{
    string* dynamicArray;
    int size;

    public:
    DynamicStringArray(){
        dynamicArray = nullptr;
        size = 0;
    }
    int getsize()const {
        return size;
    }
    void addEntry(string data){
        string* temp = new string[size + 1];
        for(int i=0; i<size; i++){
            temp[i] = dynamicArray[i];
        }
        temp[size] = data;
        size++;
        delete[] dynamicArray;
        dynamicArray = temp;
    }
    bool deleteEntry(string data){
        bool found = false;
        int ind ;
        for(int i=0; i<size; i++){
            if(dynamicArray[i] == data){
                found  = true;
                ind = i;
                break;
            }
        } 
        if(found){
            string* temp = new string[size - 1];
            int j = 0;
            for(int i=0; i<size; i++){
                if(i != ind) 
                    temp[j++] = dynamicArray[i];
            }
            size--;
            delete[] dynamicArray;
            dynamicArray = temp;
            return true;
        }
        return false;
    }
    string  getEntry(int ind){
        if(ind >= size) return nullptr;
        else return dynamicArray[ind];
    }
    DynamicStringArray(const DynamicStringArray& other){
        size = other.getsize();
        dynamicArray = new string[size];
        for(int i=0; i<size; i++)
            dynamicArray[i] = other.dynamicArray[i];
    }
    void operator= (const DynamicStringArray& other) {
        delete[] dynamicArray;
        size = other.getsize();
        dynamicArray = new string[size];
        for(int i=0; i<size; i++)
            dynamicArray[i] = other.dynamicArray[i];

    }
    ~DynamicStringArray(){
        delete[] dynamicArray;
    }



};
int main(){

    DynamicStringArray aa ;
    aa.addEntry(string("i am xyz"));
    aa.addEntry(string("i am saw"));
    aa.deleteEntry(string("i am saw"));
    cout<<aa.getsize()<<endl;
    cout<<aa.getEntry(0);
    return 0;
     
}

Related Solutions

Write a code in c++ using dynamic array of structure and dynamic array list. Make a...
Write a code in c++ using dynamic array of structure and dynamic array list. Make a dummy list for a company which stores following information about its customers. Customer ID Customer Name Gender Total items purchased Item category 20% discount in percentage of total purchase amount. Use dynamic array to save at least 20 items by dividing them into 3 different categories. Make a dummy list of items that company sells by dividing them into two categorizes. Items has following...
Problem 1: Unsorted arrays Given an array of integers that is unsorted, implement the following functions:...
Problem 1: Unsorted arrays Given an array of integers that is unsorted, implement the following functions: • myAdd ( ): add an integer d to the array; return 0 if the operation is successful; return a negative number otherwise. • search ( ): given an integer d, if d is found in the array, return the index of the cell containing d. Return a negative number otherwise (e.g., d is not found in the array). • myRemove ( ): Step...
USING MatLab (Arrays) Set ASIZE to 5. Write a program that creates an array of ASIZE...
USING MatLab (Arrays) Set ASIZE to 5. Write a program that creates an array of ASIZE numeric elements. Prompt the User for ASIZE numbers and store them in the array. After storing the values, calculate the sum of all the values in the array and display the sum. Modify the program you wrote for Problem 5 such that it calculates the product of all the values instead of the sum. Modify the program you wrote in Problem 6 such that...
5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, ....
5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, . . . , n] of positive integers, an integer K. Decide: Are there integers in A such that their sum is K. (Return T RUE or F ALSE) Example: The answer is TRUE for the array A = [1, 2, 3] and 5, since 2 + 3 = 5. The answer is FALSE for A = [2, 3, 4] and 8. Note that you...
One dimensional dynamic array Write a function that returns the number of integers in an input...
One dimensional dynamic array Write a function that returns the number of integers in an input file stream with the following interface: int findNumber(ifstream &x); Then, use this number to dynamically allocate an integer array. Write another function that reads each number in an input file stream and assign the value to the corresponding array element with the following interface: void assignNumber(ifstream &x, int y[ ]); In your main( ), first open “in.dat” as an input file. Next, apply findNumber(...
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
First assignment for C++. How do I setup this dynamic multiplication table using 2D arrays and...
First assignment for C++. How do I setup this dynamic multiplication table using 2D arrays and double pointers? The assignment asks to Write a program that displays a 2D multiplication table based on row and column value specified by the user. Perform a data validation to ensure the number of rows and columns given by the user exist on the interval [1, 10]. If possible, protect against inputs that contain symbols that would normally crash the program (i.e. letter, symbols,...
Solve the initial value problem once using power series method and once using the characteristic method....
Solve the initial value problem once using power series method and once using the characteristic method. Please show step for both 3) 3y”−y=0, y(0)=0,y’(0)=1 Note that 3y” refers to it being second order differential and y’ first
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105....
Exercises on Arrays –using C++programming 1. You want an array with the numbers 100 – 105. In the boxes below, fill in what your array should have. Fill in the index of each element below it. Array Index Open up your editor and write the following program: Declare an array that has the numbers 100 to 105. (How many elements are there in the array?) Print the array. Save your file and test it. Compare your results with your table...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT