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 redefining your own MyVector class using dynami

ID: 3789686 • Letter: F

Question

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

This assignment requires you to use a header file, MyVector.h

#ifndef MYVECTOR_
#define MYVECTOR_
/*Defines a MyVector type which mirrors the STL vector class. It uses templates and 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

Ensure that you comment all of your code well (including your name, and more importantly how and why you are using the pointers and dynamic memory allocation to meet the requirements.) Use the STL<vector> as you guide to what the functions are to do, but do NOT add the STL vector or the <algorithm> library.

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.

Explanation / Answer

PROGRAM CODE:

/*
* MyVector.cpp
*
* Created on: 11-Feb-2017
* Author: kasturi
*/


#include "MyVector.h"
#include <iostream>
#include <cstdlib>

//Constructor for Myvector
HW4::MyVector::MyVector()
{
   vsize = 0;
   vec = nullptr;
}

//pushing values inside the vector
void HW4::MyVector::push_back(T value)
{
   if(vec == nullptr)
   {
       vec = (T*) malloc(sizeof(T));
       *vec = value;
   }
   else
   {
       *(vec + vsize) = value;
   }
   vsize++;
}

//popping values out of the vector
void HW4::MyVector::pop_back()
{
   *(vec+vsize) = NULL;
   vsize--;
}

//getting values at a particular index
HW4::T HW4::MyVector::operator[] (int index)
{
   T *temp = vec;
   int i = 0;
   while(i<vsize)
   {
       if(i == index)
           return *temp;
       else
       {
           temp++;
           i++;
       }
   }
   return NULL;
}

//getting the index of a vlaue
int HW4::MyVector::search(T Value)
{
   int index = -1;
   int i = 0;
   T *temp = vec;
   while(temp != nullptr)
   {
       if(*temp == Value)
           return i;
       else
       {
           temp++;
           i++;
       }
   }
   return index;
}
//returns the size of the vector
int HW4::MyVector::size()
{
   return vsize;
}

//returns true or false indicating whether the vector is empty
bool HW4::MyVector::empty()
{
   if(vsize == 0)
       return true;
   else return false;
}
//main function - just for testing
int main()
{
   HW4::MyVector vec;
   vec.push_back(23);
   vec.push_back(24);
   vec.push_back(25);
   vec.pop_back();
   std::cout<<"Index of 24: "<<vec.search(24);
   std::cout<<" Value at index 2: "<<vec[2];
}

OUTPUT:

Index of 24: 1

Value at index 2: 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