C++ Assignment - Only Implementation file( VectorDouble.cpp file) required. The header file is already given. Please help, thumbs up guaranteed.
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 VectorDoublethat is like a class for a vector with base type double. Your class VectorDoublewill 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_countis 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 VectorDoubleand 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:
Some other useful notes:
#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H
#include <iostream>
using namespace std;
class VectorDouble {
private:
int max_count;
int count;
double* arr;
public:
//default constructor
VectorDouble();
//parameterized constructor takes an int argument for an integer
size of the array
VectorDouble(int max_count);
//copy constructor
VectorDouble(const VectorDouble& copy);
//Destructor
~VectorDouble();
//operations of vector class
//overloaded operator = for VectorDouble class
VectorDouble operator =(VectorDouble& copy);
// overloaded operator == for VectorDouble class
bool operator ==(VectorDouble b) const;
//to insert an element into a double vector
void push_back(double num);
int capacity();
int size();
void reserve(unsigned int size);
void resize(unsigned size, double defaultVal = 0.0);
double value_at(unsigned int i);
void change_value_at(double newValue, unsigned int i);
friend ostream& operator<<(ostream& os, const
VectorDouble &vd);
// DO NOT CHANGE THE FOLLOWING LINE OF CODE. It is for the testing
framework
// DO NOT IMPLEMENT THE FOLLOWING FUNCTION. It is implemented by
the testing framework
friend int reserved_driver_main();
};
#endif
Screenshot
------------------------------
Program
VectorDouble.h
#ifndef DYNAMICARRAY_H
#define DYNAMICARRAY_H
#include <iostream>
using namespace std;
class VectorDouble {
private:
int max_count;
int count;
double* arr;
public:
//default constructor
VectorDouble();
//parameterized constructor takes an int argument for
an integer size of the array
VectorDouble(int max_count);
//copy constructor
VectorDouble(const VectorDouble& copy);
//Destructor
~VectorDouble();
//operations of vector class
//overloaded operator = for VectorDouble class
VectorDouble operator =(VectorDouble& copy);
// overloaded operator == for VectorDouble class
bool operator ==(VectorDouble b) const;
//to insert an element into a double vector
void push_back(double num);
int capacity();
int size();
void reserve(unsigned int size);
void resize(unsigned size, double defaultVal =
0.0);
double value_at(unsigned int i);
void change_value_at(double newValue, unsigned int
i);
friend ostream& operator<<(ostream& os,
const VectorDouble &vd);
// DO NOT CHANGE THE FOLLOWING LINE OF CODE. It is for
the testing framework
// DO NOT IMPLEMENT THE FOLLOWING FUNCTION. It is
implemented by the testing framework
friend int reserved_driver_main();
};
#endif
VecorDouble.cpp
#include "VectorDouble.h"
//default constructor
VectorDouble::VectorDouble() {
arr = new double[50];
count = 0;
max_count = 50;
}
//parameterized constructor takes an int argument for an integer
size of the array
VectorDouble::VectorDouble(int max_count) {
arr = new double[max_count];
count = 0;
max_count = 50;
}
//copy constructor
VectorDouble::VectorDouble(const VectorDouble& copy) {
count = copy.count;
max_count = copy.max_count;
// Allocate new memory for array.
arr = new double[max_count];
// Copy data across.
for (int i = 0; i != count; ++i) {
arr[i] = copy.arr[i];
}
}
//Destructor
VectorDouble::~VectorDouble() {
delete[] arr;
}
//operations of vector class
//overloaded operator = for VectorDouble class
VectorDouble VectorDouble::operator =(VectorDouble& copy)
{
// Check for self-assignment.
if (© != this) {
// Free lhs array memory.
delete[] arr;
// Allocate new memory for lhs
array.
count= copy.count;
max_count =copy.max_count;
arr = new double[max_count];
// Copy data across.
for (int i = 0; i != count; ++i)
{
arr[i] =
copy.arr[i];
}
}
return *this;
}
// overloaded operator == for VectorDouble class
bool VectorDouble::operator ==(VectorDouble b) const {
if (b.count == count) {
for (int i = 0; i<count; ++i)
{
if (arr[i]
!=b.arr[i]) {
return false;
}
}
return true;
}
else
{
{
return
false;
}
}
}
//to insert an element into a double vector
void VectorDouble::push_back(double num) {
if (max_count == 0) {
max_count = 1;
arr = new double[max_count];
arr[count] = num;
count++;
}
else if (max_count > count) {
arr[count] = num;
count++;
}
else {
double *tempArray = new
double[max_count];
tempArray = arr;
resize(2*max_count,max_count);
arr= tempArray;
arr[count] = num;
count++;
}
}
int VectorDouble::capacity() {
return max_count;
}
int VectorDouble::size() {
return count;
}
void VectorDouble::reserve(unsigned int size) {
}
void VectorDouble::resize(unsigned size, double defaultVal) {
max_count =size;
arr= new double[defaultVal];
}
double VectorDouble::value_at(unsigned int i) {
if (i < count) {
return arr[i];
}
return -1;
}
void VectorDouble::change_value_at(double newValue, unsigned int i)
{
if (i < count) {
arr[i] = newValue;;
}
cout <<i<< " out of bound!!!!" <<
endl;
}
ostream& operator<<(ostream& os, const VectorDouble
&vd) {
os << "[ ";
for (int i = 0; i != vd.count; ++i) {
os << vd.arr[i] << "
";
}
os << "]" << endl;
return os;
}
Simpletest.cpp
#include <iostream>
#include "VectorDouble.h"
using namespace std;
int main()
{
//Create a vector object
VectorDouble v;
//Display size
cout << "array_current_size = " <<
v.size();
cout<< ", " << "array_limit = "
<< v.capacity()<< endl;
//Add values into vector
v.push_back(10.1);
v.push_back(1.1);
//Display size
cout << "array_current_size = " <<
v.size();
cout << ", " << "array_limit = " <<
v.capacity() << endl;
//Display vector
cout << v;
//Add values into vector
v.push_back(5.1);
v.push_back(6.1);
//Display size
cout << "array_current_size = " <<
v.size();
cout << ", " << "array_limit = " <<
v.capacity() << endl;
//Display vector
cout << v;
}
-------------------------------------------
Output
array_current_size = 0, array_limit = 50
array_current_size = 2, array_limit = 50
[ 10.1 1.1 ]
array_current_size = 4, array_limit = 50
[ 10.1 1.1 5.1 6.1 ]
C++ Assignment - Only Implementation file( VectorDouble.cpp file) required. The header file is already given. Please help, thumbs up guaranteed. Chapter 8 discussed vectors, which are like arrays that...
Please do it carefully Using the header file ( MyArray.h ) Type the implementation file MyArray.cpp, and a test file to test the functionality of the class. Hint: read all the explanations in the header with attention. MyArray.h : #ifndef MYARRAY_H #define MYARRAY_H #include <iostream> using namespace std; class MyArray { friend ostream& operator<<( ostream & output, const MyArray & rhs); // to output the values of the array friend istream& operator>>( istream & input, MyArray & rhs); // to...
C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...
Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 files header file , one .cpp file for function and one more main cpp file) Template Classes – Chapter 12 Write an example of a template function that can swap 2 generic elements. Create a C++ class vector using the class diagram shown in Figure 12.2. in Liangs textbook. Do not use the C++ provided header file <vector>...
8.9 Coding lab #5: create a dynamic array ADT and a singly linked list ADT. Honor Code Your answers to this homework must be your own work.You are not allowed to share your solutions.You may not engage in any other activities that will dishonestly improve your results or dishonestly improve or damage the results of others. Plagiarism Plagiarism is when you copy words, ideas, or any other materials from another source without giving credit. Plagiarism is unacceptable in any academic environment....
I only need the "functions" NOT the header file nor the main implementation file JUST the implementations for the functions Please help, if its difficult to do the complete program I would appreciate if you could do as much functions as you can especially for the derived class. I am a beginer so I am only using classes and pointers while implementing everything using simple c++ commands thank you in advanced Design and implement two C++ classes to provide matrix...
C++ 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 project 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: A private member variable called dynamicArray that references a...
How do i Overload the & operator to concatenate two arrays?, so elements from both arrays will be seen Below is code i have so far. everything works except the operator overload of &. The & operator only works when both array are equal, first array smaller then second, fails when first array is the largest. class smartArray{ public: int *elements; // dynamic array, memory is allocated in the constructor. Use *this to access size member. int length(); // returns...
Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a copy constructor, (6) overload the assignment operator, (7) overload the insertion...
In C++, develop a class that supports array rotation. Rotating an array is an operation where you shift all elements of the array some number of positions left or right, and elements that are shifted off of the left or right end of the array "wrap around" to the right or left end, respectively. For example, if we rotate the array [1, 2, 3, 4, 5] to the right by 1, we get the array [5, 1, 2, 3, 4]....
Write a code in C++ by considering the following conditions :- Tasks :- 1. Create a class Employee (with member variables: char * name, int id, and double age). 2. Create a constructor that should be able to take arguments and initialize the member variables. 3. Create a copy constructor for employee class to ensure deep copy. 4. Create a destructor and de allocate the dynamic memory. 5. Overload the assignment operator to prevent shallow copy and ensure deep copy....