Language C For this assignment you will be redefining your o
Language: C++
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, 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 as your guide to what the functions are to do, but do NOT add the STL vector or the library.
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.
MyVector.h File:
#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
Solution
Explanation :-
---------------------
Vecctor have properties Size , Capacity
Capacity :- How many Elements Vector can Hold
Size : How many elements Vector currently have
Iinitially Capacity Set to Default value
If We try to insert element beond Capacity , Array size will be increase using \"new\"
in this programme currnet capacity set to 10
MyVector.h
------------------------
#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:
 //Current Elements size in Vector
 int vsize = 0;
 //Capacity of Vector, set 10 intially
 int capacity = 10;
T* vec = new T[capacity];
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);
 // Increases the capacity of the underlying array to be sz. If sz
 // is smaller than the current capacity then nothing is done.
 void increase_capacity(int sz);
};
}//namespace
#endif
MyVector.cpp :-
-------------------------
#include \"MyVector.h\"
 #include <string.h>
 using namespace HW4;
 MyVector::MyVector()
 {
}
T MyVector::operator[] (int index){
    //Check given index valid
    if(index < 0 || index >vsize-1)
        return -1;
    //Retunr value at index
    return vec[index];
 }
void MyVector::pop_back(){
     //Set last element to 0,it is used to store new element
     vec[vsize-1] = 0;
     --vsize;
 }
void MyVector::push_back(T value){
     //If size exceds capacity then increase capacity
     if (vsize >= capacity) increase_capacity(2 * capacity);
         vec[vsize] = value;
         ++vsize;
 }
int MyVector::size(){
     //Return size
     return vsize;
 }
bool MyVector::empty(){
    //Check if Size > 0
    return (vsize > 0);
 }
int MyVector::search(T Value){
    int index = -1;
    for(int i = 0; i < vsize; ++i) { // copy old vector into new one
      //If match occurred
      if(vec[i] == Value){
          index= i;
          break;
      }
    }
    //Return result
    return index;
 }
// Increases the capacity of the underlying array to be sz. If sz
 // is smaller than the current capacity then nothing is done.
 void MyVector::increase_capacity(int sz) {
     if (sz <= capacity) return;
T* new_arr = new T[sz]; // allocate a new array on the free store
    //Set all elments 0 by default
     memset(new_arr,0,sz);
    for(int i = 0; i < vsize; ++i) { // copy old vector into new one
       new_arr[i] = vec[i];
     }
     capacity = sz;                      // set the new capacity
    delete[] vec;                       // delete the old vector
     vec = new_arr;
 }
 main.cpp
--------------------
#include <iostream>
 #include\"MyVector.h\"
 using namespace std;
 using namespace HW4;
 int main(int argc, char *argv[])
 {
     MyVector v;
     //Insert elements into vector
     for(int i = 0 ; i < 100 ; i++){
         v.push_back(i);
     }
     cout<<\"Size of Vector is\"<<v.size()<<endl;
     //Check elemets at positions
     for(int i = 0 ; i < 100 ; i++){
         cout<<\"Element at position :\"<<(i+1)<<\" is\"<<v[i]<<endl;
         v.push_back(i);
     }
     //Search Elements
     cout<<\"Elements 999 Search Result:\"<<v.search(999)<<endl;
     cout<<\"Elements 56 Search Result:\"<<v.search(56)<<endl;
    //Pop Element
     v.pop_back();
     return 0;
 }
 MakeFile :-
----------------------
CXX = g++
 CPPFLAGS =        # put pre-processor settings (-I, -D, etc) here
 CXXFLAGS = -Wall # put compiler settings here
 LDFLAGS =         # put linker settings here
MyVector: MyVector.o main.o
    $(CXX) -o $@ $(CXXFLAGS) $(LDFLAGS) MyVector.o main.o
.cpp.o:
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $<
MyVector.cpp: MyVector.h




