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

Modify the container class you wrote for Lab 2 to use pointers and dynamic array

ID: 3724567 • Letter: M

Question

Modify the container class you wrote for Lab 2 to use pointers and dynamic array allocation. Do not use the [ ] operator to access aray elements, instead use pointer Make the class templated instead of using a typedef statement (see the Bag class from Chapter 3). You may use one file for your template class instead of breaking it into 2 files. Test with an array of strings. Your class should inchude the following data members .A pointer to the array A variable to hold the current aray capacity .A variable to keep track of the number of elements currently being used. Include 2 constructors (or you can write one constructor with a default value for size): dynamically allocates an array of size 10 and initializes The default c capacity to 10 and num used variable to 0. A one argument constructor dynamically allocates an array of the size passed as an argument and initializes capacity to that number and num used variable to 0 Include a destructor that will release the allocated memory. Include a function to add a given element to the array at the next available location by feature If the array has reached its capacity, the add function should call a helper function to double the size of the array. The helper function will dynamically allocate storage for an array of twice the size, copy all the elements in the original array and update all the data members. The original pointer to the array should point to this new storage area after the copy is complete. The add function will then add the new element to the array A function to remove an e rearranged so that there are no holes" in the array). . A function to add an element at a given location (initial elements must be moved to .

Explanation / Answer

here is your program : ---------------->>>>>>>

#include<iostream>
#define unsigned int size_t

using namespace std;

template<class T>
class Array{
T *p;
size_t capacity_;
size_t num_;
void doubleArray();
public:
  Array(size_t capacity_ = 10);
  void add(T &element);
  void add(size_t index,T &element);
  void remove(size_t index);
  void printArray();
};

template<class T>
void Array<T>::doubleArray(){
T *temp = new T[capacity_*2];
for(int i = 0;i<num_;i++){
  *(temp + i) = *(p + i);
}
delete p;
p = temp;
}

template<class T>
Array<T>::Array(size_t capacity_){
p = new T[capacity_];
num_ = 0;
this->capacity_ = capacity_;
}

template<class T>
void Array<T>::add(T &element){
if(num_ >= capacity_){
  doubleArray();
}
*(p + num_) = element;
num_++;
}
template<class T>
void Array<T>::add(size_t index,T &element){
if(index < 0 && index > num_){
  cout<<" Index Out Of Bound";
  return;
}
if(num_ >= capacity_){
  doubleArray();
}
if(num_ == index){
  add(element);
  return;
}
for(size_t i = num_;i > index;i--){
  *(p + i) = *(p + (i-1));
}
*(p + index) = element;
num_++;
}

template<class T>
void Array<T>::printArray(){
cout<<endl;
for(size_t i = 0;i<num_;i++){
  cout<<" "<<*(p + i);
}
}
template<class T>
void Array<T>::remove(size_t index){
if(index < 0 && index >= num_){
  cout<<" Index Out Of Bound";
  return;
}
for(size_t i = index;i<num_-1;i++){
  *(p+i) = *(p + (i+1));
}
num_--;
}

int main(){
Array<string> a;
string s = "Cat",s1= "Dog",s2="Horse",s3="Elephant",s4="Cheetah",s5="Beer";
a.add(s);
a.add(s2);
a.add(s3);
a.add(s4);
a.add(s5);
a.add(1,s1);
a.printArray();
a.remove(1);
a.printArray();
a.add(1,s1);
a.printArray();
}

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