0% found this document useful (0 votes)
1 views20 pages

2023-09-06 Program, Algorithm Recursion

The document discusses programming concepts, focusing on the C programming language, data types, algorithms, and recursion. It emphasizes the importance of modularization in programming for better design and maintenance, and categorizes recursion into direct, indirect, and tail recursion. Additionally, it includes examples of recursive functions such as factorial and Fibonacci calculations.

Uploaded by

陳信
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views20 pages

2023-09-06 Program, Algorithm Recursion

The document discusses programming concepts, focusing on the C programming language, data types, algorithms, and recursion. It emphasizes the importance of modularization in programming for better design and maintenance, and categorizes recursion into direct, indirect, and tail recursion. Additionally, it includes examples of recursive functions such as factorial and Fibonacci calculations.

Uploaded by

陳信
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Program, Algorithm & Recursion

Kuan-Yu Chen (陳冠宇)


Review

• Homework: 35%
• Midterm: 30%
• Final: 40%
2
Data Type
• The programming language ‘C’ was developed in the early
1970s by Dennis Ritchie at Bell Laboratories
– C was initially developed for writing system software

• Data type determines the set of values that a data item can
take and the operations that can be performed on the item

– Note that C does not provide any data type for storing text
• Text is made up of individual characters
• “char” is supposed to store characters not numbers?
 In the memory, characters are stored in their ASCII codes
 ‘A’ has the ASCII code of 65 3
ASCII Codes
• In C language
– ‘5’ == int 53
– ‘5’-’0’==int 53 – int 48 == int 5
– If char c = ‘B’+32, then c==‘b’

4
Program
• A program contains one or more functions,
where a function is defined as a group of
statements that perform a well-defined task
– A program should undoubtedly give correct
results, but along with that it should also run
efficiently

• The definition of a good program is


– runs correctly
– easy to read and understand
– easy to debug
– easy to modify

5
image source: https://www.bilibili.com/video/BV1Po4y1m7DG/

Algorithm.
• The typical definition of algorithm is “a formally defined
procedure for performing some calculation”
– In general terms, an algorithm provides a blueprint to write a
program to solve a particular problem
– A program does not have to satisfy the fourth condition

(明確性)

(有限性)

(有效性:明確且可行) 6
Algorithm..
• A complex algorithm is often divided into smaller units called
modules
– This process of dividing an algorithm into modules is called
modularization (模組化)

• The key advantages of modularization are as follows:


– It makes the complex algorithm simpler to design and
implement
– Each module can be designed independently

7
Algorithm & Program

8
Function
• Let us analyze the reasons why segmenting a program into
manageable chunks is an important aspect of programming
– Dividing the program into separate well-defined functions
facilitates each function to be written and tested separately

– Understanding, coding, and testing multiple separate functions


is easier than doing the same for one big function

– Maintaining a huge program will be a difficult task

– When a big program is broken into comparatively smaller


functions, then different programmers working on that project
can divide the workload by writing different functions

9
Recursion Function.
• The recursion mechanisms are extremely powerful, because
they often can express a complex process very clearly

• Recursion functions can be categorized into three classes


– Direct Recursion
• The function may call itself before it is done

– Indirect Recursion
• The function may call other functions that again invoke the calling
function

– Tail Recursion
• The function may call itself at the end of the function
• A special case of direct recursion
10
Recursion Function..
Direct Recursion Indirect Recursion Tail Recursion

calling cycle

11
Recursion Function…
• Let’s make a comparison

Recursion Non-Recursion

Codes are more compact Codes are complicated

Easy to understand Hard to read

Time-consuming Time-saving

12
Examples – 1
• Please write down a recursive program to do factorial
0! = 1

1! = 1

2! = 1 × 2

3! = 1 × 2 × 3

𝑛𝑛! = 1 × 2 × 3 × ⋯ × 𝑛𝑛

13
Examples – 2.
• Please (1) write a recursive program, 𝐹𝐹𝐹𝐹𝐹𝐹( 𝑖𝑖𝑖𝑖𝑖𝑖 𝑎𝑎 ), to calculate
the Fibonacci number; (2) how many function calls do you
need when we want to calculate 𝐹𝐹𝐹𝐹𝐹𝐹(5)?

14
Examples – 2..
• Please write a recursive program, 𝐹𝐹𝐹𝐹𝐹𝐹( 𝑖𝑖𝑖𝑖𝑖𝑖 𝑎𝑎 ), to calculate
the Fibonacci number; (2) how many function calls do you
need to do when we want to calculate 𝐹𝐹𝐹𝐹𝐹𝐹(5)?
0, 𝑖𝑖𝑖𝑖 𝑎𝑎 = 0
𝐹𝐹𝐹𝐹𝐹𝐹(𝑎𝑎) = � 1, 𝑖𝑖𝑖𝑖 𝑎𝑎 = 1
𝐹𝐹𝐹𝐹𝐹𝐹 𝑎𝑎 − 1 + 𝐹𝐹𝐹𝐹𝐹𝐹 𝑎𝑎 − 2 , 𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜

15
Examples – 2…
• Please (1) write a recursive program, 𝐹𝐹𝐹𝐹𝐹𝐹( 𝑖𝑖𝑖𝑖𝑖𝑖 𝑎𝑎 ), to calculate
the Fibonacci number; (2) how many function calls do you
need to do when we want to calculate 𝐹𝐹𝐹𝐹𝐹𝐹(5)?

𝐹𝐹𝐹𝐹𝐹𝐹(5) 𝐹𝐹𝐹𝐹𝐹𝐹(4) 𝐹𝐹𝐹𝐹𝐹𝐹(3) 𝐹𝐹𝐹𝐹𝐹𝐹(2) 𝐹𝐹𝐹𝐹𝐹𝐹(1)

𝐹𝐹𝐹𝐹𝐹𝐹(1) 𝐹𝐹𝐹𝐹𝐹𝐹(0)

𝐹𝐹𝐹𝐹𝐹𝐹(2) 𝐹𝐹𝐹𝐹𝐹𝐹(1)

𝐹𝐹𝐹𝐹𝐹𝐹(0)

𝐹𝐹𝐹𝐹𝐹𝐹(3) 𝐹𝐹𝐹𝐹𝐹𝐹(2) 𝐹𝐹𝐹𝐹𝐹𝐹(1)

𝐹𝐹𝐹𝐹𝐹𝐹(1) 𝐹𝐹𝐹𝐹𝐹𝐹(0)

16
Examples – 3.
• Given an Ackerman’s function 𝐴𝐴(𝑚𝑚, 𝑛𝑛), please calculate
𝐴𝐴(1,2).
𝑛𝑛 + 1, 𝑖𝑖𝑖𝑖 𝑚𝑚 = 0
𝐴𝐴 𝑚𝑚, 𝑛𝑛 = � 𝐴𝐴 𝑚𝑚 − 1,1 , 𝑖𝑖𝑖𝑖 𝑛𝑛 = 0
𝐴𝐴 𝑚𝑚 − 1, 𝐴𝐴 𝑚𝑚, 𝑛𝑛 − 1 , 𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜

𝐴𝐴 1,2 = 𝐴𝐴 0, 𝐴𝐴 1,1

𝐴𝐴 1,1 = 𝐴𝐴 0, 𝐴𝐴 1,0

𝐴𝐴 1,0 = 𝐴𝐴 0,1

𝐴𝐴 0,1 = 2

17
Examples – 3..
• Given an Ackerman’s function 𝐴𝐴(𝑚𝑚, 𝑛𝑛), please calculate
𝐴𝐴(1,2).
𝑛𝑛 + 1, 𝑖𝑖𝑖𝑖 𝑚𝑚 = 0
𝐴𝐴 𝑚𝑚, 𝑛𝑛 = � 𝐴𝐴 𝑚𝑚 − 1,1 , 𝑖𝑖𝑖𝑖 𝑛𝑛 = 0
𝐴𝐴 𝑚𝑚 − 1, 𝐴𝐴 𝑚𝑚, 𝑛𝑛 − 1 , 𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜𝑜

𝐴𝐴 1,2 = 𝐴𝐴 0, 𝐴𝐴 1,1 = 𝐴𝐴 0,3 = 4

𝐴𝐴 1,1 = 𝐴𝐴 0, 𝐴𝐴 1,0 = 𝐴𝐴 0,2 = 3

𝐴𝐴 1,0 = 𝐴𝐴 0,1 = 2

𝐴𝐴 0,1 = 2

18
Summary
• Algorithm + Data Structure = Program

– A complex algorithm is often divided into smaller units called


modules

– A complex program is often split into manageable functions


• Recursion function is an important mechanism
Direct Recursion
Indirect Recursion
Tail Recursion

19
Questions?

Thank you
for your
listening!

[email protected]

20

You might also like