Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++ Project Requirements Create two recursive functions, hilo and rsort. Whil

ID: 3912769 • Letter: I

Question

In C++

Project Requirements Create two recursive functions, hilo and rsort. While these functions could easily be implemented iteratively, this project will assist you in mastering simple recursion. You can add your code for these functions to the file main.cpp below. hilo (int n) This function plays the high/low, binary search guessing game covered in this chapter. It calls GuessNumber with two changes: 1) reinterpret the guesses so that 'h' means the guess was too high, and l means the guess was too low (this is the way most people play the game), 2) modify GuessNumber to detect if any cheating has taken place. This occurs when there is nothing left to guess. void rsort (int nums , int nelems) This function sorts an array of integers by finding the minimum element, swapping it with the first element, and then repeating the process on the rest of the array, recursively, until the entire array is sorted. It should call a helper function, void sorthelp(int al , int start, int n), which plays a role for sort similar to what GuessNumber does for hilo

Explanation / Answer

#include<bits/stdc++.h>
using namespace std;

//*******************HILO*************************
void hilo(int a, int b)
{
if(b-a<=2)
{
if(b-a==1)
cout<<"You cheated"<<endl;
if(b-a==2)
{
cout<<"your number is"<<(a+1)<<endl;
}

}
else{
int x = a+(b-a)/2;
cout<<"Is it"<<x<<"(l,y,h)? ";
char ip;
cin>>ip;
cout<<endl;
if(ip == 'h')
{
hilo(a,x);
}
else if(ip == 'l')
{
hilo(x,b);
}
else if(ip=='y')
{
cout<<"your guess is"<<(b+(b-a)/2);
}
}
}

//***************RSORT******************************

int minIndex(int a[], int i, int j)
{
/*
Finding minimum index from unsorted array;
*/
if (i == j)
return i;
int valmin = INT_MAX;
int idx;
for(int k = i;k<=j;k++)
{
if(a[k]<valmin)
{
valmin=a[k];
idx=k;
}
}
return idx;
}

void rsort(int a[], int index , int n)
{
if (index == n) // array size becomes 0
return;
// finding minimum element index from array index to n;
int k = minIndex(a, index, n-1);
/*
Swapping operation between minimum element in the array
and start index of unsorted array.
*/
if (k != index)
{
int temp;
temp=a[k];
a[k]=a[index];
a[index]=temp;
}
rsort(a, index + 1,n);
}

//****************************************************
int main()
{
int num;
cout<<"Think of a number between 1 and 100"<<endl;
cin>>num;
hilo(0,num);

int arr[] = {3, 1, 5, 2, 7, 0};
int n = sizeof(arr)/sizeof(arr[0]);
rsort(arr, 0,n);
for (int i = 0; i<n ; i++)
cout << arr[i] << " ";
cout << endl;
}