COM SCI 32 Lecture Notes - Lecture 6: Linked List
Document Summary
Writing recursive functions: write the function header. What arguments your functions has and what it returns: define our magic function. Same aforementioned parameters but cannot pass in same values: add base case code (stopping condition) Take care of the simplest possible inputs: solve the problem using the same function. Use this magic function in your new function to solve the problem. If x is small enough to be trivially processed. Break x into several smaller problems (x1, x2, ) Call our function to solve each sub-problem: result1 = processsomedata(x1,n1) result2 = processsomedata(x2,n2) Combine the results (result1 resultn) into a solution to the whole problem. Factorial int fact (int n) { if (n == 0) return 1; int part1 = n; int part2 = fact (n-1); return part1 * part2; //insert base cases int rear = sumarr(arr+1, n-1); int total = arr[0] + rear; return total; : add first half and the last half int sumarr( int arr[], int n)