Question
C++ topic .. RECURSION // Please comment as much as you can for my understanding.. Thanks! ..Please Use cout to fully demonstrate your works for
C++ topic .. RECURSION
// Please comment as much as you can for my understanding.. Thanks!
..Please Use cout to fully demonstrate your works for all tasks. The output should clearly show how each of these functions work.
Task 1 Reverse array
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
Use a linear recursive function to add the integers of the array in Task 1 and display the sum.
Task 3 Array sum: Binary recursive version
Use a binary recursive function to add the integers of the array in Task 1 and display the sum.
Task 4 Linear search: Recursive version
Use a linear recursive function to search an integer in an array and display the index of the target.
Task 5 Binary search: Recursive version
Use a binary recursive function to search an integer in an array and display the index of the target.
#include using namespace std;
int linearSearch(int [], int, int, int);
int binarySearch(int [], int, int, int);
int main() { const int SIZE =9; int arrInt[SIZE]={11,22,33,44,55,66,77,88,99};
/// Enter your testing code for Task 1, 2 and 3 here
cout << endl << "Task 4" << endl; int target = 99; int pos = linearSearch(arrInt, 0, SIZE, target); cout << "Linear search: The position of the target " << target << " is " << pos << endl;
target = 66; pos = linearSearch(arrInt, 0, SIZE, target); cout << "Linear search: The position of the target " << target << " is " << pos << endl;
cout << endl << "Task 5" << endl; target = 99; pos = binarySearch(arrInt, 0, SIZE - 1, target); cout << "Binary search: The position of the target " << target << " is " << pos << endl;
target = 66; pos = binarySearch(arrInt, 0, SIZE - 1, target); cout << "Binary search: The position of the 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, int target) { /// TO DO: ADD YOUR CODE HERE } |
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