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

Hey, I\'ve been struggling with recursion, so if you guys can post the C++ sourc

ID: 3769955 • Letter: H

Question

Hey, I've been struggling with recursion, so if you guys can post the C++ source code for the following I'd greatly appreciate it. Thanks in advance.

1) C++ does not have an exponential operator. Write the recursive value-returning function Power that takes two parameters (number and exponent) and returns number multiplied by itself exponent times. That is, Power returns the result of numberexponent. Run a driver program (that is, a main program) that calls Power(7, 3) and prints the result.

2) Write a recursive function to print the values of a string in reverse order. Run a driver program that calls this function and prints the result.

3) Write a recursive int function that returns the position of a given value in an array. If the value is not in the array, return 0. Run a driver program that calls this function and prints the result.

4) How many possible bridge hands are there? This question is a specific case of the general question: how many combinations of X items can I make out of Y items? In the case of the bridge hand, x is 13 and y is 52. The solution is given by the following formula: Combinations(Y, X) = Y if X = 1   1 if X = Y   (Combinations(Y-1, X-1) + Combinations(Y-1, X)) if Y > X > 1

Write a recursive function that calculates the number of combinations of X items that can be made from Y items. Write a driver and answer the original questions. For this exercise you should use 7 for y and 3 for x.

5) Write a recursive function named CountOdd that accepts three arguments: an array, the size of the array and an integer parameter that represents the number of odd values in the array. The function should return the number of odd values in the array or 0 if there is no odd value in the array. Demonstrate the function in a driver program. No output statement in the function.

6) Ackermann’s Function is a recursive mathematical algorithm that can be used to test how well a computer performs recursion. Write a function A(m , n) that solves Ackermann’s Function. Use the following logic in your function. If m = 0 then return n + 1 If n = 0 then return A(m-1, 1) otherwise, return A(m-1, A(m, n-1)) Test your function in a driver program that displays the following values: A(0,0) A(0,1) A(1,1) A(1,2) A(1,3) A(2,2) A(3,2)

Explanation / Answer

1. Here is the solution for the first question:

#include <iostream>
using namespace std;
int Power(int a, int b)
{
if(b == 0)
return 1;
return a * Power(a, b-1);
}
int main()
{
cout<<"The result of Power(7,3) is: "<<Power(7,3)<<endl;
}

2. Here is the solution for the second question:

#include <iostream>
using namespace std;
string Reverse(string s)
{
if(s.length() == 1)
return s;
return Reverse(s.substr(1, s.length())) + s.at(0);
}
int main()
{
string str;
cout<<"Enter the string: ";
cin>>str;
cout<<"The reverse of: "<<str<<" is: "<<Reverse(str)<<endl;
}

3. Here is the solution for 3rd question:

#include <iostream>
using namespace std;
int RecursiveSearch(int A[], int count, int key)
{
if(count == -1)
return 0;
else if(A[count-1] == key)
return count;
else
return RecursiveSearch(A, count-1, key);

}
int main()
{
int Array[] = {10, 20, 40, 50, 30, 90, 70};
int ele, pos;
cout<<"Enter the element to search for: ";
cin>>ele;
pos = RecursiveSearch(Array, 7, ele);
if(pos == 0)
cout<<ele<<" is not found in the list."<<endl;
else
cout<<ele<<" found at position: "<<pos<<endl;
}

4. Here is the solution for 4th question.

#include <iostream>
using namespace std;
int Combinations(int y, int x)
{
if(x == 1)
return y;
else if(x == y)
return 1;
else if(y > x && x > 1)
return Combinations(y-1, x-1)+Combinations(y-1, x);
return 0;
}
int main()
{
int X, Y;
cout<<"Enter the value of X: ";
cin>>X;
cout<<"Enter the value of Y: ";
cin>>Y;
cout<<"The possible number of bridgehands for X: "<<X<<" and Y: "<<Y<<" is: "<<Combinations(Y, X)<<endl;
}

If you have any further queries, just get back to me.