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

I need some help with my C++ code. I\'d appreciate it if someone could help! The

ID: 663640 • Letter: I

Question

I need some help with my C++ code. I'd appreciate it if someone could help! The part of my code i'm having trouble with is the "insert()" function. I need to be able to insert a value before a specified location. I already wrote about 90% of the code, I would love it if someone could go through it and fix my mistakes and help me complete the code!

This is the .cpp file of the code: (If you need any information regarding the assignment, please let me know, I will tell you ASAP)

#include "pm1vector2.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, int initial_value) { /I NEED HELP HERE... how do I implement the initial_value?
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_;
}

bool pm1::Vector::empty(){
   if(size_ != 0)
       return false;
   return true;
}
  
unsigned int pm1::Vector::size(){
   return size_;
}
  
void pm1::Vector::clear(){ //I NEED HELP HERE. It says this function is incorrect, I dont understand whats wrong here.
   ~Vector();
   size_ = 0;
   mem_size_ = 0;
}

void pm1::Vector::erase(pm1::Vector::iterator location){
   if(begin()>location or end()<=location){      
       throw std::out_of_range ("Location out of range");
  
   for (iterator n=location+1; n!=end(); ++n){
       std::swap(*n,*(n-1));
   }
   --size;
}
  
void pm1::Vector::push_back(int value){
   insert(end(),value)
}
   
void pm1::Vector::pop_back(){
   if(empty()){
       throw std::out_of_range("Location out of range");
   }
   else{
       erase(end()-1);
   }
}
  
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 //I NEED HELP HERE.
//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;
   }
//I need to finish this function but dont know how...
//I 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
}

Explanation / Answer

#include "pm1vector.h"

#include <stdexcept>

#include<iostream>

// The null constructor

pm1::Vector :: Vector () {

//Assume that the user will want to hold at least a 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;

}

}

pm1 :: vector :: Vector(unsigned int initial_size,int initial_value) {

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;

for(int i = 0; i < initial_size;i++) {

data_[i] = initial_value;

}

}

}

//Destrutor

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

}

//Function for checking if the vector is empty

bool pm1::vector::empty() {

bool isEmpty = false;

if(size_ = 0) {

isEmpty = true;

return isEmpty;

}

else

return isEmpty;

}

//Returning the size of the vector

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

return size_;

}

//Clearing the vector

void pm1::Vector::clear() {

delete[] data_;

data_ = new int[10];

increaseSize();

size_ = 0;

}

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

}

size_ = size_ + 1;

for(int i = size_;

}

void pm1::Vector::erase(pm1::Vector::iterator location) {

if (begin() > location or end() < location) {

//Stops the function and throws an exception

throw std::out_of_range("erase location is out of range");

}

}

void pm1::vector::pop_back() {

if (size_ == 0)

//Stops the function and throws an exception

throw std::out_of_range("The container is empty");

size_ = 1;

}

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

//This operator is given as an example to do 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;

}