Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this homework you will continue the pm1::Vector class that we started in lec

ID: 643620 • Letter: F

Question

For this homework you will continue the pm1::Vector class that we started in
lecture 10. It may look like a lot of work, but most of these functions should
onl be a line or two of code and quite a few simply call one of the other
functions. You will add the following functions to the class:

//Constructor with an initial size and value (5%)

Vector(unsigned int initial_size, int initial_value);

//Functions to check the number of elements (5% each for 10%)

//Returns true if the container is empty, false otherwise
bool pm1::Vector::empty();

//Returns the number of elements in the container

unsigned int pm1::Vector::size();

//Functions to modify the the container (10% for each for 50%)

//Remove all elements from the container
void pm1::Vector::clear();

//Insert value before the specified location

//If the value is out of range, throw a std::out_of_range exception
//Note that the end() location is valid; this is the same as push_back
void pm1::Vector::insert(pm1::Vector::iterator location, int value);

//Remove the value at the specified location

//If the value is out of range, throw a std::out_of_range exception
void pm1::Vector::erase(pm1::Vector::iterator location);

//Insert the given value at the end of the container

void pm1::Vector::push_back(int value);

//Remove the value at the end of the container

//If the container is empty, throw a std::out_of_range exception
void pm1::Vector::pop_back();

HINT! At times you will need to make the array bigger. To do that, this

private member function is provided to you:

void pm1::Vector::increaseSize() {

//Make the new, larger array
int* bigger = new int[mem_size_*2];
//Copy over the existing data
for (int i = 0; i < size_; ++i) {
bigger[i] = data_[i];
}
//The available memory size is now doubled
mem_size_ *= 2;
//Delete the old array
delete[] data_;
//Set the pointer to the new array
data_ = bigger;

}



You do not need to bother making the array smaller, even if all of the data is
removed with the clear() function. The insert member function is the most
tricky, so you have been given half of the code for it.

The equality operator is given to you, and you will implement the rest:

//Comparison operators. These will compare elements of the vector one by one.

bool operator==(pm1::Vector& a, pm1::Vector& b) {
//They are not equal if the size is different
//or if any element does not match
if (a.size() != b.size()) {
return false;
}
else {
for (int i = 0; i < a.size(); ++i) {
if (a[i] != b[i]) {
return false;
}
}
}
//Otherwise the two vectors are equal
return true;
}

//Comparison operators. These will compare elements of the vector one by one.
//(5% for each working operator)

bool operator!=(pm1::Vector& a, pm1::Vector& b);
bool operator<(pm1::Vector& a, pm1::Vector& b);
bool operator<=(pm1::Vector& a, pm1::Vector& b);
bool operator>(pm1::Vector& a, pm1::Vector& b);
bool operator>=(pm1::Vector& a, pm1::Vector& b);

Thats the question and the two files referenced are

#include "pm1vector.h"
#include

//The null constructor
pm1::Vector::Vector() {
//Assume that the user will want to hold at least a few values
//This saves us from constantly allocating new memory
data_ = new int[10];
mem_size_ = 10;
//Nothing stored yet
size_ = 0;
}

pm1::Vector::Vector(unsigned int initial_size) {
if (0 == initial_size) {
size_ = 0;
mem_size_ = 0;
//Mark the memory as invalid
data_ = nullptr;
}
else {
data_ = new int[initial_size];
mem_size_ = initial_size;
size_ = initial_size;
}
}

//Destructor
pm1::Vector::~Vector() {
//Delete the data if there is any allocated
if (0 != mem_size_) {
delete[] data_;
}
}

int& pm1::Vector::operator[](unsigned int offset) {
//Notice that c++ knows we want to return a reference
//No special syntax is required
return data_[offset];
}

int& pm1::Vector::at(unsigned int offset) {
if (offset >= size_) {
//Stops the function and throws an exception
throw std::out_of_range("vector index out of range");
}
return data_[offset];
}

pm1::Vector::iterator pm1::Vector::begin() {
//The first position in the vector
return data_;
}
pm1::Vector::iterator pm1::Vector::end() {
//One beyond the last valid position
return data_ + size_;
}

void pm1::Vector::increaseSize() {
   //Make the new, larger array
   //Handle the case where the array started empty (nothing to copy or delete)
   if (0 == mem_size_) {
       mem_size_ = 10;
       data_ = new int[mem_size_];
   }
   else {
       //Otherwise the available memory size is now doubled
       mem_size_ *= 2;
       int* bigger = new int[mem_size_];
       //Copy over the existing data
       for (int i = 0; i < size_; ++i) {
           bigger[i] = data_[i];
       }
       //Delete the old array
       delete[] data_;
       //Set the pointer to the new array
       data_ = bigger;
   }
}

//Insert value before the specified location
//If the value is out of range, throw a std::out_of_range exception
//Note that the end() location is valid; this is the same as push_back
void pm1::Vector::insert(pm1::Vector::iterator location, int value) {
if (begin() > location or end() < location) {
//Stops the function and throws an exception
throw std::out_of_range("insert location is out of range");
}
//First see if there is enough space
   if (size_+1 > mem_size_) {
       //If we increase the size of the vector then the previous
       //location pointer isn't value since we deleted the older, smaller array
       //Remember its offset and find the new iterator location after
       //increasing the array size
       int offset = std::distance(begin(), location);
       increaseSize();
       location = data_ + offset;
   }
//TODO You must finish this function
//You'll need to increase the size and shift all of the elements
//from location to end() over by one position, and copy value
//into the given location
}

//Comparison operators. These will compare elements of the vector one by one.
//This operator is given as an example to do the rest
bool operator==(pm1::Vector& a, pm1::Vector& b) {
   //They are not equal if the size is different
   //or if any element does not match
   if (a.size() != b.size()) {
       return false;
   }
   else {
       for (int i = 0; i < a.size(); ++i) {
           if (a[i] != b[i]) {
               return false;
           }
       }
   }
   //Otherwise the two vectors are equal
   return true;
}

and

//Use a name (PM1VECTOR_H_) to check if this file was
//already included. If the name isn't defined, then
//define it and create the class
#ifndef PM1VECTOR_H_
#define PM1VECTOR_H_

namespace pm1 {
//This vector will only hold ints for now
class Vector {
public:
//Name int* iterator so that the name Vector::iterator exists
typedef int* iterator;
//Null constructor to create an empty vector
Vector();
//Constructor with an initial size
Vector(unsigned int initial_size);
//Destructor that frees any allocated memory
~Vector();
//Array operator for element access
int& operator[](unsigned int offset);
//At function for element access
int& at(unsigned int offset);
//Fetch the first iterator
iterator begin();
//Fetch the last iterator
iterator end();
private:
//We'll put underscores after private variable names to help us
//tell them apart from other variables

//A pointer to the actual data
int* data_;
//How large is the array?
unsigned int mem_size_;
//How many element are currently being held?
unsigned int size_;

//Doubles the allocated space, copies old array to the new one, and
//deletes the old array
void increaseSize();
};
}

//End the preprocessor guard from before
#endif

Explanation / Answer

The methods to be completed are written below (parts of the code like function declaration etc are maintained same as given in the question which is not plagiarized):-

Vector(unsigned int initial_size,int initial_value){
   initial_size=0;
   initial_value=0;
}

bool pm1::Vector::empty(){
   if (size_==0)
       return true;
   else
           return false;
}

unsigned int pm1::Vector::size(){
   return size_;
}

void pm1::Vector::clear()
{
    delete[] data_;
    size_= 0;
    mem_size_=0;
}

void pm1::Vector::insert(pm1::Vector::iterator location,int value) {
    if(begin()>location or end()<location) {
        throw std::out_of_range("Insert location is out of range");
    }
    if(size_+1>mem_size_){
        int offset=std::distance(begin(),location);
        increaseSize();
        location=data_+offset;
    }
    if(size_==0){
        data_[0]=value;
        size_++;
        return;
    }
    if(location==end())
    {
        push_back(value);
        return;
    }
    for(auto cur = end()-1;cur!= location-1;--cur)
    {
        shift_right(cur);
        if(cur==location)
        {
            *location=value;
        }
    }
    size_++;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote