Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please see the output first.I want the same output as shown below. Write a C++ program that will test five functions described below that use

Please see the output first.I want the same output as shown below.

Write a C++ program that will test five functions described below that use pointers and dynamic memory allocation.

Functions:

You will write the five functions described below. Then you will call them from the main function, to demonstrate their correctness.

  1. 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).

  2. swap: 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.

    1.  int oddSwap (int &x, int &y) { int temp = x; x=y*5; y = temp * 5; return x + y; }
  3. grow_array: 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.

  4. concat_array: 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.

5. 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 three lines: a label indicating which function is being tested, theexpected results (use one string with the values hard coded into it), and the actualresults (use the actual values returned/altered by the function in this line).

testing maximum: Expected maximum: 9 Actual maximum: 9 
testing oddSwap Expected result: 40 a: 25 b: 15 Actual results : 40 a: 25 b: 15 

testing expand: Expected result: 1 2 3 4 5 6 7 8 9 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

Actualresult: 1 2 3 4 5 6 7 8 9 0 -1 -1 -1 -1 -1 -1 -1 - 1 -1 -1

testing concat: Expected result: 1 2 3 4 5 6 7 8 9 0 11 22 33 44 55

Actualresult: 1 2 3 4 5 6 7 8 9 0 11 22 33 44 55

testing subArray: Expected result: 6 7 8 9 Actual result: 6 7 8 9

RULES:

  • DO NOT change the names of the functions!

  • DO NOT do any output from the functions (only from main)!

  • DO NOT do any input at all!

    NOTES:

  • This program DOES need to be done in a Linux or Unix environment.

  • It is your responsibility to fully test your functions. They must work for ANY valid input. The main function you submit must have at least one test case for each function. However, you should test the functions over several different test cases to convince yourself that they work.

  • You do not need to use named constants for your test data (or array sizes) in this assignment, but you DO need to follow the rest of the style guidelines including function definition and comments.

  • Your program should release any dynamically allocated memory when it is finished using it.

  • you may want to include a function that displays the values of an int array on one line, separated by spaces, for displaying test arrays and results.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions