In C can you change this code to use Tail Call Recursion
int min (int n, int[] input) { int i; int result; for (result = input[0], i = 0; i < n; i += 1) { if (result > input[i]) result = input[i]; } return result; }
//This is min function using Tail call Recurssion
//It takes 4 arguments
//1. Number of elements in array
//2. Array
//3. index of the array
//4. small element of the array
int min(int n,int input[], int index,int small){
//If index is equal to n that means we traverse the complete array and return the small element
if(index == n){
return small;
}else{
//Now we check if input[index] is less than small element then we update the small elements
if(input[index] < small){
small = input[index];
}
//Here is the tail call recurssion
//we update the index by add +1 to it
return minHelper(n,input,index+1,small);
}
}
Get Answers For Free
Most questions answered within 1 hours.