Question
Hello this is c++ programming please help Chapter 8 discussed vectors, which are like arrays that can grow in size. Suppose that vectors were not
Hello this is c++ programming please help
Chapter 8 discussed vectors, which are like arrays that can grow in size. Suppose that vectors were not defined in C++. Define a class called VectorDouble that is like a class for a vector with base type double. Your class VectorDouble will have a private member variable for a dynamic array of doubles. It will also have two member variables of type int; one called max_count for the size of the dynamic array of doubles; and one called count for the number of array positions currently holding values. (max_count is the same as the capacity of a vector; count is the same as the size of a vector.)
If you attempt to add an element (a value of type double) to the vector object of the class VectorDouble and there is no more room, then a new dynamic array with twice the capacity of the old dynamic array is created and the values of the old dynamic array are copied to the new dynamic array.
Your class should have all of the following:
-Three constructors: a default constructor that creates a dynamic array for 50 elements, a constructor with one int argument for the number of elements in the initial dynamic array, and a copy constructor.
-A destructor.
-A suitable overloading of the assignment operator =.
-A suitable overloading of the equality operator ==. To be equal, the values of count and the count array elements must be equal, but the values of max_count need not be equal.
-Member functions push_back, capacity, size, reserve, and resize that behave the same as the member functions of the same names for vectors.
-Two member functions to give your class the same utility as the square brackets: value_at(i), which returns the value of the ith element in the dynamic array; and change_value_at(d, i), which changes the double value at the ith element of the dynamic array to d. Enforce suitable restrictions on the arguments to value_at and change_value_at. (Your class will not work with the square brackets. It can be made to work with square brackets, but we have not covered the material which tells you how to do that.)
----main.cpp---
// ** DO NOT MODIFY SECTION START #include #include "dyn_arr.h" #include using namespace std;
void personalTesting(); void dumpVector(VectorDouble v); void printVector(VectorDouble v, string vectorName);
int main() { personalTesting(); VectorDouble vec1; printVector(vec1, "vec1"); vec1.push_back(4.8); vec1.push_back(5.9); vec1.reserve(100); // capacity should be 100 printVector(vec1, "vec1"); dumpVector(vec1); VectorDouble vec2(3); vec2.push_back(0.1); vec2.push_back(0.2); vec2.push_back(0.3); vec2.push_back(0.4); // size should be 3*2 = 6 printVector(vec2, "vec2"); dumpVector(vec2); vec2.resize(10, 9.9); vec2.change_value_at(9.1, 2); vec2.push_back(0.0); printVector(vec2, "vec2"); dumpVector(vec2); vec2 = vec1; // vec2 should be same as vec1 printVector(vec2, "vec2(after ve2=vec1)"); dumpVector(vec2);
VectorDouble vec3(vec1); if (vec3 == vec1) { cout << " vec3 == vec1" << endl; // yes }else{ cout << "Equality operator incorrect."; } if (vec1 == vec2) { cout << "vec1 == vec2" << endl; // yes }else{ cout << "Equality operator incorrect."; } vec2.resize(3); printVector(vec2, "vec2(after ve2=vec1)"); dumpVector(vec2); vec3.resize(109); printVector(vec2, "vec2(after vec3.resize)"); dumpVector(vec3); try { vec3.resize(-2); } catch (logic_error err) { cout << err.what() << endl; } try { vec1.change_value_at(0, 10000); } catch (logic_error err) { cout << err.what() << endl; } try { VectorDouble(-8); } catch (logic_error err) { cout << err.what() << endl; } }
void dumpVector(VectorDouble v){ for (int i = 0; i < v.size(); i++) cout << v.value_at(i) << " "; cout << endl; }
void printVector(VectorDouble v, string vectorName){ cout << " [" + vectorName + "] size: " << v.size() << " capacity: " << v.capacity() << endl; } // ** DO NOT MODIFY SECTION END
void personalTesting(){ cout << "======= Personal Test - START =======" << endl; cout << "======= Personal Test - END =========" << endl; }
create "dyn_arr.h" and "dyn_arr.cpp"
thank you
Step 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