Question

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...

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:

  • Three constructors:
    1. A default constructor that creates a dynamic array for 50 elements.
    2. A constructor with one int argument for the number of elements in the initial dynamic array.
    3. 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 resizethat behave the same as the member functions of the same names for vectors.
    • See documentation for the vector class here: http://www.cplusplus.com/reference/vector/vector/ (Links to an external site.)Links to an external site.
      • For push_back(), you should increase the capacity of the vector by 2 if there is not room to push an additional element (i.e.  countequals max_count).
      • For reserve()and resize(), make sure to copy the current contents of the VectorDoubleinto the new underlying array.
  • Two member functions to give your class the same utility as the square brackets:
    1. value_at(i), which returns the value of the i-th element in the dynamic array; and
    2. change_value_at(d, i), which changes the double value at the i-th element of the dynamic array to d.
    • Enforce suitable restrictions on the arguments to value_atand change_value_at.
      • Make sure indices are in bounds of the array. If they are out of bounds, throw a std::logic_error
    • (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.)
  • You may not use the Standard’s

Some other useful notes:

  • A skeleton of the class has been provided to help you get started. Do not change the return types, argument types, or number of arguments to each function.
    • Do not change what is already in VectorDouble.h. You may add functions, but do not change anything that is already there.
  • You may add any additional helpful functions as you see fit
  • I have provided a working <<operator for the VectorDoubleclass. The testing framework uses this operator extensively - do not change its behavior in any way.
  • Get basic working versions of the constructors, copy constructor, and assignment operator working first.
    • If you don’t, the testing code will crash when it hits points that expect these to be implemented. If these functions don’t succeed in some basic manner, it’s highly likely your program will crash. This is true for any class, not just VectorDouble.
  • You’ll notice a friend int reserved_driver_main()is in the VectorDouble header. This is how the testing framework accesses the private members of the VectorDouble(since the function is a friend!). This was done so that the tests don’t rely on functions like capacity(), size(), value_at(), and change_value_at(). If the framework did rely on these, and you hadn’t implemented them correctly, you would be guaranteed to fail everytest case. This allows more partial credit. However, if you copy and paste the testing code into your user_main(), your program won’t compile (the intention of the providing the testing code was not to copy and paste it, but to show you what cases you need to handle).
    • Two ways around this:
      1. Make the private members public ( arr, max_count, count). Then there’s no need for the friend function, and the you can copy the testing code into your IDE/ user_main().
      2. After you’ve implemented capacity(), size(), value_at(), and change_value_at(), replace the uses of private members with these functions appropriately in the testing code.
  • Here is the Header file
  • #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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Screenshot

▽ 凹WectorGivulnCpp Microsoft Viswi Studi Fie Feit w Pmjert Build Dehug Team Toele Tet Anaye Windew Help Ouck Launch [Ctrl+Q)

------------------------------

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 (&copy != 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 ]

Add a comment
Know the answer?
Add Answer to:
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...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please do it carefully Using the header file ( MyArray.h ) Type the implementation file MyArray.cpp,...

    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...

    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...

    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...

    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 impleme...

    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...

    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...

    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...

    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...

    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...

    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....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT