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

Language: C++ I need help creating a class called DynamicArray and its purpose i

ID: 3749410 • Letter: L

Question

Language: C++

I need help creating a class called DynamicArray and its purpose is to store an array that uses the concept of templates to store any type of data. It needs accessors: unsigned int GetCapacity() const; , unsigned int GetSize() const; , const T * GetData() const; , const T &operator[](unsigned int index) const; , T & operator [](unsigned int index); ,  const T &At(unsigned int index) const; , and T &At(unsigned int index); It also needs mutators void Add(const T &data); , which adds item to end of array if there's room, void Resize(unsigned int newSize); , which resizes the array if there's not enough room, and void Remove(unsigned int index); , which removes element from array and shrinks array to the gap. It also needs constructors, assignment operators, and destructor. All of this is suppose to be in a header file DynamcArray.h

Explanation / Answer

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks

//Code

//DynamicArray.h

#ifndef DynamicArray_h

#define DynamicArray_h

#define INITIAL_CAPACITY 10

template <typename T>

class DynamicArray{

              private:

              //array pointer

              T* array;

              //current size

              int size;

              //current capacity

              int max_capacity;

              public:

              //constructor

              DynamicArray(){

                             //initializing array pointer with default capacity (10)

                             array=new T[INITIAL_CAPACITY];

                             size=0;

                             max_capacity=INITIAL_CAPACITY;

              }

              //destructor

              ~DynamicArray(){

                             //de allocating the memory

                             delete array;

              }

              //returns the current capacity

              unsigned int GetCapacity() const{

                             return max_capacity;

              }

              //returns the current size

              unsigned int GetSize() const{

                             return size;

              }

              //returns the array

              const T * GetData() const{

                             return array;

              }

              //returns the element at given index

              const T &operator[](unsigned int index) const{

                             if(index>=0 && index<size){

                                           return array[index];

                             }

              }

              //returns the element at given index

              T & operator [](unsigned int index){

                             if(index>=0 && index<size){

                                           return array[index];

                             }

              }

              //returns the element at given index

              const T &At(unsigned int index) const{

                             if(index>=0 && index<size){

                                           return array[index];

                             }

              }

              //returns the element at given index

              T &At(unsigned int index){

                             if(index>=0 && index<size){

                                           return array[index];

                             }

              }

             

              //method to add an element to the array

              void Add(const T &data){

                             //ensuring capacity before adding

                             if(size==max_capacity){

                                           //resizing the array with twice the capacity

                                           Resize(max_capacity*2);

                             }

                             array[size]=data;

                             size++;

              }

              //method to resize the array

              void Resize(unsigned int newSize){

                             max_capacity=newSize;

                             //creating array with new size, copying all elements

                             T* newArray=new T[max_capacity];

                             for(int i=0;i<size;i++){

                                           newArray[i]=array[i];

                             }

                             //assigning new array to original array pointer

                             array=newArray;

              }

              //method to remove an element at given index

              void Remove(unsigned int index){

                             if(index>=0 && index<size){

                                           //shifting all remaining elements to the left

                                           for(int i=index;i<size-1;i++){

                                                          array[i]=array[i+1];

                                           }

                                           size--;

                             }

              }

              //assignment operator to do a copy of current array

              DynamicArray & operator = (const DynamicArray &other){

                             DynamicArray<T> newDynamicArray;

                             for(int i=0;i<other.GetSize();i++){

                                           newDynamicArray.Add(other[i]);

                             }

                             return newDynamicArray;

              }

};

#endif

//main.cpp

#include<iostream>

#include "DynamicArray.h"

using namespace std;

int main(){

              //creating a Dynamic array and testing methods thoroughly

              DynamicArray<int> array;

              for(int i=1;i<=10;i++){

                             array.Add(i);

              }

             

              cout<<"Size: "<<array.GetSize()<<endl;

              cout<<"Current capacity: "<<array.GetCapacity()<<endl;

              cout<<"Contents: "<<endl;

              for(int i=0;i<array.GetSize();i++){

                             cout<<array[i]<<" ";

              }

              cout<<endl;

              cout<<"Adding some more elements.."<<endl;

              for(int i=100;i<105;i++){

                             array.Add(i);

              }

              cout<<"Size: "<<array.GetSize()<<endl;

              cout<<"Current capacity: "<<array.GetCapacity()<<endl;

              cout<<"Contents: "<<endl;

              for(int i=0;i<array.GetSize();i++){

                             cout<<array[i]<<" ";

              }

              cout<<endl;

             

              cout<<"Removing element at 5th index"<<endl;

              array.Remove(5);

             

              cout<<"Contents: "<<endl;

              for(int i=0;i<array.GetSize();i++){

                             cout<<array[i]<<" ";

              }

             

}

/*OUTPUT*/

Size: 10

Current capacity: 10

Contents:

1 2 3 4 5 6 7 8 9 10

Adding some more elements..

Size: 15

Current capacity: 20

Contents:

1 2 3 4 5 6 7 8 9 10 100 101 102 103 104

Removing element at 5th index

Contents:

1 2 3 4 5 7 8 9 10 100 101 102 103 104