COM SCI 32 Lecture Notes - Lecture 6: Linked List

192 views6 pages

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)

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents