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. findLast: takes an array of integers, its size and a target integer value. Returns an index of the last occurrence of the target element in the array. If the target value doesn't exist, returns -1. For this function only: Do not use square brackets anywhere in the function, not even the parameter list (use pointer notation instead). nextFibonacci. Modify the following function to use pointers as parameters (instead of reference parameters). The Fibonacci sequence is a series of numbers where each number is the addition of the previous two numbers, starting with 0, and 1 (specifically: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55…). This function updates the given i-th and (i+1)-st Fibonacci numbers to (i+1)-st and (i+2)-nd. For example, if fibCurr is 13 and fibNext is 21, it puts 21 in fibCurr and 34 in fibNext. Don't modify the function except to make it use pointers instead of reference parameters. void nextFibonacci(int& fibCurr, int& fibNext){ int fibPrev = fibNext - fibCurr; fibNext += fibCurr; fibCurr += fibPrev; } repeat: takes an array of integers, its size and a number N. Creates a new array that is N times bigger than an input array and fills the new array with values of an input array cyclically repeated N times (see Sample Execution). Returns the new array. swapEvenOdd: takes an array of integers and its size. Creates and returns a new array that is the same as an original array, but with each element with an even index swapped with a subsequent element with an odd index (if it exists). 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 subArray function given below to your program. Fill in the blanks with expressions so that the function subArray behaves as described above. Note that the duplicateArray function (from the Unit 3 lecture slides) has already been added to the provided template main.cpp file. DO NOT alter duplicateArray, DO NOT alter subArray as defined below. int *subArray (int *array, int start, int length) { int *result = duplicateArray(__________, ___________); return result; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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. findLast: takes an array of integers, its size and a target integer value. Returns an index of the last occurrence of the target element in the array. If the target value doesn't exist, returns -1. For this function only: Do not use square brackets anywhere in the function, not even the parameter list (use pointer notation instead).

  2. nextFibonacci. Modify the following function to use pointers as parameters (instead of reference parameters). The Fibonacci sequence is a series of numbers where each number is the addition of the previous two numbers, starting with 0, and 1 (specifically: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55…). This function updates the given i-th and (i+1)-st Fibonacci numbers to (i+1)-st and (i+2)-nd. For example, if fibCurr is 13 and fibNext is 21, it puts 21 in fibCurr and 34 in fibNext. Don't modify the function except to make it use pointers instead of reference parameters.

void nextFibonacci(int& fibCurr, int& fibNext){ int fibPrev = fibNext - fibCurr; fibNext += fibCurr; fibCurr += fibPrev; }

  1. repeat: takes an array of integers, its size and a number N. Creates a new array that is N times bigger than an input array and fills the new array with values of an input array cyclically repeated N times (see Sample Execution). Returns the new array.

  2. swapEvenOdd: takes an array of integers and its size. Creates and returns a new array that is the same as an original array, but with each element with an even index swapped with a subsequent element with an odd index (if it exists).

  3. 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 subArray function given below to your program. Fill in the blanks with expressions so that the function subArray behaves as described above. Note that the duplicateArray function (from the Unit 3 lecture slides) has already been added to the provided template main.cpp file. DO NOT alter duplicateArray, DO NOT alter subArray as defined below.

int *subArray (int *array, int start, int length) { int *result = duplicateArray(__________, ___________); return result; }

Main function

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

Rules

  • DO NOT change the names of the functions or the order of parameters!
  • DO NOT do any output from the functions (only from main)!
  • DO NOT do any input at all!

Notes

  • Use the template in the provided main.cpp file and sample output below as guides to test your functions. Remember, your output should match exactly, so you may need to play with spacing.
  • Your program should release any dynamically allocated memory when it is finished using it.

Output

Testing findLast: test data: 1 2 3 2 1 2 searching for value 1 result: 4 expected: 4 Testing nextFibonacci: test data: 55 89 result: 89 144 expected: 89 144 Testing repeat: test data: 4 5 1 N = 4 result: 4 5 1 4 5 1 4 5 1 4 5 1 expected: 4 5 1 4 5 1 4 5 1 4 5 1 Testing swapEvenOdd: test data: 1 2 7 8 23 20 3 result: 2 1 8 7 20 23 3 expected: 2 1 8 7 20 23 3 Testing subArray: test data: 18 19 15 16 12 11 13 14 start and length: 3 4 result: 16 12 11 13 expected: 16 12 11 13

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Functions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education