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

zybook c++ please help me in the easiest way to understand. Books My library >CO

ID: 3755467 • Letter: Z

Question

zybook c++
please help me in the easiest way to understand.

Books My library >COSC 1430 home > 7.25: Homework 4 zyBooks catalog Help/FA0 Toan To 1- Objective The purpose of this assignment is for students to gain a better understanding of object-oriented programming, classes, mutators accessors, method, destructors, and dynamic allocation. 2- Problenm You have to write a class "DynamicArray" to store an dynamic array of integers and allow manipulations on that array It would have following private attributes: int *arr A pointer that points to the array int arraySize: An integer that stores the size of the array. It should be at least size one int numOfElements: An integer that tracks how many elements are in the array The constructor and copy constrcutor will be defined ds follows: . Constructor: In constructor a valid arraySize (at least 1) should be passed. If any number less than 1 is passed to the constructor or copy constructor, then you should set arraySize to 1 by default. DynamicArray (int arraySize) Copy Constructor: In copy Conctructor, you need to implement Deep Copy, in a way that all elements of one DynamicArray object is copied to the second object DynamicArray( const DynamicArray &a) The class would also include following public accessor functions and methods: . Accessors:

Explanation / Answer

Please find the code below.

CODE

===============

#include <iostream>

#include <string>

using namespace std;

class DynamicArray {

public:

int *arr;

int arraySize;

int numOfElements;

DynamicArray(int size) {

if (size < 1) {

arraySize = 1;

} else {

arraySize = size;

}

arr = new int[arraySize];

numOfElements = 0;

}

DynamicArray(const DynamicArray &a) {

arraySize = a.arraySize;

numOfElements = a.numOfElements;

arr = new int[arraySize];

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

arr[i] = a.arr[i];

}

}

int getArraySize() {

return arraySize;

}

int getNumOfElements() {

return numOfElements;

}

string print() {

if (numOfElements == 0) {

return "No element";

}

string res = "";

for(int i=0; i<numOfElements - 1; i++) {

res += to_string(arr[i]) + ", ";

}

res += to_string(arr[numOfElements - 1]);

return res;

}

void addElement(int num) {

if (numOfElements >= arraySize) {

int *temp = new int[2*arraySize];

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

temp[i] = arr[i];

}

arraySize = 2*arraySize;

arr = new int[arraySize];

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

arr[i] = temp[i];

}

delete[] temp;

}

arr[numOfElements ++] = num;

}

void deleteElement(int num) {

int found = 0;

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

     if(arr[i] == num) {

       for(int j=i; j<(arraySize-1); j++) {

         arr[j]=arr[j+1];

       }

found = 1;

}

if(found == 1) {

numOfElements --;

break;

}

if (numOfElements <= arraySize/2) {

int *temp = new int[arraySize/2];

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

temp[i] = arr[i];

}

arraySize = arraySize/2;

arr = new int[arraySize];

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

arr[i] = temp[i];

}

delete[] temp;

}

   }

}

~DynamicArray() {

delete[] arr;

}

};

NOTE: Due to lack of time, I could not implement the bonus method. Please post the bonus method separately and I will try to answer it. And, if any of the test methods in main() fails, please reach out to me.