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

NOTE: You may NOT alter, edit, change, etc. Vector.h, rather you will write your

ID: 3877053 • Letter: N

Question

NOTE: You may NOT alter, edit, change, etc. Vector.h, rather you will write your own Vector.cc implementation solution. You are required to expand the main.cc as your test harness. For full credit, you must also enumerate test harness code that tests ordinary and edge cases. Carefully consider possible inputs to your program’s functions and the correct behavior you intend to test for.

Strategy: It is suggested you comment out sections of Vector.h and implement a Vector.cc that compiles with the sample main.cc provided. For example, you will need push_back(), clear(), display(), and operator[ ] to compile with the supplied main.cc. Consider beginning by implementing push_back() and display() only. Test it with the main.cc, commenting out those sections that operate on clear() and operator[ ]. Then repeat this process as you tackle more member functions.

Summary: You will implement a user defined Vector class with similar functionality to the Standard Template Library vector class. In addition, you will implement a test bench with unit tests that thoroughly test, via program execution, each of the member functions of your Vector class. A Vector.h (see below) and sample main.cc file are provided for download which is listed below.

C++ code ------

main . cc
#include <iostream>
#include "Vector.h"

using namespace std;

int main ()
{
Vector myv;
myv.push_back(7);
myv.push_back(77);
myv.push_back(777);

// Test push_back()
// output = "myv contents:7, 77, 777"
cout << "myv contents:";
myv.display();

myv.clear();
myv.push_back(2001);
myv.push_back(2012);

// Test clear()
// output = "myv contents:2001, 2112"
cout << "myv contents:";
myv.display();

// Test operator[]
for(int i = 0; i < myv.size(); i++)
    cout << myv[i] << ", ";
cout << endl;

return 0;
}

Vector.h
#ifndef VECTOR_H_
#define VECTOR_H_

class Vector {
private:
    int capacity = 100; //initialize capacity to 100
    int v[100]; //declare internal array of size 100
    int s = 0; //initialize current number of items in v to 0
public:
    Vector(); //default constructor
    // mutators
    void push_back(int item); //inserts item at position size-1
    void push_front(int item); //inserts item at position 0, shifts items right
    bool swap_i_j(int i, int j); //swaps the items at pos i and j, true if success
    void swap(Vector v2); //swaps contents, at same positions of v and v2
    void reverse(); //reverses the items in v
    void clear();   //clears the items in v, sets s to 0
  
    //accessors
    int size() const; // returns the number of items in v
    void display() const; //prints the contents of v to std::out
    bool is_empty() const; //true if size()==0, false otherwise
    const int& at(int i) const; //returns const item& at position i in v
    int find(int p) const; //returns position of 1st instance of p in v, -1 if not found
    int find(int a[], int size_a); //returns pos of 1st instance a[size_a] in v, -1 if not found
  
    // overloaded operators
    bool operator==(Vector rhs); //true if this==rhs, item wise, false otherwise
    const int& operator[](int i); //returns const item& at position i in v
};
#endif /* VECTOR_H_ */

Vector.cc

code requried.

Explanation / Answer

vector.cc

#include <vector>

#include <array>

#include <iostream>

using namespace std;

int main() {

vector<std::array<int, 3>> vec;

  

array<int, 3> a0 { {0, 1, 2} };

array<int, 3> a1 { {3, 4, 5} };

array<int, 3> a2 { {6, 7, 8} };

  

vec.push_back(a0);

vec.push_back(a1);

vec.push_back(a2);

for(array<int, 3> i : vec) {

cout << i[0] << " " << i[1] << " " << i[2] << endl;

}

  

return 0;

}