Question

In: Computer Science

For this assignment you will implement a dynamic array. You are to build a class called...

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.

You should write your class in the file MyDynamicArray.cpp. We will include your MyDynamicArray.cpp file into a main program that uses the dynamic array class. When the array grows or shrinks, you should print a message as seen in the sample output.


Here is a sample main.cpp 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;

    

}

Here is the output from the main.cpp above :

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 : 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


Solutions

Expert Solution

Please find the code implemented below.Make sure to save the file with name MyDynamicArray.cpp.

/*

* MyDynamicArray.cpp

*

* Created on: 20-Feb-2017

* Author: yourname

*/

#include <iostream>

class MyDynamicArray {

private:

   int size;

   int allocated;

   int *array;

public:

   MyDynamicArray() {

       allocated = 2;

       size = 0;

       array = new int[2];

   }

   MyDynamicArray(int s) {

       allocated = s;

       size = s;

       array = new int[s];

   }

   virtual ~MyDynamicArray() {

       delete array;

   }

   int& operator[](int i) {

       if (i >= size) {

           std::cout << "Out of bounds reference : " << i << std::endl;

           int t = 0;

           int *temp = &t;

           return *temp;

       }

       int *ref = &array[i];

       return *ref;

   }

   void add(int v) {

       if (size == allocated) {

           allocated *= 2;

           int *temp = new int[size];

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

               temp[i] = array[i];

           }

           array = new int[allocated];

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

               array[i] = temp[i];

           }

           delete[] temp;

           std::cout << "Doubling to : " << allocated << std::endl;

       }

       array[size++] = v;

   }

   void del() {

       if (size == 0) {

           return;

       }

       if (4 * size <= allocated) {

           allocated /= 2;

           int *temp = new int[size];

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

               temp[i] = array[i];

           }

           array = new int[allocated];

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

               array[i] = temp[i];

           }

           delete[] temp;

           std::cout << "Reducing to : " << allocated << std::endl;

       }

       --size;

   }

   int length() {

       return size;

   }

   void clear() {

       allocated = 2;

       array = new int[allocated];

       size = 0;

   }

};


Related Solutions

For this lab you will continue your dynamic array by completing the class called MyDynamicArray. The...
For this lab you will continue your dynamic array by completing the class called MyDynamicArray. The MyDynamicArray class should manage the storage of an array that can grow and shrink. The public methods of your class should already be the following: MyDynamicArray(); Default Constructor. The array should be of capacity 2. MyDynamicArray(int s); For this constructor the array should be of capacity and size s. int& operator[](int i); Traditional [] operator. Should print a message if i is out of...
Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for...
Implement a class Polynomial that uses a dynamic array of doubles to store the coefficients for a polynomial. Much of this work can be modelled on the C++ dynamic array of ints List that we discussed in class. This class does not need the method the overloaded += operator. Your class should have the following methods: Write one constructor that takes an integer n for the degree of a term and a double coefficient c for the coefficient of the...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t use vectors), and provide an implementation for the following operations on books in the array 1)isEmpty() returns true if the array is empty, otherwise false 2)isFull() returns true if the array is full, otherwise false 3)listSize() prints the number of books in the array 4)print() prints the content of the array 5)insert(Book) asks the user to enter new book info, and it adds the...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t...
Implement in C++ Design a BookstoreManager class which creates a dynamic array of type Book (don’t use vectors), and provide an implementation for the following operations on books in the array 1)isEmpty() returns true if the array is empty, otherwise false 2)isFull() returns true if the array is full, otherwise false 3)listSize() prints the number of books in the array 4)print() prints the content of the array 5)insert(Book) asks the user to enter new book info, and it adds the...
Overview For this assignment, implement and use the methods for a class called Seller that represents...
Overview For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson. The Seller class Use the following class definition: class Seller { public: Seller(); Seller( const char [], const char[], const char [], double ); void print(); void setFirstName( const char [] ); void setLastName( const char [] ); void setID( const char [] ); void setSalesTotal( double ); double getSalesTotal(); private: char firstName[20]; char lastName[30]; char ID[7]; double salesTotal; };...
Overview For this assignment, implement and use the methods for a class called Seller that represents...
Overview For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson. The Seller class Use the following class definition: class Seller { public: Seller(); Seller( const char [], const char[], const char [], double ); void print(); void setFirstName( const char [] ); void setLastName( const char [] ); void setID( const char [] ); void setSalesTotal( double ); double getSalesTotal(); private: char firstName[20]; char lastName[30]; char ID[7]; double salesTotal; };...
(JAVA) Why do we need a dynamic stack? How do you implement a dynamic stack array?
(JAVA) Why do we need a dynamic stack? How do you implement a dynamic stack array?
Why do we need a dynamic stack and How to implement a dynamic array stack? (...
Why do we need a dynamic stack and How to implement a dynamic array stack? ( Please answer in Java)
IN C++ (THIS IS A REPOST) Design a class, Array, that encapsulates a fixed-size dynamic array...
IN C++ (THIS IS A REPOST) Design a class, Array, that encapsulates a fixed-size dynamic array of signed integers. Write a program that creates an Array container of size 100 and fills it with random numbers in the range [1..999]. (Use std::rand() with std::srand(1).) When building the array, if the random number is evenly divisible by 3 or 5, store it as a negative number. Within your main.cpp source code file, write a function for each of the following processes....
For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson.
For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson.The Seller classUse the following class definition:class Seller { public:   Seller();   Seller( const char [], const char[], const char [], double );        void print();   void setFirstName( const char [] );   void setLastName( const char [] );   void setID( const char [] );   void setSalesTotal( double );   double getSalesTotal(); private:   char firstName[20];   char lastName[30];   char ID[7];   double salesTotal; };Data MembersThe data members for the class are:firstName holds the Seller's first namelastName holds the Seller's last nameID holds the Seller's id numbersalesTotal holds the Seller's sales totalConstructorsThis class has two constructors. The default constructor (the one that takes...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT