Write four programs one to solve each problem below. These programs should use recursion to solve these problems. Test you programs with the given test data for each problem. Upload your source into blackboard. maximum Write a recursive method to find the maximum value of an array of ints. If an empty array is given as input you should return Integer. MIN_VALUE. maximum(new int[] {1, 5, 4, 3, 2}) returns the int value 5. maximum(new int[] {}) returns Integer.MIN.VALUE (-2147483648). reverse Write a recursive method that takes a String and returns the reversed String (characters in the opposite order). Feel free to create your own private, recursive method. Alternatively the String.substring and String.toChar methods will be helpful. reverse(\"abcde\") returns \"edcba\". euler Write a recursive method that takes a double value x, and computes the function e^x using the infinite series c^x = 1 + (x^1)/1! + (x^2)/2! + (x^3)/3! +. .. euler(2) returns something close to 7.389056098930649. Due to round off error, the last several digits may differ slightly. Matching 6 digits is sufficient. collatz Write a recursive method that calculates the length of the collatz sequence for a given input. The collatz sequence for an input may be calculated as follows. If the current value is even, the next value is that number divided by two (n/2). If the current value is odd, the next value is that number multiplied by three and then incremented (3n+1). This process continues until the next value is 1 For input = 3 the collatz sequence would be {3, 10, 5, 16, 8, 4, 2, 1}. Only positive inputs (greater than 1) will be given! collatz(3) returns 8.
Assuming the programming language to be used is C++
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
#define MIN_VALUE -2147483648
int maximum( int a[] ){
int size = sizeof( a )/sizeof(a[0]);
if( size == 0 ){
return MIN_VALUE;
}
int maxAt = 0;
for(int i = 1; i < size; i++ ){
if( a[i] > a[maxAt] ){
maxAt = i;
}
}
return a[maxAt];
}
string reverse( string a ){
string res = \"\";
for( int i = 0; i < a.size(); i++){
res = a[i] + res;
}
return res;
}
double euler( double x ){
double result = 0;
double tempRes = 1;
for(int i = 1; i < 100; i++ ){
result += tempRes;
tempRes*= x;
tempRes/= i;
}
return result;
}
int collatz( int x ){ //x > 1 in inputs
int result = 1;
while(x != 1){
result++;
if( x%2 == 0 ){ x = x/2;}
else{ x= 3*x + 1; }
}
return result;
}
int main(){
int a[]= { 1,5,4,3,2 };
cout << maximum(a) << endl;
cout << reverse(\"abcde\") << endl;
cout << fixed << setprecision(10);
cout << euler(2) << endl;
cout << collatz(3) << endl;
}