1 answer

C++ Assignment - Only Implementation file( VectorDouble.cpp file) required. The header file is already given. Please help, thumbs up guaranteed.

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


Answers

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 ]

.

Similar Solved Questions

1 answer
2-2 Chemoautotrophic bacteria are responsible for the oxidation of NH:-N to NO -N in many biologi...
please help 2-2 Chemoautotrophic bacteria are responsible for the oxidation of NH:-N to NO -N in many biological wastewater treatment processes. These bacteria, collectively called nitrifiers, consist of the genera Nitrosomonar and Ntro- bacter. The oxidation of ammonia to nitrate is considered ...
1 answer
Please show all steps. Thanks
Please show all steps. Thanks...
1 answer
How did the Spanish-American War lead to the Filipinos rebelling against the United States?
How did the Spanish-American War lead to the Filipinos rebelling against the United States?...
1 answer
The Royal Fruit Company produces two types of fruit drinks. The first type is 70% pure fruit juice, and the second type is 95% pure fruit juice. How many pints of each drink must be used to make 50 pints of a mixture that is 90% pure fruit juice?
The Royal Fruit Company produces two types of fruit drinks. The first type is 70% pure fruit juice, and the second type is 95% pure fruit juice. How many pints of each drink must be used to make 50 pints of a mixture that is 90% pure fruit juice?...
1 answer
Need statement of owners equity and if you have time, balance sheet. Following is a trial...
need statement of owners equity and if you have time, balance sheet. Following is a trial balance for Songahm Taekwondo, a sole proprietorship as of July 31, 2019, the end of its first month of business. To do: Based on the trial balance, prepare an income statement, a statement of owners equity an...
1 answer
Navaroli Company began operations in January 5, 2018. Cost and sales information for its first two...
Navaroli Company began operations in January 5, 2018. Cost and sales information for its first two calendar years of operations are summarized below: Manufacturing costs                      ...
1 answer
This is a question with four parts, so all four parts should be answered 1. Consider...
This is a question with four parts, so all four parts should be answered 1. Consider the following spin wave functions for a system of two identical Are they symmetric or antisymmetric wavefunction? If not, construct a suitable symmetric wavefunction combining vi and v?2. Don't forget to 2. Sho...
1 answer
Nucor Corporation produces steel and steel products at its eight mills and is a major recycler...
Nucor Corporation produces steel and steel products at its eight mills and is a major recycler of scrap metal. The following data relate to Nucor for four years. In 2017, Nucor's net Income was higher by $175.2 million because of a one-time effect of the Tax Cuts and Jobs Act. Nucor Corporation ...
1 answer
< HW#16: Waves &amp;amp;amp; Sound Problem 12.80 Two trains emit 441 Hz
< HW#16: Waves &amp;amp;amp; Sound Problem 12.80 Two trains emit 441 Hz...
1 answer
0% Нас он CH3 CH3 H20 Br however, does not result from the addition of HO-Br, for example. Instea...
0% Нас он CH3 CH3 H20 Br however, does not result from the addition of HO-Br, for example. Instead the addition is done indirectly by reaction of the alkene with Br2 in the presence of water. The reaction also works with Cl2 to give chlorohydrins instead of bromohydrins...
1 answer
What element do all organic compounds contain?
What element do all organic compounds contain?...
1 answer
I need only i, j,and k please in clear writing 1. (15 Marks) The following are...
I need only i, j,and k please in clear writing 1. (15 Marks) The following are body mass index (BMI) scores measured in 1l patients who are free of diabetes and participating in a study of risk factors for obesity Body mass index is measured as the ratio of weight in kilograms to bright in meters s...
1 answer
How does the middle class of Jackson's America compare to today's middle class? How would your...
How does the middle class of Jackson's America compare to today's middle class? How would your profession of choice be different then instead of now?...
1 answer
5. Use the following information to complete the questions: State of Economy Probability that State will...
5. Use the following information to complete the questions: State of Economy Probability that State will Occur Return on Stock A Return on Stock B Return on Stock Recession 0.4 10% 15% 20% Boom 0.6 8% 4% 0% 8.8% 8.4% 8% Expected Return 0.20 0.50 0.30 Portfolio Weight 5a. Compute the expected return ...
1 answer
Implement the following expression in assembly language: for (i= 5; i > 0; i--) { for(j...
Implement the following expression in assembly language: for (i= 5; i > 0; i--) { for(j = 3; j > 0; j--) { y = i + j ;} } -Assume I andj are 32-bit registersand y is a list....
1 answer
Evaluate whether the following statements are true, false, or it is unclear from the provided information....
Evaluate whether the following statements are true, false, or it is unclear from the provided information. Then offer a explanation of no more than two complete sentences for why you chose your answer. Your score will depend heavily on the explanation. (a) McDonalds and Burger King have locations ar...
1 answer
P6-26 (similar to) Question Help Bond valuation Quarterly interest Calculate the value of a $500-par-value bond...
P6-26 (similar to) Question Help Bond valuation Quarterly interest Calculate the value of a $500-par-value bond paying quarterly interest at an annual coupon interest rate of 14% and having 14 years until maturity if the required return on similar-risk bonds is currently a 11% annual rate paid quart...
1 answer
Find the final amount in the following retirement​ account, in which the rate of return on...
Find the final amount in the following retirement​ account, in which the rate of return on the account and the regular contribution change over time. ​$548 per month invested at 4​%, compounded​ monthly, for 5 ​years; then ​$717 per month invested at 6​%, co...