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

#include <iostream> #include <vector> #include <string> #include \"DynamicArray.

ID: 3752088 • Letter: #

Question

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

// Sample class, to show a template can work with anything
class Hero
{
   string name_;
   int hitpoints_;
   int maxHP_;
public:
   Hero() {}
   Hero(const char *name, int hp, int maxHP)
   {
       name_ = name;
       hitpoints_ = hp;
       maxHP_ = maxHP;
   }
   void SetName(const char *name)
   {
       name_ = name;
   }
   string GetName()
   {
       return name_;
   }
   friend ostream &operator<<(ostream &os, const Hero &h)
   {
       cout << h.name_ << " Hitpoints: " << h.hitpoints_ << " / " << h.maxHP_;
       return os;
   }
};

void TestOne();
void TestTwo();
void TestThree();
void TestFour();

int main()
{
   cout << "**** Testing the Dynamic Array ****" << endl << endl;
   TestOne();
   TestTwo();
   TestThree();
   TestFour();
  
   return 0;
}

void TestOne()
{
   cout << "Integer container: Initial capacity of 0" << endl;
   DynamicArray<int> data(0);
   cout << "Adding 5 items..." << endl;
   for (int i = 1; i < 32; i *= 2)
       data.Add(i);
   data[4] = 500;
   for (unsigned int i = 0; i < data.GetSize(); i++)
       cout << data[i] << endl;

   cout << "Capacity: " << data.GetCapacity() << endl;
   cout << "Size: " << data.GetSize() << endl;
}
void TestTwo()
{
   DynamicArray<float> data2(10);
   cout << " Float container: Initial capacity of 10" << endl;
   cout << "Adding 5 items..." << endl;

   for (int i = 0; i < 5; i++)
       data2.Add((float)101 / (i + 2));

   for (unsigned int i = 0; i < data2.GetSize(); i++)
       cout << data2[i] << endl;

   cout << "Capacity: " << data2.GetCapacity() << endl;
   cout << "Size: " << data2.GetSize() << endl;

   cout << " Shrinking container to 2 elements..." << endl;
   data2.Resize(2);

   for (unsigned int i = 0; i < data2.GetSize(); i++)
       cout << data2[i] << endl;
   cout << "Capacity: " << data2.GetCapacity() << endl;
   cout << "Size: " << data2.GetSize() << endl;

   cout << "Attempting to access index[50]...";
   try { cout << data2[50] << endl; }
   catch (const char *msg)
   {
       cout << msg << endl;
   }
}
void TestThree()
{
   DynamicArray<int> data3;

   data3.Add(100);
   data3.Add(999);
   data3.Add(200);
   data3.Add(300);
   data3.Add(400);

   for (unsigned int i = 0; i < data3.GetSize(); i++)
       cout << data3[i] << endl;

   cout << "Removing '999' from the list... " << endl;
   data3.Remove(1);

   for (unsigned int i = 0; i < data3.GetSize(); i++)
       cout << data3[i] << endl;

   cout << "Creating a copy of the list (testing the copy constructor)... " << endl;
   auto data4 = data3;

   cout << " Printing copy of dynarray..." << endl;
   for (unsigned int i = 0; i < data4.GetSize(); i++)
       cout << data4[i] << endl;
  
   cout << " Attempting to remove index[100]..." << endl;
   try { data3.Remove(100); }
   catch (const char *msg)
   {
       cout << msg << endl;
   }

}
void TestFour()
{
   DynamicArray<Hero> heroes;
   heroes.Add(Hero("Conan", 100, 120));
   heroes.Add(Hero("Thor", 150, 150));
   heroes.Add(Hero("Merlin", 80, 85));
   heroes.Add(Hero("Bob", 25, 26));
   cout << " Printing heroes from dynarray..." << endl;

   for (unsigned int i = 0; i < heroes.GetSize(); i++)
       cout << heroes[i] << endl;

   DynamicArray<Hero> heroClones;
   cout << " Cloning our heroes (testing the assignment operator)... ";
   heroClones = heroes;

   cout << " Printing cloned heroes from dynarray (and changing their names)..." << endl;
   for (unsigned int i = 0; i < heroClones.GetSize(); i++)
   {
       string name = "Evil " + heroClones[i].GetName();
       heroClones[i].SetName(name.c_str());
       cout << heroClones[i] << endl;
   }
}

Description All programs need storage of some kind as they execute. Arrays are the most basic, and probably the most common form of storage. However, because of their fixed nature they can be problematic to use, particularly if you need to store data of varying lengths as your application's needs change over time. One of the first data structures people start to use in any programming language is some form of an expandable array. Java has the ArrayList class, C# has the List class, and even C++ has the vector class. The DynamicArray class you create will be very similar to the vector class. The concepts behind allocating and freeing memory will be applicable when implementing numerous other data structures and algorithms, so you aren't reinventing the wheel here. You're learning how wheels are made, so you can build your own, custom version of them as needed in later projects. Templates Templates in C++ can be a bit confusing at first, but they allow you to reuse code very easily. By defining a class as a template, you can create multiple instances of that class to use different types. For example: Foo a; // Instance of Foo which uses ints Foo b; // Instance of Foo which uses floats Foo c; // Instance of Foo which uses Bars The DynamicArray class you write is going to be a storage container, and while you may create storage containers in the future that are custom-built to solve a single problem, reusable code can make your life much easier. DynamicArray Storage Internally, a class like this does not store much data. There are only three data members that you are required to store (though you may add any additional variables you see fit to help you write this) Data A pointer to the data you are storing. This will be based on the type-id you define for the template. This pointer is the core of this class. It's where the magic happens. Size How many objects are being stored currently? Capacity- How many objects COULD be stored? This is not the same as the size. .

Explanation / Answer

#include <stdio.h>

#include <iostream>

template<class T>

class DynamicArray

{

public:

DynamicArray(){

actualSize = initialsize_of_array;

size = 0;

array = (T *)malloc(actualSize*sizeof(T));

}

~DynamicArray(){

if (array)

{

free(array);

array = NULL;

}

}

void Add(const T &item)

{

size++;

if (size > actualSize)

{

actualSize *= enlarge_array;

array = (T *)realloc(array, sizeof(T)*actualSize);

}

array[size-1] = item;

}

int GetSize()

{

return size;

}

void ReSize(int newsize){

size = newsize;

if (size != 0)

{

if ((size > actualSize) || (size < actualSize/2))

{

actualSize = size;

array = (T *)realloc(array, sizeof(T)*size);

}

}

}

void Delete(int index)

{

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

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

size--;

}

int GetCapacity()

{

return initialsize_of_array;

}

DynamicArray& operator = (const DynamicArray &a);

T& operator [] (unsigned int index);

private:

T *array;

int size;

int actualSize;

static int initialsize_of_array = 128;

static int enlarge_array = 2;

};

template <class T>

T& DynamicArray<T>::operator [] (unsigned int index)

{

return array[index];

}

template <class T>

DynamicArray<T>& DynamicArray<T>::operator = (const DynamicArray &a)

{

if (this == &a)

return *this;

ReSize(a.size); // set size

memcpy(array, a.array, sizeof(T)*a.size);

return *this;

}

Hai I referred some of the code from google like assignment operater.

Thank you.