Write a C++ program that will implement and test the five functions described below that use pointers and dynamic memory allocation.
The Functions:
You will write the five functions described below. Then you will call them from the main function, to demonstrate their correctness.
maximum: takes an int array and the array's size as arguments. It should return the maximum value of the array elements. Do not use square brackets ANYWHERE in the function (use pointers instead). Extra challenge: Do not use the loop variable in the body of the loop.
oddSwap: The following function uses reference parameters. Rewrite the function so it uses pointers instead of reference variables. When you test this function from the main program, demonstrate that it changes the values of the variables passed into it.
int oddSwap (int &x, int &y) { int temp = x; x = y * 5; y = temp * 5; return x + y; }
resize: takes an int array and the array's size as arguments. It should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the new array with -1. The function should return a pointer to the new array (Note: you could use a modified version of this function to make your inventory array in PA#2 bigger when it gets to 100 elements).
concatenate: takes two int arrays and the arrays' sizes as arguments (that's 4 arguments). It should create a new array big enough to store both arrays. Then it should copy the contents of the first array to the new array, and then copy the contents of the second array to the new array in the remaining elements, and return a pointer to the new array.
subArray: Define subArray as follows:
int *subArray (int *array, int start, int length) { return duplicateArray(__________, ___________); }
Add the code for duplicateArray from the lecture slides for chapter 9 (slide 24). Fill in the blanks with expressions so that the function subArray behaves as follows: It takes an int array, a start index and a length as arguments. It creates a new array that is a copy of the elements from the original array starting at the start index, and has length equal to the length argument. For example, subArray(aa,5,4) would return a new array containing only the elements aa[5], aa[6], aa[7], and aa[8].
DO NOT alter duplicateArray, DO NOT alter subArray as defined above.
Output:
Test these five functions using the main function as a driver. The driver should pass (constant) test data as arguments to the functions. Select appropriate test data for each function and then call that function using the test data. For each function, you should output four lines: a label indicating which function is being tested, the test data, the expected results, and the actual results. For the test data and Expected result, you may hard code the output values (use string literals containing the numeric values), for the Actual results, use the actual values returned/altered by the function.
testing maximum: test data: 1 2 3 4 5 6 7 -8 9 0 Expected maximum: 9 Actual maximum: 9
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference
and should not be submitted as is. We are not held liable for any misuse of the solutions.
Please see the frequently asked questions page
for further questions and inquiries.
Kindly complete the form.
Please provide a valid email address and we will get back to you within 24 hours.
Payment is through PayPal, Buy me a Coffee
or Cryptocurrency.
We are a nonprofit organization however we need funds to keep this organization operating
and to be able to complete our research and development projects.