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

For this assignment, you will be redefinining your own MyVector class using dyna

ID: 3789239 • Letter: F

Question

For this assignment, you will be redefinining your own MyVector class using dynamic memory allocation(dynamic allocating arrays)

In addition to the main.cpp, MyVector.h, MyVector.cpp you must submit a completed readme.txt file, a makefile AND an analysis.txt file. In the analysis.txt file you will provide a partial asymptotic analysis. This analysis will list the function, the Big O notation, and importantly how you determined this for each of these functions: Operator [ ], pop_back, push_back and search (based on constant expressions, loops, nested loops etc.). Note that this analysis should be based on YOUR code, not something you might look up for a vector STL.

#ifndef MYVECTOR_

#define MYVECTOR_

/*Defines a MyVector type which mirrors the STL vector class. It uses templatesand dynamic memory allocation*/
namespace HW4

{

typedef int T;

class MyVector

private:    

int vsize = 0;    

T* vec = nullptr;
public:    

MyVector();    

T operator[] (int index);    

void pop_back();   

void push_back(T value);   

int size(); //returns the vector size   

bool empty();//determine if is empty   

int search(T Value);    

  };
}//namespace

#endif

C++ Language

Can someone help me on this problem?

Explanation / Answer

//vector.h same as given

#ifndef MYVECTOR_
#define MYVECTOR_
/*Defines a MyVector type which mirrors the STL vector class. It uses templatesand dynamic memory allocation*/
namespace HW4
{
   typedef int T;
   class MyVector
   {
private:
   int vsize = 0;
   T* vec = nullptr;
public:
   MyVector();
   T operator[] (int index);
   void pop_back();
   void push_back(T value);
   int size(); //returns the vector size   
   bool empty();//determine if is empty   
   int search(T Value);
};
}//namespace
#endif

----------------------------------------------------------------

//vector.cpp

#include"vector.h"
#include<stdlib.h>
#include<iostream>

using namespace std;
using namespace HW4;

MyVector::MyVector()
{
   vec = NULL;
   vsize = 0;
}

T MyVector::operator[](int index)
{
   return vec[index];
}

void MyVector::pop_back()
{
   T *tmp;
   tmp = new T[vsize- 1];
  
   for (int i = 0; i < vsize-1; i++)
   {
       tmp[i] = vec[i];
   }
   cout << vec[vsize-1] << endl;
   delete[]vec;
   vec = tmp;
   --vsize;
}

void MyVector::push_back(T value)
{
   T* tmp = vec;
   if (empty())
   {
       vec = new T[1];
       vec[0] = value;
   }
   else
   {
       tmp = new T[vsize + 1];;
       int i;
       for ( i = 0; i < vsize; i++)
       {
           tmp[i] = vec[i];
       }
       tmp[i] = value;
       delete[]vec;
       vec = tmp;
   }
   ++vsize;
}

int MyVector::size()
{
   return vsize;
}
bool MyVector::empty()
{
   if (vec == NULL)
       return true;
   else
       return false;
}
//return index of the element if it is found in vector
int MyVector::search(T Value)
{
   int found = -1;
   for (int i = 0; i < vsize; i++)
   {
       if (vec[i] == Value)
       {
           found = i;
           break;
       }
   }
   return found;
}

---------------------------------------------------------------------------

//main.cpp

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

int main()
{
   MyVector v;
  
   v.push_back(1);
   v.push_back(2);
   v.push_back(3);
   v.push_back(4);
   cout << "Elements of vector: ";
   for (int i = 0; i < v.size(); i++)
   {
       cout << v[i] << " ";
   }
   cout << endl;
   //test search vector element
   int index;
   if (( index = v.search(3)))
   {
       cout << "Elelement 3 is found at position " << index+1 << endl;
   }
   else
       cout << "Elelement is not found" << endl;
   cout << "Poped elements of vector : " << endl;
   v.pop_back();
   cout << "Poped elements of vector : " << endl;
   v.pop_back();
   cout << "Elements of vector after pop operation: ";
   for (int i = 0; i < v.size(); i++)
   {
       cout << v[i] << " ";
   }
   cout << endl;
  
}

-------------------------------------------------------------------------------------

//output

Elements of vector: 1 2 3 4
Elelement 3 is found at position 3
Poped elements of vector :
4
Poped elements of vector :
3
Elements of vector after pop operation: 1 2

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