Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help on exercise 1 on building my vector, I will provide my code below but can anyone help me improve my code based

image text in transcribed

I need help on exercise 1 on building my vector, I will provide my code below but can anyone help me improve my code based on exercise 1?

#ifndef VECTOR_H_INCLUDED #define VECTOR_H_INCLUDED

#include #include #include #include

template

class Vector

{ public: explicit Vector(int initSize = 0): theSize{init Size}, theCapacity{initSize + SPARE_CAPACITY} { data = newdata[theCapacity]; }

Vector( const Vector & rhs) : theSize{rhs.theSize}, theCapacity{rhs.theCapacity}, data{nullptr} { data = new data [theCapacity]; for (int k = 0; k

Vector & operator= (const Vector & rhs) { Vector copy = rhs; std::swap(*this, copy); return *this; }

~Vector() { delete[ ] data; }

Vector(Vector && rhs) : theSize{rhs.theSize}, theCapacity{rhs.theCapacity}, data{rhs.data} { rhs.data= nullptr; rhs.theSize= 0; rhs.theCapacity= 0; }

Vector & operator= (Vector && rhs) { std::swap( theSize, rhs.theSize); std::swap( theCapacity, rhs.theCapacity); std::swap(data, rhs.data);

return *this; }

void resize(int newSize) { if(newSize > theCapacity) reserve(newSize * 2); theSize= newSize; }

void reserve( int newCapacity) { if(newCapacity

data *newArray = new Object[newCapacity]; for (int k = 0; k

theCapacity = newCapacity; std::swap(objects, newArray); delete[] newArray;

}

data & operator[](int index) { return size() == 0; } const data & operator [](int index) const { return data[index]; }

bool empty() const { return size() == 0; }

int size() const { return theSize; }

int capacity() const { return theCapacity; }

void push_back(const data & x) { if(theSize == theCapacity) reserve(2 * theCapacity + 1); data[ theSize++ ]= x; }

void push_back(data && x) { if(theSize == theCapacity) reserve(2 * theCapacity + 1); data[theSize++] = std::move(x); }

void pop_back() { --theSize; }

const data & back () const { return data[theSize -1]; }

typedef data * iterator; typedef const data * const_iterator;

iterator begin() { return &objects [0]; }

const_interator begin() const { return &data[0]; }

iterator end() { return &data[size()]; } const_interator end() const { return &data[size()]; }

static const int SPARE_CAPACITY = 16;

private: int theSize; int theCapacity; T*data;

};

CSE 2020 LABORATORY -- Week 2 - Spring 2021 Instructor: Kerstin Voigt This lab will have you do the hard labor of reproducing a C++ implementation of the Vector ADT. The implementation closely follows the one in Chapter 2 of Weiss, Data Structures and Algorithm Analysis in C++. However, the instructor made a few modifications: Vector anothervec (myvec): for (int i = 0; i STL o We will write ' instead of ''. The private data member 'object' is renamed to 'data'. Vector yetanother = anothervec; for (int i = 0; i #include "Vector.h" using namespace std; return 0 ) void print_vector (const Vector int>& v) cout Exercise 2: Test your Vector implementation with an Application. Download from Blackboard the MaxSubSumMain.cpp program. Compile and run it. The sample input to be used by all for uniform testing is in file lab2ex2_input.txt. Place this file in the same directory as program. CSE 2020 LABORATORY -- Week 2 - Spring 2021 Instructor: Kerstin Voigt This lab will have you do the hard labor of reproducing a C++ implementation of the Vector ADT. The implementation closely follows the one in Chapter 2 of Weiss, Data Structures and Algorithm Analysis in C++. However, the instructor made a few modifications: Vector anothervec (myvec): for (int i = 0; i STL o We will write ' instead of ''. The private data member 'object' is renamed to 'data'. Vector yetanother = anothervec; for (int i = 0; i #include "Vector.h" using namespace std; return 0 ) void print_vector (const Vector int>& v) cout Exercise 2: Test your Vector implementation with an Application. Download from Blackboard the MaxSubSumMain.cpp program. Compile and run it. The sample input to be used by all for uniform testing is in file lab2ex2_input.txt. Place this file in the same directory as program

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Genomes Browsers And Databases Data Mining Tools For Integrated Genomic Databases

Authors: Peter Schattner

1st Edition

0521711320, 978-0521711326

More Books

Students also viewed these Databases questions

Question

Develop goals and timetables.

Answered: 1 week ago

Question

Question 17 SYN packet consumes sequence number? A True B False B

Answered: 1 week ago