Top Banner
Recursive Programming (I) Lecture 20 CptS 121 – Summer 2016 – Armen Abnousi
36

Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Sep 01, 2018

Download

Documents

buique
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Recursive Programming (I)

Lecture 20

CptS 121 – Summer 2016 – Armen Abnousi

Page 2: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Function calls and order of operation

• Within a block of code, the control is sequential.

• i.e. running the commands one by one, in the order

they appear.

• Calling a function (b), inside a block of code (fun a):

pauses the operation of the current block (a),

Moves the control to the callee function (b),

Executes the callee function (b) until it finishes (returns the

output or ends the block)

Control returns to the caller function (a)

Resumes operation in (a) from where it paused

Page 3: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Function calls and order of operation

int main()

{

double num;

fun();

return 0;

}

1

2

3

4

Order of operation

Page 4: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Function calls and order of operation

int main()

{

double num;

fun();

return 0;

}

void fun()

{

int inFun;

printf(“inside function”);

}

1

2

3

4

Order of operation

5

6

10

Page 5: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Function calls and order of operation

int main()

{

double num;

fun();

return 0;

}

void fun()

{

int inFun;

printf(“inside function”);

}

1

2

3

4

Order of operation

5

6

10

11

Page 6: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Function calls and order of operation

• There might be more than one function call:

(Each box represents a function. Function name is written

above the box. Inside the box are some statements from

the function).

funa();

main()

Page 7: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Function calls and order of operation

• There might be more than one function call:

funa();

main()

Page 8: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Function calls and order of operation

• There might be more than one function call:

funa();

main()

funb();

funa()

Page 9: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Function calls and order of operation

• There might be more than one function call:

funa();

main()

funb();

funa()

Page 10: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Function calls and order of operation

• There might be more than one function call:

funa();

main()

funb();

funa()

funb()

Page 11: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Function calls and order of operation

• There might be more than one function call:

funa();

main()

funb();

funa()

funb()

Page 12: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Function calls and order of operation

• There might be more than one function call:

funa();

main()

funb();

funa()

funb()

Page 13: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Function calls and order of operation

• There might be more than one function call:

funa();

main()

funb();

funa()

funb()

Page 14: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Function calls and order of operation

• There might be more than one function call:

funa();

main()

funb();

funa()

funb()

Page 15: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Function calls and order of operation

• There might be more than one function call:

funa();

main()

funb();

funa()

funb()

Page 16: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Recursion

• We might have a cycle of function calls!

Function funa() might call itself

Function funa() might call funb() while funb() calls funa()

• This is called recursion.

• When a function calls itself a copy of the function starts

running with new local variables and input parameters

Page 17: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Recursion and local variables

• Function funa() is calling itself. Recursion!

funa(1);

main()

funa(a+b);

funa(a)

b = 1

Page 18: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Recursion and local variables

• Function funa() is calling itself. Recursion!

funa(1);

main()

funa(a+b);

funa(a)

b = 1

Page 19: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Recursion and local variables

• Function funa() is calling itself. Recursion!

funa(1);

main()

funa(a+b);

funa(a)

b = 1

a b

Memory: 1 1

Data space for funa()’s

first call

Page 20: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Recursion and local variables

• Function funa() is calling itself. Recursion!

funa(1);

main()

funa(a+b);

funa(a)

b = 1

a b a b

Memory: 1 1 2 ?

Data space for funa()’s

first call

funa(a+b);

funa(a)

b = 1

Data space for funa()’s

second call

Page 21: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Recursion and local variables

• Function funa() is calling itself. Recursion!

funa(1);

main()

funa(a+b);

funa(a)

b = 1

a b a b

Memory: 1 1 2 1

Data space for funa()’s

first call

funa(a+b);

funa(a)

b = 1

Data space for funa()’s

second call

Calling funa() with argument 3

Page 22: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Recursion – points to remember

1. Recursive calls to a function use different areas in the

memory. Local variables for different calls might have

different values!

• For the first call we had a = 1, b = 1. For the second call

we had a = 2, b = 1.

2. There need to be at least one special case which

doesn’t result in calling the recursive function again!

• When will the series of calls to funa() stop in our

previous slide?

• There needs to be some type of selection structure

somewhere in funa, that if its condition is met then

doesn’t call funa anymore, but rather returns to the

calling function.

Page 23: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

When do we use recursion?

If:

One or more simpler cases of the problem have a straight-

forward solution

And other cases can be broken down to smaller

subproblems that are closer to the simple case

then recursive programs can be helpful.

Page 24: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

How do we use recursion?

If:

One or more simpler cases of the problem have a straight-

forward solution

And other cases can be broken down to smaller

subproblems that are closer to the simple case

then recursive programs can be helpful.

• For each simple case return the answer that is already

known

• For each more complicated case, partially solve the

problem and call the recursive function to solve the

remaining part of it.

Page 25: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

How do we use recursion?

• For each simple case return the answer that is already

known

• For each more complicated case, partially solve the

problem and call the recursive function to solve the

remaining part of it.

if this is a simple case

solve it

else

redefine the problem using recursion

Simple case is checked by

the terminating condition

(i.e. no more recursions)

Page 26: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Example – Counting a letter in a string

• Given a string s and a letter c, we want to count how

many times the letter appears in the string.

Page 27: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Example – Counting a letter in a string (page 525)

• Given a string s and a letter c, we want to count how

many times the letter appears in the string.

• Solution:

If the string length is zero then the letter appears 0 times.

If the string length is n (≠0), then

• if the first character is not c, then we can compute

the count by solving the problem with the substring

that starts with our second letter

• If the first character is c, then we can compute the

count by adding 1 to the count in the substring that

starts with our second letter.

Page 28: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Example – Counting a letter in a string (page 525)

int countLetter(char query, char str[])

{

int count = 0;

if (strlen(str) == 0) /* simple case */

count = 0;

else /* redefine using recursion */

{

if (str[0] == query)

count = 1 + countLetter(query, &str[1]);

else

count = 0 + countLetter(query, &str[1]);

}

return count;

}

Page 29: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Example – Counting a letter in a string (page 525)

int main()

{

char str[] = "These are our sentences!";

printf("%d\n", countLetter('s', str));

return 0;

}

Page 30: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Example - factorial

• Write a program that given an integer n, computes n!.

We have written an iterative version of this program

before, now we want to write a recursive version.

Page 31: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Example - factorial

long factorial(int n)

{

long result = 0;

if (n == 0) /* simple case */

result = 1;

else /* redefine using recursion */

result = n * factorial(n - 1);

return result;

}

int main()

{

printf( "%ld\n", factorial(6) );

return 0;

}

Page 32: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Tracing recursive programs

• Printing the input and output of each recursive call and

paying attention to the order of calls can give us an

insight on how recursion works! Let’s trace the factorial

problem:

long factorial(int n)

{

printf("computing %d!\n", n);

long result;

if (n == 0)

result = 1;

else

result = n * factorial(n - 1);

printf("returning result of %d!\n", n);

return result;

}

Page 33: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Tracing recursive programs

• Printing the input and output of each recursive call and

paying attention to the order of calls can give us an

insight on how recursion works! Let’s trace the factorial

problem:

long factorial(int n)

{

printf("computing %d!\n", n);

long result;

if (n == 0)

result = 1;

else

result = n * factorial(n - 1);

printf("returning result of %d!\n", n);

return result;

}

Page 34: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Tracing recursive programs

• Printing the input and output of each recursive call and

paying attention to the order of calls can give us an

insight on how recursion works! Let’s trace the factorial

problem:

long factorial(int n)

{

printf("computing %d!\n", n);

long result;

if (n == 0)

result = 1;

else

result = n * factorial(n - 1);

printf("returning result of %d!\n", n);

return result;

}

Does this remind you

of anything?

Page 35: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

Tracing recursive programs

• C (and many other languages) use stacks for function

calls.

• When a new function is called its variables are pushed

on the variables of the caller function.

• When the function returns, all its variables are popped.

Push 6, push 5, push 4, push 3, push 2,

push 1, push 0.

Pop 0, pop 1, pop 2, pop3, pop 4, pop 5,

pop 6.

Does this remind you

of anything?

Yes, stacks

Page 36: Recursive Programming (I) - eecs.wsu.eduaabnousi/cpts121/lectures/L20_recursion_1.pdf · problem and call the recursive function to solve the ... factorial •Write a program that

References

• J.R. Hanly & E.B. Koffman, Problem Solving and Program

Design in C (8thed.), Pearson, 2016

• Andy O’Fallon’s lecture notes for CptS121

(http://eecs.wsu.edu/~aofallon/cpts121)