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...
Write a C++ program using dynamic arrays that allows the user to enter the last names...
Write a C++ program using dynamic arrays that allows the user to enter the last names of the candidates in a local election and the number of votes received by each candidate. The program must ask the user for the number of candidates and then create the appropriate arrays to hold the data. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should...
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...
1. Maintain two arrays for use in “translating” regular text to “leetspeak”: (a) One array will...
1. Maintain two arrays for use in “translating” regular text to “leetspeak”: (a) One array will contain the standard characters: char[] letters = {'a', 'e', 'l', 'o', 's', 't'}; (b) The other will contain the corresponding symbols: char[] symbols = {'@', '3', '1', '0', '$', '7'}; 2. Prompt the user to enter a full sentence with all lower-case characters, then store it in a variable to be used later. (a) You can (optionally) manually convert the string to all lower-case...
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,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT