The document provides examples of implementing various recursive functions including:
1. Printing an array, finding the maximum/minimum element, checking if a string is a palindrome, calculating factorials, and more using recursion.
2. Detailed implementations and test cases are given for each function to demonstrate how to approach recursion problems.
3. The examples cover a wide range of applications from math, strings, arrays, and more to illustrate different recursive problem solving techniques.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
72 views3 pages
Fucntion - Recursion
The document provides examples of implementing various recursive functions including:
1. Printing an array, finding the maximum/minimum element, checking if a string is a palindrome, calculating factorials, and more using recursion.
2. Detailed implementations and test cases are given for each function to demonstrate how to approach recursion problems.
3. The examples cover a wide range of applications from math, strings, arrays, and more to illustrate different recursive problem solving techniques.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3
Implement function: void printArray(int n){}
to print 0, 1, 2, ..., n (n is positive integer and has no space at the end).
---------- void printArray(int n) { if (n==0) { cout << n; return; } else printArray(n-1); cout << ", " << n; } ========== Given a positive number, print following a pattern without using any loop. Test 1: Input: n = 16 Output: 16, 11, 6, 1, -4, 1, 6, 11, 16 (has no space at the end) Test 2: Input: n = 10 Output: 10, 5, 0, 5, 10 (has no space at the end) We basically first reduce 5 one by one until we reach a negative or 0. After we reach 0 or negative, we one add 5 until we reach n. ---------- void printPattern(int n) { if (n-5 > 0) { cout << n << " "; printPattern(n-5); } else cout << n << " " << n-5; cout << " " << n; } ========== Implement function: int findMax(int* arr, int length){} to find the largest element using recursion (with length is the number of elements in integer array arr). Test: int arr[] = {10, 5, 7, 9, 15, 6, 11, 8, 12, 2}; cout << findMax(arr, 10); Result: 15 ---------- int findMax(int* arr, int length){ if (length == 1) return arr[0]; return max(findMax(arr, length-1), arr[length-1]); } ---------- int findMax (int* a, int n){ if (n==1) return a[0]; int maxEle = findMax (a, n-1); return (maxEle > a[n-1]?maxEle: a[n-1]); } ========== Implement function: bool isPalindrome(string str){} to check if the given non empty string is palindrome, else not palindrome using recursion. Test: cout << isPalindrome("do geese see god"); Result: 1 ---------- bool isPalindrome(string str){ if (str.length() < 2) return true; else { if (str[0] == ' ') return isPalindrome(str.substr(1, str.length()-1)); else if (str[str.length()-1] == ' ') return isPalindrome(str.substr(0, str.length()-1)); else if (str[0] == str[str.length()-1] && isPalindrome(str.substr(1, str.length()-2))) return true; else return false; } } ========== Give two positive integers a and b, implement function: int findGCD(int a, int b) {}to find GCD (Greatest Common Divisor) of a and b using recursion. ---------- int findGCD(int a, int b) { if (a==b) return a; else if(a>b) return findGCD(a-b, b); else return findGCD(b, b-a); } ========== Give a positive integer x, implement recursive function: void printHailstone(int number){} to print the Hailstone Sequence of a given number upto 1 (no space at the end). Hailstone Sequences follow these rules: If a number is even, divide it by 2 If a number is odd, multiply it by 3 and add 1. Example: If number = 5. 5 is odd number so next number is 5*3 + 1 = 16. 16 is even number so next number is 16/2 = 8... Finally, we get Hailstone sequence: 5 16 8 4 2 1. ---------- void printHailstone(int number) { if (number == 1) { cout << number; return; } cout << number << " "; if (number % 2 == 0) return printHailstone(number/2); else return printHailstone(number*3 + 1); } ========== Function: int myArrayToInt(char* str, int n){} takes a string str (which represents an positive decimal number), n is the number of elements in the string as arguments and returns its value. Test: char str[] = "2020"; printf("%d", myArrayToInt(str, 4)); Result: 2020 ---------- int myArrayToInt(char *str, int n) { if (n == 0) return 0; return (*str-48)*pow(10, n-1) + myArrayToInt(str+1, n-1); } ========== Give two positive integers a and b, implement function: int findLCM(int a, int b){} to find LCM (Lowest Common Multiple) of a and b using recursion. --------- int findLCM(int a, int b) { static int multiple = 0; multiple += b; if((multiple % a == 0) && (multiple % b == 0)) return multiple; else return findLCM(a, b); } ========== Given a string, implement function: int strLen(char* str){} to calculate length of the string using recursion. ---------- int strLen(char* str) { if (*str == '\0') return 0; return strLen(str + 1) +1; } ========== Given a string s representing a sentence consisting only of a-z and A-Z and space character. Your task is to implement a function with following prototype: string reverseSentence(string s); The function returns the reverse sentence of sentence s Test: cout << reverseSentence("data structure and algorithm is scary"); Result: scary is algorithm and structure data ---------- string reverseSentence(string s) { if (s.find(" ") == std::string::npos) return s + " "; int index = s.find(" "); return reverseSentence(s.substr(index+1, s.length()-index-1)) + s.substr(0, index+1); } ========== Given a string s consisting only of '(' and ')'. Your task is to implement a function with following prototype: int mininumBracketAdd(string s); The function returns the mininum number of brackets needed to be inserted to s so that the brackets are balanced. More info: A sequence of brackets is balanced when there are no unmatched brackets. Example: ()(()) is balanced, but ))() is not. ---------- int mininumBracketAdd(string s) { if (s.length()==0) return 0; unsigned int index1 = s.find("("); if (index1 <= s.length()) { unsigned int index2 = s.find(")", index1); if (index2 <= s.length()) { s.erase(index1,1).erase(index2-1,1); return mininumBracketAdd(s); } return s.length(); } return s.length(); } ========== Find number of step needed so that you can walk through all stair case if each step equals 1 or 2 ---------- int count(int k){ if (k == 0) return 1; else if (k < 0) return 0; else return count(k - 2) + count(k - 1); }