DO NOT use for loop and while loop
Goal:
Learn how to use recursive functions.
General requirement:
Use cout to fully demonstrate your works for all tasks. Theoutput must clearly show how each of these functions work. If nooutput is displayed for a task below, 0 point will be given to thetask. If the output is not clear, some points may be deducted.
Task 1 – Reverse array (3 pts)
Given an integer array like:
const int SIZE =9;
int arrInt[SIZE]={11,22,33,44,55,66,77,88,99};
Use a recursive function to reverse the values in the array.That is, after calling the function the array should be like:
arrInt[SIZE]={99,88,77,66,55,44,33,22,11};
Task 2 – Array sum: Linear recursive version (3pts)
Use a linear recursive function to add the integersof the array in Task 1 and display the sum.
Task 3 – Array sum: Binary recursive version (4pts)
Use a binary recursive function to add the integersof the array in Task 1 and display the sum.
Task 4 – Linear search: Recursive version (5pts)
Use a linear recursive function to search aninteger in an array and display the index of the target.
Task 5 – Binary search: Recursive version (5pts)
Use a binary recursive function to search aninteger in an array and display the index of the target.
#include <iostream> using namespace std; int linearSearch(int [], int, int, int); int binarySearch(int [], int, int, int); int main() { const int SIZE =9; intarrInt[SIZE]={11,22,33,44,55,66,77,88,99}; /// Enter your testing code for Task 1, 2 and3 here cout << endl << “Task 4” <<endl; int target = 99; int pos = linearSearch(arrInt, 0, SIZE,target); cout << “Linear search: The position ofthe target ” << target << ” is ” << pos <<endl; target = 66; pos = linearSearch(arrInt, 0, SIZE,target); cout << “Linear search: The position ofthe target ” << target << ” is ” << pos <<endl; cout << endl << “Task 5” <<endl; target = 99; pos = binarySearch(arrInt, 0, SIZE – 1,target); cout << “Binary search: The position ofthe target ” << target << ” is ” << pos <<endl; target = 66; pos = binarySearch(arrInt, 0, SIZE – 1,target); cout << “Binary search: The position ofthe target ” << target << ” is ” << pos <<endl; return 0; } int linearSearch(int arrInt[], int pos, int sz, int target) { /// TO DO: ADD YOUR CODE HERE } int binarySearch(int arrInt[], int begIndex, int endIndex, inttarget) { /// TO DO: ADD YOUR CODE HERE } |
Expert Answer
Answer to DO NOT use for loop and while loop Goal: Learn how to use recursive functions. General requirement: Use cout to fully de…