Question

In: Computer Science

In C++: Set up a vector using an extendable array and name it ArrayVector (use an...

In C++: Set up a vector using an extendable array and name it ArrayVector (use an extendable array). It can be a template/generic class or you can set it up with a certain data type. Use a test driver to try out your ArrayVector by performing various operations.

Solutions

Expert Solution

#include <iostream>

using namespace std;

class svec {
        private:
        string *ds;
        int len, num;

        public:

        svec() {
                ds = NULL;
                len = 0;
                num = 0;
        }
        svec(int n) {
                if(n < 0) {
                        n = 0;
                }

                if(n != 0) {
                        ds = new string[n];
                } else {
                        ds = NULL;
                }
                len = n;
                num = 0;
        }
        svec(int n, string s) {
                if(n < 0) {
                        n = 0;
                }

                if(n != 0) {
                        ds = new string[n];
                        for(int i=0; i<n; i++) {
                                ds[i] = s;
                        }
                        num = n;
                } else {
                        ds = NULL;
                        num = 0;
                }
                len = n;
        }
        int size() {
                return num;
        }
        string front() {
                if(num == 0) {
                        return "";
                }
                return ds[0];
        }
        string back() {
                if(num == 0) {
                        return "";
                }
                return ds[num-1];
        }
        string get(int i) {
                if(i < 0 || i >= num) {
                        return NULL;
                }
                return ds[i];
        }
        void cleanup() {
                delete [] ds;
                len = 0;
                num = 0;
                ds = NULL;
        }
        void push_back(string s) {
                if(len == 0) {
                        len = 10;
                        ds = new string[len];
                        ds[0] = s;
                        num = 1;
                        return;
                } else if(num < len) {
                        ds[num++] = s;
                } else if(len == num) {
                        int newLen = len + 10;
                        string *tmp = new string[newLen];

                        for(int i=0; i<len; i++) {
                                tmp[i] = ds[i];
                        }
                        len = newLen;
                        ds[num++] = s;
                        delete [] ds;
                        ds = tmp;
                }

        }
};
int main() {
        // this is a string vector implementation.
        svec myVector;
        myVector.push_back("First");
        myVector.push_back("Second");
        myVector.push_back("third");
        myVector.push_back("Fourth");
        myVector.push_back("Fifth");
        myVector.push_back("Sixth");
        myVector.push_back("Seventh");
        myVector.push_back("Eighth");

        cout << "Vector size: " << myVector.size() << endl;
        for(int i=0; i<myVector.size(); i++) {
                cout << myVector.get(i) << endl;
        }

        cout << "Front: " << myVector.front() << endl;
        return 0;
}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input #...
How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input # of rows: 2 Input Row 1: 9 8 7 6 3 2 1 5 4 Input Row 2: 1 2 4 3 5 6 9 8 7 Output 1,2,3,4,5,6,7,8,9 1,2,3,4,5,6,7,8,9
Verify using an example that vector a + (vector b * vector c) is not equal...
Verify using an example that vector a + (vector b * vector c) is not equal to (vector a + vector b) * (vector a + vector c) explain the problem that arrises
Pass or Fail (C++) Given a vector, where each element in the vector is a [name,...
Pass or Fail (C++) Given a vector, where each element in the vector is a [name, grade] pair sort range by name, partition range into pass and fail, preserving alphabetical order within partition. The grades are the number of points earned (600 points earns a passing grade). std::vector<std::pair<std::string, int>> v { {"josh", 851}, {"mark", 600}, {"charles", 412}, {"sebnem", 1000}, {"abdol", 905}, {"imen", 300} }; --------EXPECTED OUTPUT------------------------------------------------- std::vector<std::pair<std::string, int>> w { {"abdol", 905}, {"josh", 851}, {"mark", 600}, {"sebnem", 1000}, // Everyone...
Write a statement to call prepare Arr to set values for the array (Using C++) #include...
Write a statement to call prepare Arr to set values for the array (Using C++) #include using namespace std; const int NUM = 10; void prepareArr(int a[]); int countEven (int b[]); int main() { int arr[NUM]; // write a statement to call prepareArr to set values for the array // write a statement to call countEven and print the data returned for(int i = 0; i cout << arr[i] <<" "; cout < return 0; } void prepareArr(int a[]) {...
In c++ how do I search a vector for a name and then determine if the...
In c++ how do I search a vector for a name and then determine if the name is found?
In C++ write an implementation of the ADT sorted list that uses a resizable array (vector...
In C++ write an implementation of the ADT sorted list that uses a resizable array (vector class of C++ STL) to represent the list items.
Write a complete C program that searches an element in array using pointers. Please use the...
Write a complete C program that searches an element in array using pointers. Please use the function called search to find the given number. //Function Prototype void search (int * array, int num, int size)
In C# using a Console App, create an array that stores 20 integer values. Use the...
In C# using a Console App, create an array that stores 20 integer values. Use the random number generator to select an integer value between 0 and 10. Store the 20 random integers in the array. Once the array has been created: Print the values in the array. Print the values in the array in reverse order. Sort and print the values in the array in ascending order. Sort and print the values in the array in descending order. Count...
In C create an array of 4 integers. Assign a pointer to the array. Use the...
In C create an array of 4 integers. Assign a pointer to the array. Use the pointer to find the average value of the elements in the array and display the results on the screen.
2. Set up both the vector of state probabilities and the matrix of transition probabilities given...
2. Set up both the vector of state probabilities and the matrix of transition probabilities given the following information: Store 1 currently has 40% of the market; store 2 currently has 60% of the market. In each period, store 1 customers have an 80% chance of returning; 20% of switching to store 2. In each period, store 2 customers have a 90% chance of returning; 10% of switching to store 1. a. Find the percentage of market for each store...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT