Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

 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:

 int getArraySize() int getNumOfElements() 

Methods:

 string print() 

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.

 void addElement(int num) 

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.

 void deleteElement(int num) 

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.

 ~DynamicArray() 

3- Bonus: Mutator for arraySize

 void setArraySize(int array_size) 

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

#include "DynamicArray.cpp" int main(){ //*********** addElements Function is Tested *********** DynamicArray Darr(1); Darr.addElement(1); Darr.addElement(2); Darr.addElement(3); Darr.addElement(4); Darr.addElement(5); if (Darr.getArraySize() == 8 ) { cout << "Correct Result"<< endl; cout << "array Size after expansion is : 8" << endl<< endl; } else { cout << "Your addElement Function is not correct" << endl; cout << "Your arraySize after Expansion is : " << Darr.getArraySize() << endl; cout << "Expected Output : 8 " <

Sample output

Correct Result array Size after expansion is : 8 Correct Result array Size after given wrong value (less than one) : 1 Correct Result array Size is after given a wrong value (less than actual numOfElements) : 3 Correct Result Elements of array :1,2,3 Correct Result First array has 10 Copied array after modification has 10,100,1000 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Automating Access Databases With Macros

Authors: Fish Davis

1st Edition

1797816349, 978-1797816340

More Books

Students also viewed these Databases questions

Question

How do Dimensional Database Models differ from Relational Models?

Answered: 1 week ago

Question

What type of processing do Relational Databases support?

Answered: 1 week ago

Question

Describe several aggregation operators.

Answered: 1 week ago