 We have 3 pegs(1, 2, 3)!the purpose is moving n disks from peg 1 to peg 3 with help of peg 2!  At no time, a larger disk should’nt be placed above.

Slides:



Advertisements
Similar presentations
3/25/2017 Chapter 16 Recursion.
Advertisements

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Recursion, Divide and Conquer Lam Chi Kit (George) HKOI2007.
Use of Computer Technology in Education Course Management Systems Learning Management Systems Classroom Teaching Technology Modeling and Simulation and.
1 CSE1301 Computer Programming Lecture 28 Recursion (Part 2)
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
FIT FIT1002 Computer Programming Unit 20 Recursion.
Review of Recursion What is a Recursive Method? The need for Auxiliary (or Helper) Methods How Recursive Methods work Tracing of Recursive Methods.
Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.
Functions g g Data Flow g Scope local global part 4 part 4.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
Variables, Functions & Parameter Passing CSci 588 Fall 2013 All material not from online sources copyright © Travis Desell, 2011.
CS Final Project 2010 Rohan Paramesh. The Programs  Implementing 3 programs each in C# and Python  Student Scheduler  Assigns courses and students,
Sequential & Object oriented Programming
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 8 Recursion Modified.
Recursion Part 3 CS221 – 2/27/09. Recursion A function calls itself directly: Test() { … Test(); … }
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
Basic Mathematics Chapter 1 (1.2 and 1.3) Weiss. Recursion / Slide 2 Logarithms * Definition: if and only if * Theorem 1.1: n Proof: apply the definition.
11 Project 2 Towers of Hanoi. 22 Towers of Hanoi is a well known puzzle that can be very difficult to solve manually but can be programmed very easily.
 Prentice Hall. All rights reserved. 1 Recursion (Section 7.13 & Exercise7.40 from ed.3) (Sections 6.15, 6.16 from ed.1) Many slides modified.
Basics of Most C++ Programs // Programmer: Clayton Price date: 9/4/ // File: fahr2celc.cpp 03. // Purpose:
Array & Matrix Selected topics. 1. It will compare two adjacent elements, if second element is smaller than the first then it will swap them, if we wanted.
Tower of Hanoi Puzzle Jianying Yu. I. Introduction The Puzzle: Conditions: n disks and three pegs. Conditions: n disks and three pegs. Goal: Move disks.
22C:19 Discrete Math Advanced Counting Fall 2010 Sukumar Ghosh.
1 Examples of Recursion Instructor: Mainak Chaudhuri
Solving Multiplication and Division Equations By
Tuesday, 1 st of Aban 1 st TA Session.  Let’s Meet:  I’m Aideen NasiriShargh, a 83er of CE, a C(++) Lover!  You’re 86ers of EE!  You’re supposed to.
Midterm Review Linear list Stack queue. What is a data structure What is an abstract data type.
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods – Exercises 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
UNIT 17 Recursion: Towers of Hanoi.
1 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
Recursion occurs when a method calls itself. public class RecursionOne { public void run(int x) { System.out.println(x); run(x+1); } public static void.
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.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Review of Recursion What is a Recursive Method?
User Defined Functions
CprE 185: Intro to Problem Solving (using C)
ㅎㅎ Fourth step for Learning C++ Programming Namespace Function
Passing Arguments to a Function
CS41B recursion David Kauchak CS 52 – Fall 2015.
CSC 253 Lecture 8.
Recursive Definitions
CSC 253 Lecture 8.
PC02 Term 2 Test Recursion and Sorting. PC02 Term 2 Test Recursion and Sorting.
Adapted from slides by Marty Stepp, Stuart Reges & Allison Obourn.
CS1120: Recursion.
Pointers & Functions.
Recursion Data Structures.
The Run-Time Stack and Reference Parameters
Review of Recursion What is a Recursive Method?
The Hanoi Tower Problem
CS302 - Data Structures using C++
Recursion When performing a repetitive task either: a loop recursion
CS1201: Programming Language 2
Recursion.
CSC 143 Recursion.
MIPS function continued
Review of Recursion What is a Recursive Method?
Introduction to Algorithms and Programming
Pointers & Functions.
This is my sheet And this is my sheet.
Review of Recursion What is a Recursive Method?
Presentation transcript:

 We have 3 pegs(1, 2, 3)!the purpose is moving n disks from peg 1 to peg 3 with help of peg 2!  At no time, a larger disk should’nt be placed above a smaller disk!:D  Write a program to solve this problem.Use a recursive function with 4 parameters:  The number of disks to be moved  The peg on which these disks are initially placed  The peg to which this stack of disk is to be moved  The peg to be used as a temp holding area

1312321321231313123213212313

a) Move n-1 disks from peg 1 to peg 2, using peg 3 as a temp holding area. b) Move the last disk(the largest) from peg 1 to peg 3. c) Move the n-1 disks from peg 2 to peg 3, using peg 1 as a temp holding area. Now the world is over!:D

#include using namespace std; int mystery(int a, int b); int main(){ int a, b; cout<<"Enter 2 numbers!:"<<endl; cin>>a>>b; cout<<"The result is : "<<mystery(a, b)<<endl; return 0; } int mystery(int a, int b){ if(b == 1) return a; return a + mystery(a, b -1); }

 Can main() be called recursively in your system?!  Take a static variable and increment it!  Then call main() at main()!  What happened?  Think til next session!;) int count; /* count is global */

 Please write your names, Std # and s in a sheet for me.pleas some one accept this task  Feel free ask/suggest anything you want!  Please put “C++ Course::” in the begging of your subjects.  My s again:  

Thanks!