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

Note: the program should be written in C++ Important, please read! Make sure you

ID: 3594052 • Letter: N

Question

Note: the program should be written in C++

Important, please read!

Make sure you use the specified function prototypes, as they will be referred to as such in the unit tests.

Please feel free to introduce additional subroutines (i.e., functions) to help you implement the required functions, although they will not be tested.

zyBook's compilation environment is not the same as your IDE! zyBook's C++ compilation environment seems to be "bare-bone". It's much closer to programming without help from a sophisticated IDE such as NetBeans or Visual Studio. I would recommend you to test your programs using a less-embellished IDE such as Dev C++ or the online site cpp.sh as recommended by Scott on iLearn.

Submit three files as described below. Please name your files as specified to avoid unnecessary complications (even though I don't think it actually matters.) You can submit your solution for grading up to 25 times.

Your attitude matters! Believe or not, if you hold the belief that your codes are perfect and they should pass the unit tests on zyBook, this will effectively set up a mental barrier that prevents you from fixing the issues faster!

The main objectives of this lab include:

Use pointers to manage a dynamic array of integers, including

memory allocation & value initialization

resizing

changing and reordering the contents of an array

memory deallocation

Learn the difference between passing a pointer to a function by value vs. by reference

Learn to use pointers to functions to make a function more powerful and template-like.

Learn to include multiple files in a C++ project by distributing the source codes accordingly to different files.

1. Your project will include the following three files:

A header file:  dynamicArray.h that includes a list of function prototypes as enumerated in the next section.

An implementation file: dynamicArray.cpp that implements the functions declared in the header file.

A test driver file: dynamicArray-main.cpp that includes the main() function so that you can test all the functions you've implemented above.

2. The header file dynamicArray.h will include the following list of functions:

constructing a dynamic array of the specified size and initializing the i-th array element to i*i


Specifically, the above function allocates space to intPtr and uses it to manage a dynamic array of size integers. Use proper exception handling to make sure that the allocation is successful. Then initialize the value of the i-th element to i*i. The function returns a pointer pointing to the new array. To avoid memory leakage, you should check whether intPtr already has a valid pointee when being passed into this function. If the answer is yes, you would want to first deallocate the space occupied by its pointee.

As an example, after invoking this function array_constructor( myArray, size=5); in a different function, the content of myArray will be

resizing a dynamic array pointed to by intPtr, where the new size can be smaller or larger than the array's current size

You will need to first make sure both curSize and newSize have valid values, i.e., positive integers. Then consider the following three scenarios:

currSize==newSize or newSize<0: do nothing

currSize>newSize: the array's size is reduced to newSize. Furthermore, its content is reduced to its first newSize elements.

currSize<newSize: the array's size is increased to newSize. The content of the array will be expanded by inserting at the end as many elements as needed to reach newSize. Furthermore, initialize each of the new elements to i*i, where i is the index of the element.

As an example, after invoking this function array_resize( myArray, currSize, newSize); (where currSize=5, newSize=9), the content of myArray will be changed to

Later, another invocation array_resize( myArray, currSize, newSize);(where currSize=9, newSize=2) will change the content of myArray to:

deallocating the memory space occupied by the dynamic array intPtr. Please make sure you check whether this array actually exists. After you finish deallocating the array space, make sure to assign nullptr to the pointer.

Randomizing the content of the dynamic array intPtr by calling the srand() and rand() functions (see zyBook section 2.19).

Specifically, after having set a seed value using the srand( time(0) ) function. Then invoke the rand() to assign each element in the array a random value.

As an example, after invoking array_set( myArray, currSize); (where currSize=9) in a different function, myArray will look like (yours will be different, of course):

Passing a pointer to functions to a sorting function mysort() so that this function can either sort an array in ascending or descending order. Please modify the insertionSort() you implemented in the last coding lab to sort an integer array. (Please feel free to use the implementation in the sample solution posted on iLearn if yours didn't pass the test.)

To do this, please include the following two boolean functions for integer comparison in your header file:

Now, if one calls mysort( myArray, size, my_less_equal); in another function, the content in myArray will be sorted in ascending order; calling mysort( myArray, size, my_greater_equal); will sort myArray in descending order.

3. The implementation file dynamicArray.cpp will implement all the functions declared in the above dynamicArray.h header file.

4. A test driver file: dynamicArray-main.cpp that includes the main() function so that you can test all the functions you've declared and implemented in the above two files.

Explanation / Answer

//dynamicArray.h

#include<iostream>
using namespace std;

int * array_constructor(int * &intPtr, int &size);
int * array_resize(int * &intPtr, int& currSize, int& newSize);
void array_destructor(int * &intPtr);
void array_set(int* &intPtr, int &size);
void mysort(int* &intPtr, int size, bool(*comp)(int&, int&));
bool my_less_equal(int& x, int & y); //return true if x<=y, false otherwise.
bool my_greater_equal(int& x, int & y); //return true if x>=y, false otherwise.

-----------------------------------------------------------------------------------------------------------------------------

//dynamicArray.cpp

#include"dynamicArray.h"

#include<exception>

#include<ctime>

int * array_constructor(int * &intPtr, int &size)

{

//destroy the array if it's not NULL

//array_destructor(intPtr);

try

{

if (size < 0)

throw "size cannot be negetive" ;

intPtr = new int[size];

}

catch (exception e)

{

cout << e.what() << endl;

}

//return allocated momory to calling function

return intPtr;

}

int * array_resize(int * &intPtr, int& currSize, int& newSize)

{

int *newArr;

newArr = new int[newSize];

if (currSize <= newSize)

{

for (int i = 0; i < currSize; i++)

{

newArr[i] = intPtr[i];

}

}

else

{

for (int i = 0; i < newSize; i++)

{

newArr[i] = intPtr[i];

}

}

//now distroy [revious array intPtr

array_destructor(intPtr);

//return new array with resize

return newArr;

}

void array_destructor(int * &intPtr)

{

delete[]intPtr;

intPtr = NULL;

}

void array_set(int* &intPtr, int &size)

{

srand(time(0));

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

{

//assign random numbers to array

intPtr[i] = rand();

}

}

void mysort(int* &intPtr, int size, bool(*comp)(int &x, int&y))

{

int tmp;

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

{

for (int j = 0; j < size-i - 1; j++)

{

if ((*comp) == my_less_equal )

{

if (intPtr[j] > intPtr[j + 1])

{

tmp = intPtr[j];

intPtr[j] = intPtr[j + 1];

intPtr[j + 1] = tmp;

}

}

if ((*comp) == my_greater_equal)

{

if (intPtr[j] < intPtr[j + 1])

{

tmp = intPtr[j];

intPtr[j] = intPtr[j + 1];

intPtr[j + 1] = tmp;

}

}

}

}

}

bool my_less_equal(int& x, int & y) //return true if x<=y, false otherwise.

{

if (x <= y)

return true;

else

return false;

}

bool my_greater_equal(int& x, int & y) //return true if x>=y, false otherwise.

{

if (x >= y)

return true;

else

return false;

}

----------------------------------------------------------------------------------------

//main.cpp

#include"dynamicArray.h"

int main()

{

//call array construct

int *myArray = NULL, *ptr1, *ptr2;

int size = 8;

myArray = array_constructor(myArray, size);

//set

array_set(myArray, size);

//print arrayy

cout << "Print dynamic array myArray : ";

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

{

cout << myArray[i] << " ";

}

cout << endl;

//sort array in ascending

mysort(myArray, size, my_less_equal);

cout << " Print myArray after sorting in ascending order" << endl;

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

{

cout << myArray[i] << " ";

}

cout << endl;

//sort array in descnding

mysort(myArray, size, my_greater_equal);

cout << " Print myArray after sorting in descending order" << endl;

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

{

cout << myArray[i] << " ";

}

cout << endl;

//array_destructor(ptr);

int size1 = 2;

ptr1 = array_resize(myArray, size, size1);

if (size1 > size)

{

ptr2 = ptr1 + size;

array_set(ptr2, size1);

}

//print arrayy

cout << " Print dynamic array ponited by ptr1 after resizing to " << size1 << " : ";

for (int i = 0; i < size1; i++)

{

cout << ptr1[i] << " ";

}

cout << " Distroying pointer to array....";

array_destructor(ptr1);

}

----------------------------------------------------------------------------------------------------------------------------------------------

//output1

Print dynamic array myArray : 4469 8731 24792 20100 21504 13180 29418 20973

Print myArray after sorting in ascending order
4469 8731 13180 20100 20973 21504 24792 29418

Print myArray after sorting in descending order
29418 24792 21504 20973 20100 13180 8731 4469

Print dynamic array ponited by ptr1 after resizing to 2 : 29418 24792
Distroying pointer to array....

//output2

Print dynamic array myArray : 5256 10425 4682 19423 16949 9250 25855 3628

Print myArray after sorting in ascending order
3628 4682 5256 9250 10425 16949 19423 25855

Print myArray after sorting in descending order
25855 19423 16949 10425 9250 5256 4682 3628

Print dynamic array ponited by ptr1 after resizing to 5 : 25855 19423 16949 1042
5 9250
Distroying pointer to array....

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote