Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 103 Engineering Programming

Similar presentations


Presentation on theme: "ECE 103 Engineering Programming"— Presentation transcript:

1 ECE 103 Engineering Programming
Chapter 1 Towers of Hanoi Herbert G. Mayer, PSU Status 7/16/2018

2 Syllabus Story Behind The Exact Rules Small Tower with 5 Disks
Hanoi in C Program Output Bibliography

3 Story Behind Towers of Hanoi
Puzzle was invented by French mathematician Édouard Lucas in 1883 The story: An Indian temple contains a large room with three time-worn posts in it, surrounded by 64 golden disks Brahmin priests, acting out the command of an ancient prophecy, have been moving these disks in accordance with the immutable rules of Brahma since that time The puzzle is also known as the Tower of Brahma Puzzle. According to legend, when the last move of the puzzle is completed, the world will end 

4 Story Behind Towers of Hanoi
If the story were true, and if the priests were able to move disks one per second Using the smallest number of moves, then It would take the priests 264 − 1 seconds or roughly 585 billion years to finish Which is about 40 times the age of the universe Don't wait for it to happen, complete your ECE degree first! 

5 The Exact Rules We have a tower of 64 disks at Start, stacked on top of one another There are three places: Start, Buffer, and Goal Initially all 64 discs are stacked on top of one another on the Start pin All disks have different sizes A larger disk may never rest on a smaller disk Goal: The whole tower of 64 disks is to be moved, one disk at a time, from Start, to Goal, using only the 3 places Start, Buffer, and Goal Try it with just 3 or 4 disks to become familiar

6 Small Tower with 5 Disks

7 Hanoi in C #include <stdio.h> void hanoi( int discs,
char* start, char* buff, char* goal ); // Towers with disks int main( void ) { // main for( int d = 1; d < 5; ++d ) { printf( "Moving %d discs.\n", d ); hanoi( d, "start", "buff ", "goal " ); printf( "\n" ); } //end for return 0; } //end main

8 Hanoi in C void hanoi( int discs,
char* start, char* buff, char* goal ) { // hanoi if( discs > 0 ) { hanoi( discs-1, start, goal, buff ); printf( "Move disc %d from %s to %s\n“, discs, start, goal ); hanoi( discs-1, buff, start, goal ); } //end if } //end hanoi

9 Program Output 1 .. 3 Moving 1 discs. Move disc 1 from start to goal
Move disc 1 from start to buff Move disc 2 from start to goal Move disc 1 from buff to goal Moving 3 discs. Move disc 2 from start to buff Move disc 1 from goal to buff Move disc 3 from start to goal Move disc 1 from buff to start Move disc 2 from buff to goal

10 Program Output 4 Moving 4 discs. Move disc 1 from start to buff
Move disc 2 from start to goal Move disc 1 from buff to goal Move disc 3 from start to buff Move disc 1 from goal to start Move disc 2 from goal to buff Move disc 4 from start to goal Move disc 2 from buff to start Move disc 3 from buff to goal

11 Bibliography Wiki: https://en.wikipedia.org/wiki/Tower_of_Hanoi
Mayer, Herbert; Perkins, Don (1984). "Towers of Hanoi Revisited". SIGPLAN Notices. 19 (2): 80–84


Download ppt "ECE 103 Engineering Programming"

Similar presentations


Ads by Google