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

Use separate files to manage a dynamic array via pointers. Important, please rea

ID: 3593142 • Letter: U

Question

Use separate files to manage a dynamic array via pointers.

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 sizeintegers. 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 firstnewSize 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 nullptrto 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 abovedynamicArray.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

1) dynamicArray.h

#include<iostream>
#include<ctime>
#include<cstdlib>
//function decleartion
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);
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.
void mysort( int* &intPtr, int size, bool (* comp)(int&, int&) );

2) dynamicArray.cpp

#include"dynamicArray.h"

//function defination

//function creates array and set each elements of the array by i*i

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

{

//int *temp;

intPtr = new int(size);

if(intPtr == NULL)

{

cout<<"Array is not allocated"<<endl;

return NULL;

}

else

{

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

{

intPtr[i] = i * i;

}

}

//intPtr = temp;

//delete temp;

return intPtr;

}

//resizing array to newsize

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

{

int *temp;

//if array to be resize is same size or newsize is negative

if((currSize == newSize) || (newSize < 0))

return intPtr;

//creating new array and copying all elements of original array and setting remaining elements with i * i

temp = new int(newSize);

//intPtr = temp;

if(currSize < newSize)

{

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

{

temp[i] = intPtr[i];

}

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

{

temp[i] = i * i;

}

//intPtr = new int(newSize);

}

else //if newsize is less than original size then we have to skip remaining elements of original array

{

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

{

temp[i] = i * i;

}

}

return temp;

}

//deleting array

void array_destructor(int * &intPtr)

{

if(intPtr == NULL)

return;

else

delete intPtr;

}

//generating random number and copying to array

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

{

srand (time(0));

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

{

intPtr[i] = rand();

}

}

//composite function to check x <= y

bool my_less_equal(int& x, int & y)

{

return x <= y ? true:false;

}

//composite function to check x > y

bool my_greater_equal(int& x, int & y )

{

return x >= y ? true:false;

}

//insertion sort to sort an elemnts

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

{

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

{

int key = intPtr[i];

int j = i-1;

/* Move elements of arr[0..i-1], that are

greater than key, to one position ahead

of their current position */

while (j >= 0 && comp(key,intPtr[j]))

{

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

j = j-1;

}

intPtr[j+1] = key;

}

}

3) main.cpp

#include"dynamicArray.h"

int main()

{

int *intPtr=NULL;

int size = 5;

int currSize = 5;

int newSize = 9;

intPtr = array_constructor(intPtr, size );

cout<<"Array created is : "<<endl;

cout<<"========================================================"<<endl;

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

cout<<intPtr[i]<<endl;

int *arr_resize = array_resize(intPtr, currSize, newSize);

cout<<"Array after resize is : "<<endl;

cout<<"========================================================"<<endl;

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

cout<<arr_resize[i]<<endl;

cout<<endl;

cout<<"Array after set is : "<<endl;

cout<<"========================================================"<<endl;

array_set(arr_resize,newSize);

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

cout<<arr_resize[i]<<endl;

cout<<endl;

cout<<"Array after sorting in ascending order is : "<<endl;

cout<<"========================================================"<<endl;

mysort(arr_resize,9,&my_less_equal);

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

cout<<arr_resize[i]<<endl;

cout<<endl;

//array_destructor(intPtr);

return 0;

}

PLEASE REFER BELOW OUTPUT FOR REFERENCE

Array created is :
========================================================
0
1
4
9
16
Array after resize is :
========================================================
0
1
4
9
16
25
36
49
64

Array after set is :
========================================================
366338171
77068316
726011635
474472292
1264331394
870371725
1461421562
445586524
505530588

Array after sorting in ascending order is :
========================================================
77068316
366338171
445586524
474472292
505530588
726011635
870371725
1264331394
1461421562