Question

In: Computer Science

In C++ and pls comment every line so I UNDERSTAND Implement a class named DynamicArray that...

In C++ and pls comment every line so I UNDERSTAND

Implement a class named DynamicArray that has the following members:
A pointer to hold a dynamically allocated array, of type int.
A member variable to hold the size of the array.
A default constructor, which will allocate an array of size 10
A parameterized constructor, which takes a size and use the size to allocate array.
A copy constructor, which performs deep copy.
A copy assignment operator, which performs deep copy and supports self-assignment of the form x = x. See reference: https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three
A destructor that recycles allocated memory
A member function that fills array with random numbers.
A member function that prints all elements from the array.
A member function that performs insertion sort.

Solutions

Expert Solution

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 or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#include<iostream>

#include<cstdlib>

#include<ctime>

using namespace std;

//required class. for the ease of submission, I have written the definition for

//every methods within the class itself.

class DynamicArray{

                private:

                //array pointer

                int* array;

                //current size

                int size;

                //current maximum capacity

                int max_capacity;

               

                //private method to resize the array when it is full.

                void resize(unsigned int newSize){

                                max_capacity=newSize;

                                //creating array with new size, copying all elements

                                int* newArray=new int[max_capacity];

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

                                               newArray[i]=array[i];

                                }

                                //assigning new array to original array pointer

                                array=newArray;

                }

               

                public:

                //default constructor

                DynamicArray(){

                                //initializing array pointer with default capacity (10)

                                array=new int[10];

                                size=0;

                                max_capacity=10;

                }

               

                //constructor taking initial size

                DynamicArray(int capacity){

                                //initializing array pointer with given capacity

                                array=new int[capacity];

                                size=0;

                                max_capacity=capacity;

                }

               

                //copy constructor

                DynamicArray(const DynamicArray &dyn){

                                //initializing array pointer with capacity of dyn

                                array=new int[dyn.max_capacity];

                                size=dyn.size;

                                max_capacity=dyn.max_capacity;

                                //copying elements of dyn to this array

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

                                               array[i]=dyn.array[i];

                                }

                }

                //destructor

                ~DynamicArray(){

                                //de allocating the memory

                                delete[] array;

                }

                //returns the current size

                int getSize() const{

                                return size;

                }

               

                //method to add an element to the array

                void add(int &data){

                                //ensuring capacity before adding

                                if(size==max_capacity){

                                               //resizing the array with twice the capacity

                                               resize(max_capacity*2);

                                }

                                //adding to the index size and updating size

                                array[size]=data;

                                size++;

                }

               

                //assignment operator to do a copy of current array

                DynamicArray & operator = (const DynamicArray &other){

                                //copying all attributes

                                array=new int[other.size];

                                size=other.size;

                                max_capacity=other.max_capacity;

                                //copying each element one by one

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

                                               array[i]=other.array[i];

                                }

                                //returning a reference to this object

                                return *this;

                }

               

                //method to fill the array with random numbers

                void fillArray(){

                                size=0;

                                //looping and filling all elements upto current capacity

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

                                               //generating and assigning a value between 0 and 999

                                               array[i]=rand()%1000;

                                               size++;

                                }

                }

               

                //method to print the array

                void printArray(){

                                cout<<"[";

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

                                               cout<<array[i];

                                               //if this is not last element, printing a comma and space

                                               if(i!=size-1){

                                                               cout<<", ";

                                               }

                                }

                                cout<<"]"<<endl;

                }

               

                //method to sort the elements of the array

                void sort(){

                                //using insertion sort algorithm to sort numbers

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

                                               int temp = array[i];

                                               int j = i;

                                               while ((j > 0) && (array[j - 1] > array[j])) {

                                                               temp = array[j - 1];

                                                               array[j - 1] = array[j];

                                                               array[j] = temp;

                                                               j--;

                                               }

                                }

                }

               

};

int main(){

                //creating a dynamic array of initial capacity 15

                DynamicArray arr(15);

                //filling with random numbers

                arr.fillArray();

                //creating another object as the deep copy of above

                DynamicArray arr2(arr);

                //displaying both

                cout<<"Array 1: ";

                arr.printArray();

                cout<<"Array 2: ";

                arr2.printArray();

                //sorting first array

                arr.sort();

                //displaying both

                cout<<"Array 1 after sorting: ";

                arr.printArray();

                cout<<"Array 2 unsorted: ";

                arr2.printArray(); //this should be unchanged.

                return 0;

}

/*OUTPUT*/

Array 1: [41, 467, 334, 500, 169, 724, 478, 358, 962, 464, 705, 145, 281, 827, 961]

Array 2: [41, 467, 334, 500, 169, 724, 478, 358, 962, 464, 705, 145, 281, 827, 961]

Array 1 after sorting: [41, 145, 169, 281, 334, 358, 464, 467, 478, 500, 705, 724, 827, 961, 962]

Array 2 unsorted: [41, 467, 334, 500, 169, 724, 478, 358, 962, 464, 705, 145, 281, 827, 961]


Related Solutions

in JAVA: implement a class called tree (from scratch) please be simple so i can understand...
in JAVA: implement a class called tree (from scratch) please be simple so i can understand thanks! tree(root) node(value, leftchild,rightchild) method: insert(value)
Hi, I want to implement the following methods with a driver class In the comment block...
Hi, I want to implement the following methods with a driver class In the comment block for add, give the best possible big-O of the worst-case running time for executing a single add operations and give the best possible big-O of the total worst-case running time of executing a sequence of N add operations. here is the Implement class: import java.util.Iterator; // Do not modify the given code. @SuppressWarnings("unchecked") // Given public class MyArrayList { private T[] data; // Given...
Can you please write this in python and comment as well so I can understand what...
Can you please write this in python and comment as well so I can understand what yo are doing. Thank you. 1)Loop through list A, at each iteration, show the square root result of that element. Add proper text to your print function. A = [-4, 1, -16, 36, -49, 64, -128] 2)Create a counter variable, name it i and initialize it to 0. Using a for loop, count how many numbers are divisible by 3 in range of 1...
1.Implement the generic PriorityQueueInterface in a class named PriorityQueue. Note: it must be named PriorityQueue The...
1.Implement the generic PriorityQueueInterface in a class named PriorityQueue. Note: it must be named PriorityQueue The priority queue MUST be implemented using a linked list. 2 test program checks that a newly constructed priority queue is empty o checks that a queue with one item in it is not empty o checks that items are correctly entered that would go at the front of the queue o checks that items are correctly entered that would go at the end of...
#data structure 1.Implement the generic PriorityQueueInterface in a class named PriorityQueue. Note: it must be named...
#data structure 1.Implement the generic PriorityQueueInterface in a class named PriorityQueue. Note: it must be named PriorityQueue The priority queue MUST be implemented using a linked list. 2 test program checks that a newly constructed priority queue is empty o checks that a queue with one item in it is not empty o checks that items are correctly entered that would go at the front of the queue o checks that items are correctly entered that would go at the...
C# programming. Comment/Explain the below code line by line. I am having a hard time following...
C# programming. Comment/Explain the below code line by line. I am having a hard time following it. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Nth_prime {     class Program     {         public static bool isPrime(int number)         {             int counter = 0;             for (int j = 2; j < number; j++)             {                 if (number % j == 0)                 {                     counter = 1;                     break;                 }             }             if (counter == 0)             {                 return true;             }             else             {                 return false;             }         }...
“A complete and consistent class diagram is all that is required to implement and to understand...
“A complete and consistent class diagram is all that is required to implement and to understand an application”. Discuss the correctness or otherwise of this statement. In your answer, among other things, consider how class diagrams evolve and who has a stake in the understanding and implementation of an application.
“A complete and consistent class diagram is all that is required to implement and to understand...
“A complete and consistent class diagram is all that is required to implement and to understand an application”. Discuss the correctness or otherwise of this statement. In your answer, among other things, consider how class diagrams evolve and who has a stake in the understanding and implementation of an application.
“A complete and consistent class diagram is all that is required to implement and to understand...
“A complete and consistent class diagram is all that is required to implement and to understand an application”. Discuss the correctness or otherwise of this statement. In your answer, among other things, consider how class diagrams evolve and who has a stake in the understanding and implementation of an application.
Please in C++ I don't have the binarySearchTree (Carrano) Implement the code of the BinarySeachTree Class...
Please in C++ I don't have the binarySearchTree (Carrano) Implement the code of the BinarySeachTree Class to solve: (Gaddis) Programming Challenger 3. Car Class p. 802 Ch. 13 • Implement a menu of options • Add a motor vehicle to the tree • Remove a motor vehicle to the tree • Search by vehicle model • Vehicle Inventory Updates • Take one of the tours to verify the contents of the tree. Car Class Write a class named Car that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT