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

1- Objective The purpose of this assignment is for students to gain a better und

ID: 3757146 • Letter: 1

Question

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

You have to write a class “DynamicArray” to store a 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 constructor will be defined as follows:

Constructor : In constructor a valid arraySize (at least 1) should be passed. If any number less than 1 is passed to the constructor, you should set arraySize to 1 by default.

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.

The class would also include following public accessor functions and methods:

Accessors:

Methods:

This function will return a string which stores all elements currently stored in the dynamic array separated by a comma (and no space before or after the numbers). If there are no elements in the array, it prints out “No element” (without double quotes). if there are 1 2 3 in the Dynamic array then it outputs "1,2,3" (without double qoutes).

Important: This method is used by more than one test case to compare output of other methods etc, so unable to implement this method correctly would result in failing more than one test cases.

This option asks the user to input a number to store into the array, then inserts the number in the array.

If the array does not have room to add another element, the array should be expanded to 2 times its current size before adding the element.

Anytime the array size is changed, you should copy the old array elements into the new one and de-allocate the old one to prevent memory leaks.

This option should ask the user for a number to remove from the array. This function should only delete the first occurrence of the to-be deleted number.

If the specified number does not exist inside the array, there should be no change in the array and no output.

Anytime an element is deleted from the beginning or middle of the array, all other elements in the array should be shifted over to fill the empty gap.

Anytime the array size is changed, you should copy the old array element into the new one and deallocate the old one to prevent memory leaks.

If the number of elements is less than or equal to half the size of the array, the array should be shrunk down to half its current size.

Note: In case of any error input, you don't need to output anything. You have either set value to default (where it's asked to do so) or ignore that operation/modification for other cases (where there's no default value to set to).

A destructor should be included to release all memory that has been allocated.

3- Bonus: Mutator for arraySize

In mutator for arraySize, the valid entry should be checked, if any value less than current numOfElement is passed, then arraySize should not be changed. If any value greater than or equal to numOfElement is passed, array should be re-allocated accordingly preserving all the existing elements in it. For example, if array size is 8 and numOfElement is 5, and setArraySize(4) is called it should do nothing. However, if setArraySize(5) or setArraySize(10) is called, it should re-allocate appropriate memory for the array and preserve its content.

4- Submission guideline

You have to write your code in your IDE, save it as DynamicArray.cpp and submit the file in Zybooks. If you encounter an error while submitting, try refreshing your browser to see if your code has been saved in Zybooks (even if it grades a zero for your submission). We won't be able to re-evaluate your code if it's not saved (successfully uploaded) in Zybooks, when you may have a reason to believe that your submission was graded properly. If you figure out that your submission is not saved in Zybooks, please notify your respective TA with a copy of your submission. It doesn't garauntee you a grade, but you would have a proof for submission within deadline.

5- How your code is tested

We will have test benches (small programs), each of which will check a part of your implementation, by creating object(s) of DynamicArray class and then call accessor/methods etc to modify the object and match them with expected changes. For example if you start with a DynamicArray object of size 2 and add three elements we expect the arraySize should be 4. So if we call getArraySize on that object, the output should be 4.

To help you with testing the correctness of your implementation, five sample test benches are provided below. Expected outputs are provided for each test bench which helps you figure out what is testing in each test bench. You can put following code into a main.cpp and run it to test the functionalities of your class. Submission of this main.cpp is not required.

Sample Main Function

Sample output

Explanation / Answer

Answer :

DynamicArray.cpp

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

class DynamicArray
{
int *arr;
int arraySize;
int numOfElements;
public:
DynamicArray(int a=1)
{
if(a<1)
a=1;
arraySize=a;
arr=new int[arraySize];
numOfElements=0;
}

DynamicArray(const DynamicArray &a)
{
arraySize=a.arraySize;
arr=new int[arraySize];
for(int i=0;i<a.numOfElements;i++)
arr[i]=a.arr[i];
numOfElements=a.numOfElements;
}

int getArraySize()
{
return arraySize;
}
int getNumOfElements()
{
return numOfElements;
}
string print()
{
string strArr="No Element";

if(numOfElements>0)
{
strArr="";
for(int i=0;i<numOfElements;i++)
{
stringstream ss;
ss<<arr[i];
string temp=ss.str();
strArr=strArr+temp;
if(i<numOfElements-1)
strArr=strArr+",";
}
}
return strArr;
}

void addElement(int num)
{
if(numOfElements==arraySize)
{
int *arr1=new int[arraySize*2];
int i;
for(i=0;i<arraySize;i++)
arr1[i]=arr[i];
arraySize*=2;
delete arr;
arr=arr1;
}
arr[numOfElements++]=num;
}

void deleteElement(int num)
{
for(int i=0;i<numOfElements;i++)
if(arr[i]==num)
{
for(;i<numOfElements;i++)
arr[i]=arr[i+1];
numOfElements--;
break;
}
if(numOfElements<=(arraySize/2))
{
int *arr1=new int[arraySize/2];
int i;
for(i=0;i<numOfElements;i++)
arr1[i]=arr[i];
arraySize/=2;
delete arr;
arr=arr1;
}
}

~DynamicArray()
{
delete[] arr;
}

void setArraySize(int array_size)
{
if(array_size>=numOfElements)
{
int *arr1=new int[array_size];
for(int i=0;i<numOfElements;i++)
arr1[i]=arr[i];
arraySize=array_size;
delete arr;
arr=arr1;
}
}

};