In C only use include and please show your output For this a
In C++
only use #include <iostream>
and please show your output
For this assignment you will implement a dynamic array. You are to build a class called MyDynamicArray. Your dynamic array class should manage the storage of an array that can grow and shrink. The public methods of your class should be the following:
 MyDynamicArray(); Default Constructor. The array should be of size 2.
 MyDynamicArray(int s); For this constructor the array should be of size s.
 ~MyDynamicArray(); Destructor for the class.
 int& operator[](int i); Traditional [] operator. Should print a message if
 i is out of bounds and return a reference to
 a zero value.
 void add(int v); increases the size of the array by 1 and
 stores v there.
 void del(); reduces the size of the array by 1.
 int length(); returns the length of the array.
 int clear(); Frees any space currently used and starts
 over with an array of size 2.
The output should be:
Doubling to : 4
 Doubling to : 8
 Doubling to : 16
 Doubling to : 32
 Doubling to : 64
 Doubling to : 128
 The sum is : 4950
 Reducing to : 64
 Reducing to : 32
 Reducing to : 16
 Out of bounds reference : 60
 Doubling to : 20
 Doubling to : 40
 Doubling to : 80
 Doubling to : 160
 Doubling to : 320
 The sum is : 20185
 Reducing to : 160
 Reducing to : 80
 Reducing to : 40
 Out of bounds reference : 60
 Doubling to : 80
 Doubling to : 160
 Doubling to : 320
 The sum is : 20195
This is the sample main file:
#include <iostream>
 using namespace std;
 #include \"MyDynamicArray.cpp\"
 int main() {
 MyDynamicArray x;
 for (int i=0; i<100; i++){
 x.add(i);
 }
 int sum = 0;
 for (int i=0; i<x.length(); i++){
 sum+=x[i];
 }
 cout << \"The sum is : \" << sum << endl;
 for (int i=0; i<95; i++)
 x.del();
 x[60] = 27;
 MyDynamicArray y(10);
 for (int i=0; i<y.length(); i++) y[i] = i*i;
 for (int i=0; i<200; i++){
 y.add(i);
 }
 sum = 0;
 for (int i=0; i<y.length(); i++){
 sum+=y[i];
 }
 cout << \"The sum is : \" << sum << endl;
 for (int i=0; i<195; i++)
 y.del();
 y[60] = 27;
 for (int i=0; i<200; i++){
 y.add(i);
 }
 sum = 0;
 for (int i=0; i<y.length(); i++){
 sum+=y[i];
 }
 cout << \"The sum is : \" << sum << endl;
 }
Solution
// your output and question do not match so doing as per output
 # include <iostream>
 using namespace std;
class MyDynamicArray
 {
 private:
 int *arr;
 int siz;
 int length_a;
   
 public:
 MyDynamicArray(int s)
 {
 arr = new int[s];
 length_a = s;
 siz = 0;
 }
MyDynamicArray()
 {
 arr = new int[2];
 length_a = 2;
 siz = 0;
 }
~MyDynamicArray()
 {
 //delete [] arr;
 }
int& operator[](int i)
 {
 if (i >= siz)
 {
 cout << \"Out of bounds reference : \" << i <<endl;
 int y = 0;
 int& a = y;
 return a;
 }
   
 return arr[i];
 }
 void add(int v)
 {
 if (siz < length_a)
 {
 arr[siz] = v;
 siz =siz+1;
 }
 else
 {
 cout << \"Doubling to : \" << 2*length_a <<endl;
 int *a = new int[2*length_a];
 //memcpy( a, arr, siz * sizeof(int) );
  
 for (int i = 0; i < siz; i++)
 {
 a[i] = arr[i];
 }
 length_a = 2*length_a;
 delete [] arr;
 arr = a;
 arr[siz] = v;
 siz = siz+1;
 }
 }
void del()
 {
 if (siz > 0 && length_a > 0)
 {
 if (siz - 1 < length_a/2)
 {
 cout << \"Reducing to : \" << length_a/2 << endl;
 int *a = new int[length_a/2];
 for (int i = 0; i < siz -1; i++)
 {
 a[i] = arr[i];
 }
 length_a = length_a/2;
 siz--;
 delete [] arr;
 arr = a;
 }
 else
 {
 siz--;
 }
 }
 }
int length()
 {
 return siz;
 }
int clear()
 {
 delete [] arr;
 arr = new int[2];
 siz = 0;
 length_a = 2;
 }
 };




