The main objectives of this lab include: Use pointers to manage a dynamic array
ID: 3594144 • Letter: T
Question
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.
------------------------------------------------------
#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 = i; 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 *ptr = NULL,*ptr1,*ptr2;
int size = 5;
ptr = array_constructor(ptr, size);
array_set(ptr, size);
//print arrayy
cout << "Print dynamic array created pointed by ptr: ";
for (int i = 0; i < size; i++)
{
cout << ptr[i] << " ";
}
cout << endl;
//array_destructor(ptr);
int size1 = 2;
ptr1 = array_resize(ptr, 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);
cout << endl;
}
------------------------------------------------------
//output
Print dynamic array created pointed by ptr: 21917 17995 27665 26687 27752
Print dynamic array ponited by ptr1 after resizing to 2 : 21917 17995
Distroying pointer to array
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.