hello , i been having some difficulty with writing the codes for the last two qu
ID: 3771088 • Letter: H
Question
hello , i been having some difficulty with writing the codes for the last two questions 5,6 thank you 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
#include <iostream>
using namespace std;
#define MAX 100
void CountOdd(int array[], int size, int& count_odd)
{
int* p = array;
if(p[0]%2==1)
count_odd++;
if(size == 1)
return;
else
CountOdd(p+1,size-1,count_odd);
}
int A(int m,int n)
{
if(m==0)
return n+1;
if(n==0)
return A(m-1,1);
else
return A(m-1,A(m,n-1));
}
int main()
{
int check_odd1[MAX] = {1,2,3,4,5,6,7,8,9,10};
int check_odd2[MAX] = {2,4};
int count_odd1;
CountOdd(check_odd1,10,count_odd1);
cout<<count_odd1<<endl;
int count_odd2;
CountOdd(check_odd2,2,count_odd2);
cout<<count_odd2<<endl;
cout<<"A(0,0) = "<<A(0,0)<<endl;
cout<<"A(0,1) = "<<A(0,1)<<endl;
cout<<"A(1,1) = "<<A(1,1)<<endl;
cout<<"A(1,2) = "<<A(1,2)<<endl;
cout<<"A(1,3) = "<<A(1,3)<<endl;
cout<<"A(2,2) = "<<A(2,2)<<endl;
cout<<"A(3,2) = "<<A(3,2)<<endl;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.