Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Problem description Write a C++ program that will implement and test the five functions described below that use pointers and dynamic memory allocation. The Functions:
Problem description 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. 1) swap Multiply The following function uses reference parameters. Rewrite the func- tion 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 swapMultiply (int &x, int &y) { int temp = x; X = y; y = temp; return x * y; } 2) swapEvenOdd Takes an array of integers and its size as arguments. It should do swap adjacent elements of the array with the first element that has even index. You MUST use swap Multiply function to do it. Disregard the return value of the swapMultiply function. Do not use square brackets anywhere in the function, not even the parameter list (use pointer notation instead). Example: swapEvenOdd([1 2 3 4 5]) + [21435] 3) append Takes an int array and, array's size and a integer number as arguments. It should create a new array with the size argument array size + 1. The function should copy the contents of the argument array to the new array, and set the last element to the integer number that was passed as an argument. The function should return a pointer to the new array 4) concatenate Takes two int arrays and the arrays' sizes as arguments (that's 4 argu- ments). 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. 5) sub Array 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]. You must define subArray as follows: Add the code for the du- plicateArray function from the lecture slides for Unit 3 (slide 24) to your program. Add the code for the subArray function given below to your program. Fill in the blanks with expressions so that the function subArray behaves as described above. int *subArray (int *array, int start, int length) { int *result = duplicateArray (--- return result; } DO NOT alter duplicateArray, DO NOT alter subArray as defined above. Program execution 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 func- tion 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 should 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 swapMultiply test data: a: 8 6:5 Expected result: 40 a: 5 b: 8 Actual results : 40 a: 5 b: 8 Testing swapEvenOdd: test data array 1: 1 2 3 4 5 6 7 8 Expected result: 2 1 4 3 6 5 8 7 Actual result: 2 1 4 3 6 5 8 7 Testing append: test data: 1 2 3 4 5 6 7 8 9 0 | 17 Expected result: 1 2 3 4 5 6 7 8 9 0 17 Actual result: 1 2 3 4 5 6 7 8 9 0 17 Testing concatenate: test data: 1 2 3 4 5 6 7 8 9 0 and 11 22 33 44 55 Expected result: 1 2 3 4 5 6 7 8 9 0 11 22 33 44 55 Actual result: 1 2 3 4 5 6 7 8 9 0 11 22 33 44 55 Testing subArray: test data: 1 2 3 4 5 6 7 8 9 0 start: 5 length: 4 Expected result: 6 7 8 9 Actual result: 6 7 8 9 Additional requirements This program must run in a Linux or Unix environment, using a command line compiler like g++. It is your responsibility to fully test your functions. They must work for ANY valid input. The main function must have at least one test case for each function. For sort2withSum, compute the value of the function call BE- FORE you output it: int z = sort2withSum(.......); cout
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started