Question

In: Computer Science

C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function...

C++ Recursive Functions:
Please call functions in a main function as well.

1. A recursive function that print the reverse of a string. (e.g., void
printReverse(string exp)). For example, if exp =”coding”, then the function
should print out “gnidoc”.
2. Implement a non-recursion-based binary search function. Convert this function
into a recursion-based function.
3. Implement a recursive and non-recursive Fibonacci function.

Solutions

Expert Solution

First of all we have to know about Recusrsive Funciton.

A recursive function is a function that calls itself during its execution. The process may repeat several times, outputting the result and the end of each iteration. The function Count() below uses recursion to count from any number between 1 and 9, to the number 10.

Answer for No.2

#include <stdio.h>

int binarySearch(int arr[], int l, int r, int x)

{

  while (l <= r)

  {

    int m = l + (r-l)/2;

    // Check if x is present at mid

    if (arr[m] == x)

        return m;  

    // If x greater, ignore left half  

    if (arr[m] < x)

        l = m + 1;

    // If x is smaller, ignore right half

    else

         r = m - 1;

  }

  // if we reach here, then element was not present

  return -1;

}

int main(void)

{

   int arr[] = {2, 3, 4, 10, 40};

   int n = sizeof(arr)/ sizeof(arr[0]);

   int x = 10;

   int result = binarySearch(arr, 0, n-1, x);

   (result == -1)? printf("Element is not present in array")

                 : printf("Element is present at index %d", result);

   return 0;

}

Answer for No.3

//Fibonacci Series using Recursion

#include<bits/stdc++.h>

using namespace std;

int fib(int n)

{

    if (n <= 1)

        return n;

    return fib(n-1) + fib(n-2);

}

int main ()

{

    int n = 9;

    cout << fib(n);

    getchar();

    return 0;

}


Related Solutions

In C++, please check for memory leaks Recursive Functions Goals Create and use recursive functions In...
In C++, please check for memory leaks Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character. Example: Input of “Hello,...
C++ Recursion, Change the delete_node function in the header file to a recursive delete_node function, main...
C++ Recursion, Change the delete_node function in the header file to a recursive delete_node function, main is already set. Hint: It might be helpful to modify the function so that it uses a separate recursive function to perform whatever processing is needed. //////////////////////////////////////////////////////////////header.h////////////////////////////////////////////////////////////////////////////////////////////// #ifndef HEADER_H_ #define HEADER_H_ #include <iostream> using namespace std; template <class T> class LL { private:    struct LLnode    {        LLnode* fwdPtr;        T theData;    };    LLnode* head; public:    LL();...
In C Write a main function with a function call to a function called GetLetter which...
In C Write a main function with a function call to a function called GetLetter which has two double arguments/parameters. The function returns a character. Declare and initialize the necessary data variables and assign values needed to make it executable and to prevent a loss of information
Write a C++ program which consists of several functions besides the main() function. The main() function,...
Write a C++ program which consists of several functions besides the main() function. The main() function, which shall ask for input from the user (ProcessCommand() does this) to compute the following: SumProductDifference and Power. There should be a well designed user interface. A void function called SumProductDifference(int, int, int&, int&, int&), that computes the sum, product, and difference of it two input arguments, and passes the sum, product, and difference by-reference. A value-returning function called Power(int a, int b) that...
C++ Ackermann’s function is a recursive mathematical algorithm that can be used to test how well...
C++ 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,...
C++ Write a program that has two functions. The 1st function is the main function. The...
C++ Write a program that has two functions. The 1st function is the main function. The main function should prompt the user for three inputs: number 1, number 2, and an operator. The main function should call a 2nd function called calculate. The 2nd function should offer the choices of calculating addition, subtraction, multiplication, and division. Use a switch statement to evaluate the operator, then choose the appropriate calculation and return the result to the main function.
C++ This needs to be a recursive combinational array please. This NEEDS to be recursive and...
C++ This needs to be a recursive combinational array please. This NEEDS to be recursive and no vectors. Thank you Write a program that solves the knapsack problem recursively for an arbitrary knapsack capacity and series of weights. Assume the weights are sorted in an array. Hint: The arguments to the knapsack function are target weight and the array index where the remaining items start. The knapsack problem in its simplest form involves trying to fit items of different weights...
USE C PROGRAMMING, call all functions in main, and use a 2 by 3 2d array...
USE C PROGRAMMING, call all functions in main, and use a 2 by 3 2d array to test, thanks. get_location_of_min This function takes a matrix as a 2-D array of integers with NUM_COLS width, the number of rows in the matrix and two integer pointers. The function finds the location of the minimum value in the matrix and stores the row and column of that value to the memory location pointed to by the pointer arguments. If the minimum value...
.......Subject Java..... main() main() will ask the user for input and then call functions to do...
.......Subject Java..... main() main() will ask the user for input and then call functions to do calculations. The calculations will be returned to main() where they will be printed out. First function Create a function named computeBill that receives on parameter. It receives the price of an item. It will then add 8.25% sales tax to this and return the total due back to main(). Second function Create another function named computeBill that receives 2 parameters. It will receive the...
C++ Write a recursive function that computes and returns the product of the first n >=1...
C++ Write a recursive function that computes and returns the product of the first n >=1 real numbers in an array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT