Tutorial 1 C Programming - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Tutorial 1 C Programming

Description:

http://en.wikipedia.org/wiki/Fractal. Beautiful Recursion: Fractal. More Examples. Droste Effect ... http://en.wikipedia.org/wiki/Mutual_recursion. In what ... – PowerPoint PPT presentation

Number of Views:343
Avg rating:3.0/5.0
Slides: 14
Provided by: dcsa
Category:

less

Transcript and Presenter's Notes

Title: Tutorial 1 C Programming


1
Tutorial 6Recursion
2
Beautiful Recursion Fractal
  • Sierpinski Triangle
  • Recursive Tree
  • Koch Snowflake
  • http//en.wikipedia.org/wiki/Fractal

3
More Examples
  • Droste Effect
  • http//en.wikipedia.org/wiki/Droste_effect
  • Russian Doll
  • http//en.wikipedia.org/wiki/Matryoshka_doll
  • Recursive Acronyms
  • PHP PHP Hypertext Preprocessor
  • GNU GNUs Not Unix
  • VISA VISA International Service Association
  • http//en.wikipedia.org/wiki/Recursive_acronym
  • A Definition in an English-English dictionary
  • Recursion
  • See Recursion.
  • If you are tired, stop.

4
Recursion Basic Idea
  • Template for ANY recursion
  • Recursive_Function_Name(Parameter_List)
  • // you will always see selection statement
    (if, switch, etc)
  • if (Base Case)
  • do something simple
  • else // Recursive Case
  • Recursive_Function_Name(Modified
    Parameter_List)
  • Modified Parameter_List must bring the recursive
    function closer to the base case (simplified
    problem).

5
ExampleHow many ways to choose k items out of n
items?
  • How many ways to choose when k out of (n3)?
  • 0, actually this problem is undefined for k such case will never exist.
  • How many ways to choose when k n, e.g. (k4)
    out of (n3)?
  • 0, also undefined, impossible
  • How many ways to choose when k n, e.g. (k3)
    out of (n3)?
  • 1, take all
  • How many ways to choose when k 0, e.g. (k0)
    out of (n3)?
  • 1, do not take anything
  • How many ways to choose in general case, e.g. k n, k 0 (k2) out of (n3)?
  • Either I take one item X,
  • My problem becomes choosing k-1 (k1) out of n-1
    (n2)
  • Plus if I do not take that one item X
  • My problem becomes choosing k (k2) out of n-1
    (n2)
  • Code it
  • int c(int n,int k) if (kn) // assume kundefined return 0 if (k n k 0)
    return 1 return c(n-1,k-1) c(n-1,k)

6
Recursion is Powerful
  • Divide and Conquer
  • Dynamic Programming
  • Used in future lectures in CS1102
  • Sorting Mergesort and Quicksort are recursive
  • Tree/Heap/Graph these data structures and
    operations are naturally recursive
  • Can also be used for past CS1102 materials
  • Linked List data structure and operations are
    also naturally recursive

7
Student Presentation
  • Gr3 Main Backup
  • Cao Hoang Dang Cai Jingfang, Du Xin
  • Chng Jiajie Ding Ying Shiaun
  • Chen Tianni Rebecca Jashan Deep Kaur
  • Huang Chuanxian Leow Wei Jie, Lim Wei Hong
  • Gr4 Main Backup
  • Tan Peck Luan Tan Shu Yu Cynthia
  • Li Yawen Tan Miang Yeow
  • Wang Kang Ahmed Shafeeq
  • Wong Suet Teng, M Chong Tze Koi
  • Overview of the questions
  • Trace IsEven/IsOdd
  • Recursive counting
  • Recursive sort
  • Reversing BasicLinkedList
  • The rabbit question is your homework
  • Gr5 Main Backup
  • Wu Shujun Joyeeta Biswas
  • Teo Sim Yee, Stephanie Tan Yan Hao
  • Wang Ruohan Zheng Yang
  • Liu Na Zhang Denan
  • Gr6 Main Backup
  • Rasheilla Bte Rajah Tan Ping Yang
  • Lou Wei Chen Gerard J Koh Jye Yiing
  • Zhang Chao Wong Shiang Ker
  • Gan Zhi Wei James Kuganeswari D/O K

8
Q1 Trace
  • boolean isEven(int number)
  • if (number 0)
  • return true
  • else
  • return isOdd(number-1)
  • boolean isOdd(int number)
  • if (number 0)
  • return false
  • else
  • return isEven(number-1)
  • Trace isEven(5)!
  • Trace isOdd(1), isEven(2)!
  • There are repeated sub-problems!
  • You will learn how to optimize this situation in
    more advanced modules!
  • Are these two functions recursive?
  • http//en.wikipedia.org/wiki/Mutual_recursion
  • In what situation(s) these two functions may
    fail?
  • Negative numbers
  • How to improve these codes to address the
    situation above?
  • If input is negative, multiply it with -1 first.

9
Q2 Recursive Counting
  • Adam can climb
  • 1 stair per step, or
  • 2 stairs per step
  • Given n stairs,Count how many waysAdam can go
    upFrom stair 0 (ground) to stair n!
  • n 4
  • 1111
  • 112
  • 121
  • 211
  • 22
  • Thinking steps
  • Let steps(n) the number of ways
  • Start from small cases
  • steps(0), not defined, assume n0
  • steps(1), 1 way (1 step)
  • steps(2), 2 ways (11 or 2)
  • steps(3), 3 ways(111, 21, or12)
  • steps(4), 5 ways(1111, 211, 121,
    or112, 22)
  • Feel that this problem is recursive!
  • Go backwards!
  • There are two ways to reach stair n
  • From stair n-2, then Adam 2 stairs, or
  • From stair n-1, then Adam 1 stair
  • Naturally
  • steps(n) steps(n-1)steps(n-2)

10
Q3 Recursive Sort
  • Many options
  • We will learn this again in details in Lecture 6
    (Sorting)
  • Possible answers
  • Recursive bubble sort
  • Recursive selection sort
  • Recursive merge sort
  • Recursive quick sort
  • Recursive selection sort
  • sort(array, size)
  • if (size 1)
  • return // done (base case)
  • pass through the array one time to get the
    maximum item M
  • swap M with arraysize-1
  • call sort(array, size-1) // recursive

11
Q4 Reversing BLL Recursively
  • Original BLL
  • A ? B ? C ? D
  • Head is A
  • Reversed BLL
  • A ? B ? C ? D
  • Head is D
  • Given a BLL,reverse it, recursively!
  • Possible answer
  • node reverse(node n)
  • node newhead
  • if (n.next ! null)
  • newhead reverse(n.next)
  • n.next.next n // change arrow!
  • n.next NULL
  • else // base case, this is old tail!
  • newhead n // now new head
  • return newhead
  • Call with
  • head reverse(head)

12
Food For Thought
  • If you are asked to design a recursive function
    to model your life while taking CS1102, what kind
    of function that you will design?
  • void CS1102_Life(int week_ID)
    do_tutorial_and_lab(week_ID) if (next_week)
    CS1102_Life(week_ID1)
  • Wrong, this is infinite
  • void CS1102_Life(int week_ID)
    do_tutorial_and_lab(week_ID) if (week_ID !
    end of semester) CS1102_Life(week_ID1)
  • It is going to be over ?

13
Midterm Test
  • If you want to know the truth
  • Visit my website
  • I have scanned my answers and upload the PDF
    there.
Write a Comment
User Comments (0)
About PowerShow.com