CS1010: Programming Methodology

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

Chapter 20 Recursion.
3/25/2017 Chapter 16 Recursion.
Slide 1 Insert your own content. Slide 2 Insert your own content.
By D. Fisher Geometric Transformations. Reflection, Rotation, or Translation 1.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
0 - 0.
ALGEBRAIC EXPRESSIONS
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
MULTIPLYING MONOMIALS TIMES POLYNOMIALS (DISTRIBUTIVE PROPERTY)
ADDING INTEGERS 1. POS. + POS. = POS. 2. NEG. + NEG. = NEG. 3. POS. + NEG. OR NEG. + POS. SUBTRACT TAKE SIGN OF BIGGER ABSOLUTE VALUE.
MULTIPLICATION EQUATIONS 1. SOLVE FOR X 3. WHAT EVER YOU DO TO ONE SIDE YOU HAVE TO DO TO THE OTHER 2. DIVIDE BY THE NUMBER IN FRONT OF THE VARIABLE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
© 2006 Pearson Education Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis, William.
Tower of Hanoi Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in The game starts by having few discs stacked.
The Towers of Hanoi or Apocalypse When?.
Chapter Objectives To learn about recursive data structures and recursive methods for a LinkedList class To understand how to use recursion to solve the.
The Tower Of Hanoi Edouard Lucas
CSED101 INTRODUCTION TO COMPUTING GREEDY APPROACH, DIVIDE AND CONQUER Hwanjo Yu.
Chapter 17 Recursion.
Introduction to Algorithms
Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.
ABC Technology Project
TV Show Trivia Princeton Puzzle Hunt Do NOT flip over your answer sheet!
More Recursion: Permutations and Towers of Hanoi COP 3502.
O X Click on Number next to person for a question.
© S Haughton more than 3?
Twenty Questions Subject: Twenty Questions
Squares and Square Root WALK. Solve each problem REVIEW:
Past Tense Probe. Past Tense Probe Past Tense Probe – Practice 1.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 7: Recursion Java Software Structures: Designing and Using.
Addition 1’s to 20.
25 seconds left…...
EC-111 Algorithms & Computing Lecture #11 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Chapter 8: Recursion Java Software Solutions
Test B, 100 Subtraction Facts
11 = This is the fact family. You say: 8+3=11 and 3+8=11
Week 1.
We will resume in: 25 Minutes.
WEEK 12 Class Activities Lecturer’s slides.
O X Click on Number next to person for a question.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
CS1010 Programming Methodology
WEEK 13 Class Activities Lecturer’s slides.
Towers of Hanoi
By Senem Kumova Metin 1 DATA TYPES. by Senem Kumova Metin 2 DATA TYPE? …… x; // DECLARATION OF VARIABLE X printf(“Do you want to go on? \n”) printf(“Please.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
UNIT 17 Recursion: Towers of Hanoi.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
Tower of Hanoi Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in The game starts by having few discs stacked.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
1 Towers of Hanoi Three pegs, one with n disks of decreasing diameter; two other pegs are empty Task: move all disks to the third peg under the following.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Recursion.
Recursion.
Pramada Ramachandran, Assistant Professor, Department of Mathematics,
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Java Software Structures: John Lewis & Joseph Chase
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Definitions
Chapter 12 Recursion (methods calling themselves)
ECE 103 Engineering Programming
The Hanoi Tower Problem
Presentation transcript:

CS1010: Programming Methodology

Week 11 Self-reading: Towers of Hanoi Objectives: Understand that many problems are more naturally solved with recursion, which can provide elegant solutions. Taste the classic example of recursion: Towers of Hanoi. Week CS1010 (AY2013/4 Semester 1) Remark: The contents here are non-examinable.

12. Towers of Hanoi (1/17) This classical Towers of Hanoi puzzle has attracted the attention of computer scientists more than any other puzzles. Invented by Edouard Lucas, a French mathematician, in1883. There are 3 pegs (A, B and C) and a tower of n disks on the first peg A, with the smallest disk on the top and the biggest at the bottom. The purpose of the puzzle is to move the whole tower from peg A to peg C, with the following simple rules: Only one disk (the one at the top) can be moved at a time. A bigger disk must not rest on a smaller disk. Week CS1010 (AY2013/4 Semester 1)

12. Towers of Hanoi (2/17) Demo: Tower of HanoiTower of Hanoi We attempt to write a program to produce instructions on how to move the disks from peg A to peg C to complete the puzzle. Example: A tower with 3 disks. Output produced by program: Move disk from A to C Move disk from A to B Move disk from C to B Move disk from A to C Move disk from B to A Move disk from B to C Move disk from A to C Week CS1010 (AY2013/4 Semester 1)

12. Towers of Hanoi (3/17) Example: A tower with 3 disks. Move disk from A to C Move disk from A to B Move disk from C to B Move disk from A to C Move disk from B to A Move disk from B to C Move disk from A to C ABC Week CS1010 (AY2013/4 Semester 1)

12. Towers of Hanoi (4/17) Example: A tower with 3 disks. Move disk from A to C Move disk from A to B Move disk from C to B Move disk from A to C Move disk from B to A Move disk from B to C Move disk from A to C ABC Week CS1010 (AY2013/4 Semester 1)

12. Towers of Hanoi (5/17) Example: A tower with 3 disks. Move disk from A to C Move disk from A to B Move disk from C to B Move disk from A to C Move disk from B to A Move disk from B to C Move disk from A to C ABC Week CS1010 (AY2013/4 Semester 1)

12. Towers of Hanoi (6/17) Example: A tower with 3 disks. Move disk from A to C Move disk from A to B Move disk from C to B Move disk from A to C Move disk from B to A Move disk from B to C Move disk from A to C ABC Week CS1010 (AY2013/4 Semester 1)

12. Towers of Hanoi (7/17) Example: A tower with 3 disks. Move disk from A to C Move disk from A to B Move disk from C to B Move disk from A to C Move disk from B to A Move disk from B to C Move disk from A to C ABC Week CS1010 (AY2013/4 Semester 1)

12. Towers of Hanoi (8/17) Example: A tower with 3 disks. Move disk from A to C Move disk from A to B Move disk from C to B Move disk from A to C Move disk from B to A Move disk from B to C Move disk from A to C ABC Week CS1010 (AY2013/4 Semester 1)

12. Towers of Hanoi (9/17) Example: A tower with 3 disks. Move disk from A to C Move disk from A to B Move disk from C to B Move disk from A to C Move disk from B to A Move disk from B to C Move disk from A to C ABC Week CS1010 (AY2013/4 Semester 1)

12. Towers of Hanoi (10/17) Example: A tower with 3 disks. Move disk from A to C Move disk from A to B Move disk from C to B Move disk from A to C Move disk from B to A Move disk from B to C Move disk from A to C ABC Voilà! Week CS1010 (AY2013/4 Semester 1)

12. Towers of Hanoi (11/17) Can be interpreted as: 1. move four disks from peg A to peg B 2. move disk 5 from peg A to peg C 3. move four disks from peg B to peg C Week CS1010 (AY2013/4 Semester 1)

12. Towers of Hanoi (12/17) But how do we execute step 1? Or step 3? Step 3 can be interpreted as: 3.1 move three disks from peg B to peg A 3.2 move disk 4 from peg B to peg C 3.3 move three disks from peg A to peg C Towers after steps 1 and 2: Week CS1010 (AY2013/4 Semester 1)

12. Towers of Hanoi (13/17) Can you start to visualise how to solve this using recursion? Towers after steps 1, 2, 3.1 and 3.2: Week CS1010 (AY2013/4 Semester 1)

12. Towers of Hanoi (14/17) Algorithm: if (n > 0) move n – 1 disks from the source peg to the temp peg using the dest peg move disk n from the source peg to the dest peg move n – 1 disks from the temp peg to the dest peg using the source peg Week CS1010 (AY2013/4 Semester 1)

12. Towers of Hanoi (15/17) #include void tower(char, char, char, int); int main(void) { int disks; printf("Number of disks: "); scanf("%d", &disks); tower('A','B','C', disks); return 0; } // Display instructions for moving n disk from source to dest // using temp as an auxiliary. Disks are numbered 1 to n // (smallest to largest). void tower(char source, char temp, char dest, int n) { if (n > 0) { tower(source, dest, temp, n-1); printf("Move disk %d from peg %c to peg %c\n", n, source, dest); tower(temp, source, dest, n-1); } Week11_TowersOfHanoi.c Week CS1010 (AY2013/4 Semester 1)

12. Towers of Hanoi (16/17) Trace of tower('A', 'B', 'C', 3); tower('A', 'B', 'C', 3); source is 'A' temp is 'B' dest is 'C' n is 3 tower ('A', 'C', 'B', 2) move 3 from A to C tower ('B', 'A', 'C', 2) source is 'A' temp is 'C' dest is 'B' n is 2 tower ('A', 'B', 'C', 1) move 2 from A to B tower ('C', 'A', 'B', 1) source is 'B' temp is 'A' dest is 'C' n is 2 tower ('B', 'C', 'A', 1) move 2 from B to C tower ('A', 'B', 'C', 1) source is 'A' temp is 'B' dest is 'C' n is 1 tower ('A', 'C', 'B', 0) move 1 from A to C tower ('B', 'A', 'C', 0) source is 'C' temp is 'A' dest is 'B' n is 1 tower ('C', 'B', 'A', 0) move 1 from C to B tower ('A', 'C', 'B', 0) source is 'B' temp is 'C' dest is 'A' n is 1 tower ('B', 'A', 'C', 0) move 1 from B to A tower ('C', 'B', 'A', 0) source is 'A' temp is 'B' dest is 'C' n is 1 tower ('A', 'C', 'B', 0) move 1 from A to C tower ('B', 'A', 'C', 0) Week CS1010 (AY2013/4 Semester 1)

12. Towers of Hanoi (17/17) Output generated by tower('A', 'B', 'C', 3); Move disk 1 from peg A to peg C Move disk 2 from peg A to peg B Move disk 1 from peg C to peg B Move disk 3 from peg A to peg C Move disk 1 from peg B to peg A Move disk 2 from peg B to peg C Move disk 1 from peg A to peg C Week CS1010 (AY2013/4 Semester 1)

End of File