Question

In: Computer Science

#3 Working with char vector using Array The class FixedVector has been declared and was implemented....

#3

Working with char vector using Array

The class FixedVector has been declared and was implemented. The code is available here. You want to use additional operations within the class, so the function member insert needs to be changed, as well as two new functions members, find and operator == need to be added to the class definition and implemented. Consider the new class definition below, and the new/modified functions being in bold.

// Augmented Declaration
template <typename T>
class FixedVector {
private:
   size_t size_; // number of elements in the data structure
   const size_t capacity_; // length of the array
   T* array_; // pointer to dynamically allocated array

public:
   // Constructors
   FixedVector(size_t arraysize = 0);         // Also serves as default constructor
   FixedVector(const FixedVector& input );    // Copy constructor
   ~FixedVector();

   // Getters / Setters
   T& at(size_t index);
   T& operator[](size_t index);
   void push_back(const T& value);
   void set(size_t index, const T& value);
   void erase(size_t index);

   size_t find(const T& value);
   size_t insert(size_t beforeIndex, const T& value);
   size_t size();
   bool empty();
   void clear();


   // Overloaded Operators
   FixedVector& operator= (const FixedVector& rhs); //Copy assignment

   bool   operator== (const FixedVector& rhs)
};

// Function member to look for value in FixedVector

// If value is in the FixedVector, then return the index of FixedVector that contains
// the value. If size_ is 0 (array is empty) or the value is not in FixedVector, then

// return size_

template <typename T>

size_t FixedVector<T>::find(const T& value) {

// to be completed

}

// Function member to insert value in the FixedVector at index beforeIndex and return
// beforeIndex

// If beforeIndex is between 0 and size_, then insert the value by pushing all the
// elements to the right of beforeIndex one position to the right, and increment size_

// If size would exceed capacity, then exit with an error

// If beforeIndex is >=size_ then display error and do not do any changes to FixedVector

template <typename T>

size_t FixedVector<T>::insert(size_t beforeIndex, const T& value) {

// to be completed

}

// Function member to test the equality between two FixedVectors

// It returns true if the two FixedVectors are exactly the same, false otherwise

template <typename T>

bool FixedArray<T>::operator== (const FixedVector& rhs){
// to be completed

}


int main() {

// testing the new implementation of a FixedVector

// declare & initialize a FixedVector of int with 10 elements

FixedVector<int> Array1(5);

// place 1,5,10 in the array

cout << “FixedArray gets the elements 1, 5, 10” << endl;

Array1.push_back(1);

Array1.push_back(5);

Array1.push_back(10);

// Try the find operation

cout << “Value 5 is at index “ << Array1.find(5) << endl;

// Try the insert operation

cout << “Value 2 is inserted at index” << Array1.insert(1, 2) << endl;

// Try the == operator

FixedVector<int> Array2(5);

Array2.push_back(1);

Array2.push_back(5);

Array2.push_back(10);

if (Array1 == Array2)

   cout << “The two arrays are the same.” << endl;

else

   cout << “The two arrays are different.” << endl;

return 0;

}

I just need the to be completed part

Solutions

Expert Solution

// Augmented Declaration

template <typename T>

class FixedVector {

private:

size_t size_; // number of elements in the data structure

const size_t capacity_; // length of the array

T* array_; // pointer to dynamically allocated array

public:

// Constructors

FixedVector(size_t arraysize = 0); // Also serves as default constructor

FixedVector(const FixedVector& input ); // Copy constructor

~FixedVector();

// Getters / Setters

T& at(size_t index);

T& operator[](size_t index);

void push_back(const T& value);

void set(size_t index, const T& value);

void erase(size_t index);

size_t find(const T& value);

size_t insert(size_t beforeIndex, const T& value);

size_t size();

bool empty();

void clear();

// Overloaded Operators

FixedVector& operator=(const FixedVector& rhs); //Copy assignment

bool operator== (const FixedVector& rhs);

};

// Function member to look for value in FixedVector

// If value is in the FixedVector, then return the index of FixedVector that contains

// the value. If size_ is 0 (array is empty) or the value is not in FixedVector, then

// return size_

template <typename T>

size_t FixedVector<T>::find(const T& value) {

               for(size_t i=0;i<size_;i++)

               {

                              if(value == array_[i])

                                             return i;

               }

               return size_;

}

// Function member to insert value in the FixedVector at index beforeIndex and return

// beforeIndex

// If beforeIndex is between 0 and size_, then insert the value by pushing all the

// elements to the right of beforeIndex one position to the right, and increment size_

// If size would exceed capacity, then exit with an error

// If beforeIndex is >=size_ then display error and do not do any changes to FixedVector

template <typename T>

size_t FixedVector<T>::insert(size_t beforeIndex, const T &value) {

if( size_ >= capacity_) {

throw std::range_error("insufficient capacity to add another element");

}

if( beforeIndex > size_ ) {

               throw std::range_error("Index out of range index");

}

// move elements to create space starting from the right and working left

for( size_t j = size_; j > beforeIndex; j-- ) {

array_[ j ] = array_[ j-1 ]; // shift elements to the right

}

array_[ beforeIndex ] = value; // put in empty slot

size_++;

return beforeIndex;

}

// Function member to test the equality between two FixedVectors

// It returns true if the two FixedVectors are exactly the same, false otherwise

template <typename T>

bool FixedVector<T>::operator== (const FixedVector& rhs){

               if(size_ == rhs.size_)

               {

                              for(size_t i=0;i<size_;i++)

                              {

                                             if(array_[i] != rhs.array_[i])

                                                            return false;

                              }

               }else

                              return false;

               return true;

}

int main() {

               // testing the new implementation of a FixedVector

               // declare & initialize a FixedVector of int with 10 elements

               FixedVector<int> Array1(5);

               // place 1,5,10 in the array

               cout << "FixedArray gets the elements 1, 5, 10" << endl;

               Array1.push_back(1);

               Array1.push_back(5);

               Array1.push_back(10);

               // Try the find operation

               cout << "Value 5 is at index " << Array1.find(5) << endl;

               // Try the insert operation

               cout << "Value 2 is inserted at index" << Array1.insert(1, 2) << endl;

               // Try the == operator

               FixedVector<int> Array2(5);

               Array2.push_back(1);

               Array2.push_back(5);

               Array2.push_back(10);

               if (Array1 == Array2)

                  cout << "The two arrays are the same." << endl;

               else

                  cout << "The two arrays are different." << endl;

               return 0;

}


Related Solutions

How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input #...
How to write a C++ of CountingSort function using 2D vector? CountingSort(vector > array) Input # of rows: 2 Input Row 1: 9 8 7 6 3 2 1 5 4 Input Row 2: 1 2 4 3 5 6 9 8 7 Output 1,2,3,4,5,6,7,8,9 1,2,3,4,5,6,7,8,9
Implement a Binary tree using an array using class.
Implement a Binary tree using an array using class.
The < and == operators for the class Record have already been implemented for you.
The < and == operators for the class Record have already been implemented for you. Write the code necessary to complete the >, <=,>= and != operators. (hint: you do not need to know anything about the Record class to complete)
Question: Write an implementation of the ADT sorted list that uses a resizable array (vector class...
Question: Write an implementation of the ADT sorted list that uses a resizable array (vector class of C++ STL) to represent the list items. Anytime the list becomes full, double the size of the array.
Using the following array: //may be declared outside of the main function const int NUM_Games =4;...
Using the following array: //may be declared outside of the main function const int NUM_Games =4; //may only be declared within the main function int scores[NUM_GAMES] = {122, 76, 92, 143}; Write a C++ program to run a menu-driven program with the following choices: 1) Display the scores 2) Change a score 3) Display game with the highest score 4) Display a sorted list of the scores 5) Quit Write a function called getValidScore that allows a user to enter...
Assume you have a stack and a queue implemented using an array of size 4. show...
Assume you have a stack and a queue implemented using an array of size 4. show the content of the array for the stack (then the queue) for the following operations: (for the queue replace push by add and pop by remove; keep in mind that the queue uses a circular array): push(-3), push(-5), push(-9), push(-10), pop(), pop(), push(-13), pop(), push( -15), push(-17). java code
Jen has a PhD in economics and has been working for 3 years part-time as an...
Jen has a PhD in economics and has been working for 3 years part-time as an instructor; she has always hoped to be hired as a full-time faculty member. The best way to describe Jen is to say she is: Multiple Choice a discouraged worker. underemployed. overemployed. unemployed.
Learning Outcomes Using Java, maintain a collection of objects using an array. Construct a class that...
Learning Outcomes Using Java, maintain a collection of objects using an array. Construct a class that contains an array as a private instance variable. Construct methods with arrays as parameters and return values. Use partially filled arrays to implement a class where objects can be dynamically added. Implement searching and sorting algorithms. Instructions For this assignment you will be implementing an application that manages a music collection. The application will allow the user to add albums to the collection and...
           Homework: Polynomial Using Array Description: Implement a polynomial class (1) Name your class...
           Homework: Polynomial Using Array Description: Implement a polynomial class (1) Name your class Polynomial (2) Use array of doubles to store the coefficients so that the coefficient for x^k is stored in the location [k] of the array. (3) define the following methods: a. public Polynomial()    POSTCONDITION: Creates a polynomial represents 0 b. public Polynomial(double a0)    POSTCONDITION: Creates a polynomial has a single x^0 term with coefficient a0 c. public Polynomial(Polynomial p)    POSTCONDITION: Creates...
Implement the minimum priority queue UnsortedMPQ (using vector) that is a child class of the provided...
Implement the minimum priority queue UnsortedMPQ (using vector) that is a child class of the provided MPQ class. The functions from MPQ that are virtual function (remove min(), is empty(), min(), and insert()) must be implemented in the child classes. The functions remove min() and min() should throw an exception if the minimum priority queue is empty. For the UnsortedMPQ class, you will use a vector to implement the minimum priority queue functions. The insert() function should be O(1) and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT