Question

In C can you change this code to use Tail Call Recursion int min (int n,...

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;
}

Homework Answers

Answer #1

//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);

}

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
The language is C. What is function(9)? Use a recursion tree to find the answer. int...
The language is C. What is function(9)? Use a recursion tree to find the answer. int function(int n) {                 If (n<=1)                                 return 1;                 If (n%2==0)                                 return f(n/2);                 return f(n/2) + f((n/2)+1); }
Below is C code and Python code for an algorithm. C code: void foo( int n,...
Below is C code and Python code for an algorithm. C code: void foo( int n, int A, int B, int C ) { if( n==1 ) { printf("%d to %d\n",A,B); return; } foo( n-1, A, C, B ); printf("%d to %d\n",A,B); foo( n-1, B, C, A ); Python code: def foo(n , A, B, C): if n==1: print A, "to", B return foo(n-1, A, C, B) print A, "to", B foo(n-1, B, C, A) Let Hn be the number...
Translate C code into MIPS. Do not include an exit syscall int proc1( int a, int...
Translate C code into MIPS. Do not include an exit syscall int proc1( int a, int b ) { if ( proc2( a, b ) >= 0 ) return 1; else return -1; } int proc2( int a, int b ) { return (a*10) & (b*10); } int main() { int a = 9; int b = -10; int c = a + b + proc1( a, b ); printf("%d\n", c ); return 0; }
Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an...
Write a recursive method public static int sumEveryOther(int n) that takes a positive int as an argument and returns the sum of every other int from n down to 1. For example, the call sumEveryOther(10) should return 30, since 10 + 8 + 6 + 4 + 2 = 30. The call sumEveryOther(9) should return 25 since 9 + 7 + 5 + 3 + 1 = 25. Your method must use recursion.
Find the sum of squares 1^2 + 2^2 + ... n^2 using iteration and recursion *...
Find the sum of squares 1^2 + 2^2 + ... n^2 using iteration and recursion * I'm guessing we need to modify it Here's the code unmodified public class Sum { //Non recursive sum public static long sum1 (int n) { long sum = 1L; for (int i = 2; i <= n; ++i) sum = sum + i *i ; return sum; } //Recursive sum public static long sum2 (int n) { if (n < 2)return 1L; return sum2(n...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++...
CAN YOU PLEASE WRITE THIS CODE IN A DIFFERENT WAY 'EASIER AND BETTER' QUESTION Using C++ 11. Write a function that will merge the contents of two sorted (ascending order) arrays of type double values, storing the result in an array out- put parameter (still in ascending order). The function shouldn’t assume that both its input parameter arrays are the same length but can assume First array 04 Second array Result array that one array doesn’t contain two copies of...
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n ==...
QUESTION 1 For the following recursive function, find f(5): int f(int n) { if (n == 0)    return 0; else    return n * f(n - 1); } A. 120 B. 60 C. 1 D. 0 10 points    QUESTION 2 Which of the following statements could describe the general (recursive) case of a recursive algorithm? In the following recursive function, which line(s) represent the general (recursive) case? void PrintIt(int n ) // line 1 { // line 2...
(Recursion) The function to be used in the calculation of binomial numbers, C (n, k): Express...
(Recursion) The function to be used in the calculation of binomial numbers, C (n, k): Express the recursion definition. Give the pseudo-code of the appropriate algorithm.(I would appreciate it if you explain all the questions in an explanatory way.)
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i];...
Consider the following function: double arrayavg(int a[], int n){ int sum=0; for(int i=0; i!=n; ++i){ sum+=a[i]; } return (double)sum/n; } Rewrite the function to eliminate the array subscripting (a[i]), using pointer arithmatic instead. Write a program that reads a line of input and checks if it is a palindrome (ignoring spaces) Write a program that takes the name of a file as a command line argument, and prints the contents of the file (similar to the linux 'cat' program). Write...
Convert the following C program to inline assembly int main(){ int i; printf("Hex Dec Oct Ch\n");...
Convert the following C program to inline assembly int main(){ int i; printf("Hex Dec Oct Ch\n"); for (i = 32; i < 256; i++){ printf("%2x %3d %3o %c\n",i,i,i,i); } return 0; } the code below shows where to do the assembly at: int main(){ int i = 0; char *hdr = "Hex Dec Oct Ch\n"; char *msg = " %2x %3d %3o %c\n"; printf("%s\n",hdr); ———asm{ // DO ASSEMBLY HERE } system("pause"); }
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT