Recursive Functions amp More Sudoku Lecture 32 EGR 115 Introduction to Computing for Engineers Lecture Outline Lecture 32 EGR 115 Introduction to Computing for Engineers Recursive functions Continue solving the Sudoku game ID: 760519
Download Presentation The PPT/PDF document "EGR 115 Introduction to Computing for En..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
EGR 115 Introduction to Computing for Engineers
Recursive Functions & More Sudoku
Lecture 32
EGR 115 Introduction to Computing for Engineers
Slide2Lecture Outline
Lecture 32
EGR 115 Introduction to Computing for Engineers
Recursive functionsContinue solving the Sudoku game
Slide
2
of 14
Slide3Recursive functions
Lecture 32
EGR 115 Introduction to Computing for Engineers
Slide
3
of 14
Compute:
We can do this with a for or while loop
Let’s do this recursively:
.
.
.
Slide4Recursive functions
Lecture 32
EGR 115 Introduction to Computing for Engineers
Slide 4 of 14
Algorithm:Recursively call the fn Sum(N):if, N > 1sum_N = N + Sum(N-1)else, (N = 1 case)sum_N = 1
if, N > 1else, (N = 1 case)
function [ sum_N ] = Sum( N )if N > 1 sum_N = N + Sum(N-1);else sum_N = 1;endend
function [ sum_N ] = Sum( N ) fprintf('Calling: Sum(%u)\n', N); if N > 1 sum_N = N + Sum(N-1); fprintf(' N = %u and sum_N = %u\n', N, sum_N); else sum_N = 1; endend
Try: Sum(4)
Slide5Recursive functionsTrying Sum(4)
Lecture 32
EGR 115 Introduction to Computing for Engineers
Slide 5 of 14
N=4
function [sum_N] = Sum(4) if N > 1 sum_N = 4 + Sum(3); else sum_N = 1; endend
function [sum_N] = Sum(3) if N > 1 sum_N = 3 + Sum(2); else sum_N = 1; endend
function [sum_N] = Sum(2) if N > 1 sum_N = 2 + Sum(1); else sum_N = 1; endend
function [sum_N] = Sum(1) if N > 1 sum_N = 2 + Sum(1); else sum_N = 1; endend
function [1] = Sum(1) if N > 1 sum_N = 2 + Sum(1); else sum_N = 1; endend
function [3] = Sum(2) if N > 1 sum_N = 2 + 1; else sum_N = 1; endend
function [6] = Sum(3) if N > 1 sum_N = 3 + 3; else sum_N = 1; endend
function [10] = Sum(4) if N > 1 sum_N = 4 + 6; else sum_N = 1; endend
function [sum_N] = Sum(N) if N > 1 sum_N = N + Sum(N-1); else sum_N = 1; endend
N=3
N=2
N=1
Slide6>> Factorial(7)ans = 5040
Recursive functions
Lecture 32
EGR 115 Introduction to Computing for Engineers
Slide 6 of 14
In-Class Example: Develop a recursive function to compute: For example: 5! = 5 x 4 x 3 x 2 x 1Use your function to compute 7!
function [ Factorial_N ] = Factorial( N ) if N > 1 Factorial_N = N * Factorial(N-1); else % N = 1 case Factorial_N = 1; endend
5! = 5 x
(4 x 3 x 2 x 1)
= 5 x 4!
4! = 4 x ( 3 x 2 x 1)
= 4 x 3!
3! = 3 x ( 2 x 1)
= 3 x 2!
2! = 2 x (1)
= 2 x 1!
1
!
= 1
Slide7Continue solving the Sudoku game
Lecture 32
EGR 115 Introduction to Computing for Engineers
Slide 7 of 14
Algorithm:Step A: Determine the set of admissible numbers for every empty cellIf any empty cell has only one admissible number, then, update the confirmed/known elements in theSudoku_array.Repeat until NO more updates occur
Slide8Continue solving the Sudoku game
Lecture 32
EGR 115 Introduction to Computing for Engineers
Slide 8 of 14
Step A alone will NOT solve every Sudoku puzzle
We are now STUCK!!!
Slide9Continue solving the Sudoku game
Lecture 32
EGR 115 Introduction to Computing for Engineers
Slide 9 of 14
We can use our first simple step A recursively“Guess” a candidate from the Valid_Sudoku_set
BAD guess!!
Slide10Next Lecture
Lecture 32
EGR 115 Introduction to Computing for Engineers
Handle Graphics
Slide
10
of 14