0

how it works?

#include <iostream> using namespace std; int fun(int arr[],int n) { int x; if(n == 1) return arr[0]; else x = fun(arr, n-1); if(x > arr[n-1]) return x; else return arr[n-1]; } int main() { int array[] = {10, 20, 30, 40}; int n = sizeof(array)/sizeof(int); printf(" %d ", fun(array, n)); getchar(); return 0; }

21st Sep 2018, 6:59 PM
YAKSH PATEL (aka YKP The GREAT)
YAKSH PATEL (aka YKP The GREAT) - avatar
1 Answer
0
it returns the largest number from the array. by recursion fun(arr,n) returns the largest number.. and for that first it reaches the first number which will end the recursion by n==1, fun() returns that so the value of x will be arr[0] for second last recursion and fun() will return x if it's greater meaning it'll return arr[1] since it's greater, and for third last recursion x will be arr[1] , (greater of two) and so on the function only returns the largest from first member to last...
22nd Sep 2018, 12:36 PM
Gaurav Atreya
Gaurav Atreya - avatar