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

I need help with my C++ code! I posted earlier but realized it didn\'t have suff

ID: 663646 • Letter: I

Question

I need help with my C++ code! I posted earlier but realized it didn't have sufficient information to answer/understand my code.

This is a link to screenshots of my entire code:
http://imgur.com/2Vaahwb,jCc6cSq,N71QC5E,SA6NnzZ,nrrR3hu,ry9jw9k,lptZMLC,eb0zvi6,tRuH0bW

This includes my .h file, my .cpp file and the errors I'm getting while running the code.

I would rgeatly appreciate it if someone could fix my code. I've already completed about 90% of the code, just need help finishing the rest. I've marked down using comments on the code about where I need help by writing "//NEED HELP HERE".

Thank you!

Explanation / Answer

pm1vector.h:

#ifndef PM1VECTOR_H_

#define PM1VECTOR_H_

namespace pm1 {


class Vector {

public:

//Name int* iterator so that the name Vector::iterator exists

typedef int* iterator;

//Null constructor to create an empty vector

Vector();

Vector(unsigned int initial_size);

Vector(unsigned int initial_size, int initial_value);

//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();

void insert(iterator location, int value);

void erase(int* location);

void push_back(int value);

void pop_back();

void clear();

bool empty();

int size();

bool operator!=(Vector& b);

bool operator==(Vector& b);

bool operator<=(Vector& b);

bool operator>=(Vector& b);

bool operator>(Vector& b);

bool operator<(Vector& b);

private:

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


pm1vector.cpp:

#include "pm1vector.h"

#include <stdexcept>

#include <iostream>

using std::cout;

using std::endl;

//The null constructor

pm1::Vector::Vector() {

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)

{

// Vector(initial_size); // making an empty vector

if (0 == initial_size)

{

size_ = 0;

mem_size_ = 0;

data_ = nullptr;

}

else

{

data_ = new int[initial_size];

mem_size_ = initial_size;

size_ = initial_size;

}

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

{

data_[i] = initial_value;

}

}

//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("out of range in function at");

}

return data_[offset];

}

void overlap(pm1::Vector::iterator o, pm1::Vector::iterator n)

{

*o = *n;

}


void shift_right(pm1::Vector::iterator tail)

{

*(tail + 1) = *tail;

}

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;

}

}

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

{

if (size_+1 > mem_size_) {

increaseSize(); // just incase there is not more space in the array

}

*(end()) = value; // append value to the end

++size_; // increase the viewed size

}
void pm1::Vector::pop_back()

{

if (size_ == 0)

{

throw std::out_of_range("vector is empty");

}

size_--; // just decreasing the size

}
void pm1::Vector::clear()

{

// delete the existing data and bring mem_size and size to 0

delete[] data_;

size_ = 0;

mem_size_ = 0;

}

int pm1::Vector::size()

{

return size_;

}

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_) {

int offset = std::distance(begin(), location);

increaseSize();

location = data_ + offset;

}
// if the array is empty, just insert it into the first slot.

if (size_ == 0) {

data_[0] = value;

size_++;

return;

}

// end of vector

if (location == end())

{

push_back(value); // append to the end of the array

return;

}


for (auto cur = end()-1; cur != location-1; --cur)

{

shift_right(cur); // shift current value to the right

if (cur == location)

{

*location = value; // if the current location is the location

}

}

size_++;
}
// working on erase

void pm1::Vector::erase(int* location)

{

// if the location is just the last element of the array. just pop

// back

if (location == end() ) {

pop_back(); size_--; return;

}

int offset = 0; // when the location is reached

for (pm1::Vector::iterator l = begin(); l != end(); ++l)

{

// if the current location is the wanted location, then increment the offset.

if (l == location)

{

offset++;

}

overlap(l,l+offset);

}
// delete[] tmp;

size_--;

}

bool pm1::Vector::empty() {

if (size_ == 0) return true; // only return true if size_ is 0

return false;

}


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;

}

bool pm1::Vector::operator!=(pm1::Vector& other)

{

int i = 0;

if (other.size() != size()) return true;

for (auto c = begin(); c != end(); ++c)

{

if (*c != other[i]) return true;

i++;

}

return false;

}

bool pm1::Vector::operator<(pm1::Vector& other)

{
int i = 0;

// checking the size

if (size() < other.size()) return true;

// loopign through the list

for (auto c = begin(); c != end(); ++c)

{

// making sure it's less

if (*c < other[i]) return true;

i++;

}

return false;

}
bool pm1::Vector::operator<=(pm1::Vector& other)

{

int i = 0;

if (size() <= other.size()) return true;

if (size() > other.size()) return false;

// looping through the list

for (auto c = begin(); c != end(); ++c)

{

if (*c <= other[i]) return true;

i++;

}

return false;

}
bool pm1::Vector::operator>(pm1::Vector& other)

{

int i = 0;

// return false if the opposite is true

if (size() <= other.size()) return false;

// return true if the opposite is true

if (size() > other.size()) return true;

for (auto c = begin(); c != end(); ++c)

{

// once the first element is reached where c > other[i] return true

if (*c > other[i]) return true;

i++;

}

return false;
}
bool pm1::Vector::operator>=(pm1::Vector& other)

{

int i = 0;

if (size() < other.size()) return false;

if (size() >= other.size()) {

for (auto c = begin(); c != end(); ++c)

{

// return false is `this` is less than `other`

if (*c < other[i]) return false;

i++;

}

return true;

}

return false;

}

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