Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem: Write a C++ program that will implement and test the four functions described below that use pointers and dynamic memory allocation. The Functions: You

Problem: Write a C++ program that will implement and test the four functions described below that use pointers and dynamic memory allocation.

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

1. isSorted: takes an array of integers and its size as arguments. It should return true if the elements of the array are already in ascending order, and false if they are not. Do not use square brackets anywhere in the function, not even the parameter list (use pointers instead).

2. chain: The following function uses reference parameters. Rewrite the function so it uses pointers instead of reference parameters. When you test this function from the main program, demonstrate that it sets the values of the variables passed into it

double chain (int totalInches, int &feet, int &inches) {

feet = totalInches/12;

inches = totalInches%12;

return feet*3.49 + inches*.30;

}

3. grow: takes an array of integers 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 so that each element from the first array occurs twice consecutively in the second array. For example, grow(x,3) where x is {4,5,6} should return {4,4,5,5,6,6}. The function should return a pointer to the new array.

4. subArray: 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 duplicateArray function from the lecture slides for chapter 9 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.

Output:

Test these four 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 the following: 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 result, use the actual values returned/altered by the function.

testing isSorted:

test data array 1: 1 2 3 4 5 6 7 8

Expected result: true

Actual result: true

test data array 2: 8 7 6 5 4 3 2 1

Expected result: false

Actual result: false

test data array 3: 1 2 3 5 4 6 7 8

Expected result: false

Actual result: false

testing chain for 53 inches:

Expected result: 15.46 feet: 4 inches: 5

Actual results : 15.46 feet: 4 inches: 5 3

testing grow:

test data: 1 2 3 4 5 6 7 8 9

Expected result: 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9

Actual result: 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9

testing subArray:

test data: 1 2 3 4 5 6 7 8 9

start: 5 length: 4

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 get any input from the user!! Use constants for test values!!

NOTES:

This program must be done in a Linux or Unix environment, using a command line compiler like g++. Do not use codeblocks, eclipse, or Xcode to compile.

Your program must compile and run, otherwise you will receive a score of 0.

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 chain, compute the value of the call to chain BEFORE you output it:

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 comments.

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

. I recommend using 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 to Expert-Tailored Solutions

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

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Database Fundamentals Study Guide

Authors: Dr. Sergio Pisano

1st Edition

B09K1WW84J, 979-8985115307

More Books

Students also viewed these Databases questions