What is the purpose of the following recursive program? In other words, what ope
ID: 3829154 • Letter: W
Question
What is the purpose of the following recursive program? In other words, what operation does this program perform? Provide at least 3 examples of the program’s output based on specific input (test cases), including a boundary case, and label them. You DO NOT need to provide an extreme case
example.
#include <iostream> using namespace std;
void do_stuff(int a[], int b);
int do_recursive_stuff(int c[], int d);
int main (){
const int Q = 10;
}
int x[Q];
do_stuff(x, Q); do_recursive_stuff(x,Q);
cout << "Result is: " << endl; for (int i = 0; i < Q; i++)
cout<<”Element “<<i<<” = “<<x[i]<<endl; return 0 ;
void do_stuff(int a[], int b){ int x = 0;
do{
do{
cout<<”Please enter some positive integer: “; cin>> a[x];
cout<<endl;
if (a[x]<=0)
cout<<”You must enter some positive integer, > 0!!”<<endl;
} while(a[x] <= 0);
x++;
} while (x < b);
}
void do_recursive_stuff(int c[], int d){ int max = 0;
int temp = 0;
int i = 0;
if (d==1)
return; //voids may have a return statement that simply returns control (no value) else{
for (i = 0; i < d; i++){ if (c[i] > c[max]){
max = i;
} }
temp = c[d-1];
c[d-1] = c[max];
c[max] = temp; do_recursive_stuff(c, d-1);
} }
Explanation / Answer
Please find my explanation.
I have traced one test case.Like that you can trace any array.
void do_recursive_stuff(int c[], int d){
int max = 0;
int temp = 0;
int i = 0;
if (d==1)
return; //voids may have a return statement that simply returns control (no value) else{
for (i = 0; i < d; i++){ if (c[i] > c[max]){
max = i;
}
temp = c[d-1];
c[d-1] = c[max];
c[max] = temp;
do_recursive_stuff(c, d-1);
}
This function recursively putting max elements from range 0 to d at last and calling recursive call with 0 to d-1 ranges
So, finally array will be sorted in increasing order.
Example
A = 1 4 5 3
d = 4
do_recursive_stuff(A, 4)
A = 1 4 3 5
=>do_recursive_stuff(A, 3)
A = 1 3 4 5
=> do_recursive_stuff(A, 2)
A = 1 3 4 5
=> => do_recursive_stuff(A, 1)
return
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.