In: Computer Science
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.
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]