Question
The following is the program: #include #include using namespace std; class BasicVector { private: int vector_size; int vector_capacity; int* data; void resize(); public: BasicVector(int); ~BasicVector();
The following is the program:
#include
using namespace std;
class BasicVector { private: int vector_size; int vector_capacity; int* data;
void resize();
public: BasicVector(int); ~BasicVector();
int& at(int); int& operator[](int); int& front(); int& back();
void push_back(int); void insert(int, int); void pop_back();
int size(); int capacity();
void print(); };
BasicVector::BasicVector(int capacity) { vector_size = 0;
if (capacity
BasicVector::~BasicVector() { while (vector_size > 0) pop_back();
delete [] data; }
void BasicVector::resize() { int* temp = data; data = new int[vector_capacity *= 2];
for (int i = 0; i
delete [] temp; }
int& BasicVector::at(int index) { if (0
int& BasicVector::operator[](int index) { if (0
int& BasicVector::front() { if (vector_size > 0) return data[0];
cout
int& BasicVector::back() { if (vector_size > 0) return data[vector_size - 1];
cout
void BasicVector::push_back(int value) { if (vector_size == vector_capacity) resize();
data[vector_size++] = value; }
void BasicVector::insert(int index, int value) { if (0 index; --i) data[i] = data[i-1]; ++vector_size; data[index] = value; } }
void BasicVector::pop_back() { if (vector_size > 0) data[--vector_size] = 0; }
int BasicVector::size() { return vector_size; }
int BasicVector::capacity() { return vector_capacity; }
void BasicVector::print() { cout
void interactive_mode() { int capacity, index, value; string cmd; cout > capacity; BasicVector vector(capacity);
cout
while(true) { cout "; cin >> cmd;
if (cmd == "at") { cin >> index; cout > index; cout > index >> value; vector.insert(index, value); } else if (cmd == "push") { cin >> value; vector.push_back(value); } else if (cmd == "front") { cout
int main() { interactive_mode();
return 0; }
The following is the get_datatype.cpp
int get_datatype() {
int choice;
do {
cout
cout
cout
cout
cout
cout
cout ";
cin >> choice;
} while (choice
return choice;
}
basic_integer_vector.cpp
#include
#include
using namespace std;
class BasicVector {
private:
int vector_size;
int vector_capacity;
int* data;
void resize();
public:
BasicVector(int);
~BasicVector();
int& at(int);
int& operator[](int);
int& front();
int& back();
void push_back(int);
void insert(int, int);
void pop_back();
int size();
int capacity();
void print();
};
BasicVector::BasicVector(int capacity) {
vector_size = 0;
if (capacity
vector_capacity = 16;
data = new int[16];
}
else {
int c = 16;
while (c
c *= 2;
vector_capacity = c;
data = new int[c];
}
}
BasicVector::~BasicVector() {
while (vector_size > 0)
pop_back();
delete [] data;
}
void BasicVector::resize() {
int* temp = data;
data = new int[vector_capacity *= 2];
for (int i = 0; i
data[i] = temp[i];
delete [] temp;
}
int& BasicVector::at(int index) {
if (0
return data[index];
cout
exit(1);
}
int& BasicVector::operator[](int index) {
if (0
return data[index];
cout
exit(1);
}
int& BasicVector::front() {
if (vector_size > 0)
return data[0];
cout
exit(1);
}
int& BasicVector::back() {
if (vector_size > 0)
return data[vector_size - 1];
cout
exit(1);
}
void BasicVector::push_back(int value) {
if (vector_size == vector_capacity)
resize();
data[vector_size++] = value;
}
void BasicVector::insert(int index, int value) {
if (0
if (vector_size == vector_capacity)
resize();
for (int i = vector_size; i > index; --i)
data[i] = data[i-1];
++vector_size;
data[index] = value;
}
}
void BasicVector::pop_back() {
if (vector_size > 0)
data[--vector_size] = 0;
}
int BasicVector::size() {
return vector_size;
}
int BasicVector::capacity() {
return vector_capacity;
}
void BasicVector::print() {
cout
for (int i = 0; i
cout
cout
}
void interactive_mode() {
int capacity, index, value;
string cmd;
cout
cin >> capacity;
BasicVector vector(capacity);
cout
while(true) {
cout ";
cin >> cmd;
if (cmd == "at") {
cin >> index;
cout
}
else if (cmd == "get") {
cin >> index;
cout
}
else if (cmd == "insert") {
cin >> index >> value;
vector.insert(index, value);
}
else if (cmd == "push") {
cin >> value;
vector.push_back(value);
}
else if (cmd == "front") {
cout
}
else if (cmd == "back") {
cout
}
else if (cmd == "pop") {
vector.pop_back();
}
else if (cmd == "size") {
cout
}
else if (cmd == "capacity") {
cout
}
else if (cmd == "print") {
vector.print();
}
else if (cmd == "quit") {
cout
break;
}
else {
cout
}
}
}
int main() {
interactive_mode();
return 0;
}
integer_vector_sample_program_run.txt
Page
of 2
ZOOM
Enter starting capacity of vector: 0
Now accepting commands (quit to exit program):
> size
0
> capacity
16
elements(0):
> push 1
> push 2
> push 3
> push 4
elements(4): 1 2 3 4
> size
4
> insert 0 0
elements(5): 0 1 2 3 4
> push 5
> push 6
> push 7
> push 8
> push 9
elements(10): 0 1 2 3 4 5 6 7 8 9
> size
10
> capacity
16
> push 0
> push 1
> push 2
> push 3
> push 4
> size
15
> capacity
16
> push 5
elements(16): 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
> size
16
> capacity
16
> push 6
elements(17): 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
> size
17
> capacity
32
> pop
> pop
elements(15): 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
> size
15
> capacity
32
> quit
CMPSC 122.3 February 20, 2020 Assignment 2 Due: Friday, Feb 28, 2020 End of Day - 11:59 pm Overview A vector is a container that can be treated like a normal array (create without using the new keyword) but also allows for dynamic resizing if the vector runs out of capacity to store new data. How is this possible? A vector is just an abstraction that performs dynamic memory allocation behind the scenes, but is presented in a way that is familiar to using an array, as well as providing additional functionality. The template file basic_integer_vector.cpp is provided for you, and it is a complete implementation of a very simple integer vector. The interactive_mode function allows the user to type in commands and values that will modify a vector object when properly input. Compile the program and test some of the commands. On Canvas, the file integer_vector_sample_program_run.ext shows a series of commands that provide a thorough illustration how the vector class works. (50 Points) Generic Vector Take the code from the integer_basic_vector.cpp program, and modify it to create a program (called generic_vector.cpp) that does that following: 1. Uses templates in order to allow the BasicVector class to not be limited to just containing integers The template will allow you to substitute a placeholder label in the following parts of the code: o The data type of the data member field o The data type of the value parameter in the push_back and insert functions o The return type of the at, front, back, and I) functions o Logic in the resize and the constructors that dynamically allocates data o In the pop_back function, modify the line data[ --vector_size] = 0; into data[--vector_size] = T(); 2. Insert the get_datatype function provided in the get_datatype.cpp file into your code 3. Transform the interactive_mode function into a template function, such that you can explicitly invoke interactive_mode from main with the correct type. 4. Modify the main function to call get_datatype, and based on the returned int value, explicitly call interactive_mode with the correct template argument. CMPSC 122.3 February 20, 2020 Assignment 2 Due: Friday, Feb 28, 2020 End of Day - 11:59 pm Overview A vector is a container that can be treated like a normal array (create without using the new keyword) but also allows for dynamic resizing if the vector runs out of capacity to store new data. How is this possible? A vector is just an abstraction that performs dynamic memory allocation behind the scenes, but is presented in a way that is familiar to using an array, as well as providing additional functionality. The template file basic_integer_vector.cpp is provided for you, and it is a complete implementation of a very simple integer vector. The interactive_mode function allows the user to type in commands and values that will modify a vector object when properly input. Compile the program and test some of the commands. On Canvas, the file integer_vector_sample_program_run.ext shows a series of commands that provide a thorough illustration how the vector class works. (50 Points) Generic Vector Take the code from the integer_basic_vector.cpp program, and modify it to create a program (called generic_vector.cpp) that does that following: 1. Uses templates in order to allow the BasicVector class to not be limited to just containing integers The template will allow you to substitute a placeholder label in the following parts of the code: o The data type of the data member field o The data type of the value parameter in the push_back and insert functions o The return type of the at, front, back, and I) functions o Logic in the resize and the constructors that dynamically allocates data o In the pop_back function, modify the line data[ --vector_size] = 0; into data[--vector_size] = T(); 2. Insert the get_datatype function provided in the get_datatype.cpp file into your code 3. Transform the interactive_mode function into a template function, such that you can explicitly invoke interactive_mode from main with the correct type. 4. Modify the main function to call get_datatype, and based on the returned int value, explicitly call interactive_mode with the correct template argumentStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started