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

I was given a Class vector: #include <iostream> #ifndef VECTOR_H #define VECTOR_

ID: 3821585 • Letter: I

Question

I was given a Class vector:

#include <iostream>

#ifndef VECTOR_H
#define VECTOR_H

class Vector
{

public:
Vector();  //default constructor
Vector(int s); //makes size = s
     //allocate s space
     //makes all entries 0
Vector(const Vector & other);
//copy constructor
//makes a deep copy
~Vector();  //default destructor
void print(); //Prints out the vector

void get(int val, int pos); //Prints out the vector

Vector operator+ (const Vector &other) const;

Vector operator= (const Vector & other);

private:
int size;  //sets the # of elements used
int *entries; //point to array of integers with size entries
     //e.g. entries = new int[size]
};

#endif

I was also given a main function or drover function:

#include "Vector.h"

#include <iostream>

using namespace std;

int main()
{
Vector a, b(3), c(3);
b.get(); // user enters 1 2 3
c.get(); // user enters -2 1 0

a = b + c;
cout << "The sum of ";
b.print();

cout << " and ";
c.print();

cout << " is ";
a.print();

return 0;
}

I was able to implement the class. I have the implemnation below, but I cant seem to implement some of the the member functions (such as get and the operators) and need help. This is what I have so far:

#include "Vector.h"

#include <iostream>

using namespace std;

Vector::Vector()
{
size = 0;
entries = new int[size];
for (int i = 0; i < size; i++)
{
  entries[i] = 0;
}

}

Vector::Vector(int s)
{
size = s;
entries = new int[size];
for (int i = 0; i < size; i++)
{
  entries[i] = 0;
}
}

Vector::Vector(const Vector & other)
{
this->size = other.size;
this->entries = new int[size];
for (int i = 0; i < size; i++)
{
  this->entries[i] = other.entries[i];
}
}

Vector::~Vector()
{
this->entries = NULL;
}


void Vector::print()
{
cout << endl << "[";
for (int i = 0; i < size; i++)
{
  cout << entries[i] << "";
}

cout << "]" << endl;
}


void Vector::get()
{

}

Explanation / Answer

Here is the code for myVector.h:

#include <iostream>
#ifndef VECTOR_H
#define VECTOR_H
class Vector
{
public:
Vector(); //default constructor
Vector(int s); //makes size = s
//allocate s space
//makes all entries 0
Vector(const Vector & other);
//copy constructor
//makes a deep copy
~Vector(); //default destructor
void print(); //Prints out the vector
void set(int val, int pos); //Sets the value val at specified position if it is valid.

Vector operator+ (const Vector &other) const;
Vector operator= (const Vector & other);
private:
int size; //sets the # of elements used
int *entries; //point to array of integers with size entries
//e.g. entries = new int[size]
};
#endif

And the code for myVector.cpp:

#include "myVector.h"
#include <iostream>
using namespace std;

//Initializes the vector with 0 size.
Vector::Vector()
{
size = 0;
entries = new int[size];
for (int i = 0; i < size; i++)
{
entries[i] = 0;
}
}

//Initializes the vector with specified size s.
Vector::Vector(int s)
{
size = s;
entries = new int[size];
for (int i = 0; i < size; i++)
{
entries[i] = 0;
}
}

//Initializes the vector with the members of another vector other.
Vector::Vector(const Vector & other)
{
this->size = other.size;
this->entries = new int[size];
for (int i = 0; i < size; i++)
{
this->entries[i] = other.entries[i];
}
}

//Deletes the vector (Destructor).
Vector::~Vector()
{
this->entries = NULL;
}

//Prints the contents of vector.
void Vector::print()
{
cout << endl << "[";
for (int i = 0; i < size; i++)
{
cout << entries[i] << " ";
}
cout << "]" << endl;
}

//Sets the value val at specified position if it is valid.
void Vector::set(int val, int pos)
{
if(pos < size)
    entries[pos] = val;
}

//Add the vector to other vector and stores in a vector, and will return.
Vector Vector::operator+ (const Vector &other) const
{
    Vector temp(size + other.size);
    for(int i = 0; i < size; i++)
        temp.set(entries[i], i);
    for(int i = 0; i < other.size; i++)
        temp.set(other.entries[i], size+i);
    return temp;      
}

//Assigns the values in other to this vector.
Vector Vector::operator= (const Vector & other)
{
   size = other.size;
   for(int i = 0; i < size; i++)
       entries[i] = other.entries[i];
   return *this;  
}

Finally the tester code is:

#include "myVector.cpp"
#include <iostream>
using namespace std;
int main()
{
Vector a, b(3), c(3);
//Something wrong with these 2 calls. The definition doesn't match the call.
//It needs some modification. After modification the code now looks...
// user enters 1 2 3
b.set(1, 0);
b.set(2, 1);
b.set(3, 2);
// user enters -2 1 0
c.set(-2, 0);
c.set(1, 1);
c.set(0, 2);
a = b + c;

cout << "The sum of ";
b.print();
cout << " and ";
c.print();
cout << " is ";
a.print();
return 0;
}

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