CS302 - Data Structures using C++

Similar documents
CS302 Data Structures using C++

Lecture 24 Tao Wang 1

Data Structures And Algorithms

Experiment: The Towers of Hanoi. left middle right

Recursion (Rosen, 6 th edition, Section 4.3, 4.4)

What is Recursion? ! Each problem is a smaller instance of itself. ! Implemented via functions. ! Very powerful solving technique.

Recursion. Jordi Cortadella Department of Computer Science

Programming with Recursion. What Is Recursion?

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Chapter 17 - Notes Recursion

Binary Search. the towers of Hanoi recursive C++ code. sorted vectors of numbers fast power function. when not to use recursion memoization

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

VARIABLES & ASSIGNMENTS

Computer Programming

Unit 14. Passing Arrays & C++ Strings

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

Chapter Five: Functions. by Cay Horstmann Copyright 2018 by John Wiley & Sons. All rights reserved

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

Computer Programming. Dr. Fahad Computer Science Deptt.

1 Recursion. 2 Recursive Algorithms. 2.1 Example: The Dictionary Search Problem. CSci 235 Software Design and Analysis II Introduction to Recursion

Data Structures and Algorithms(3)

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 5. The Towers of Hanoi. Divide and Conquer

Exceptions, Case Study-Exception handling in C++.

Algorithm Analysis. CENG 707 Data Structures and Algorithms

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

CSCI 104 Recursion. Sandra Batista & Mark Redekopp by Mark Redekopp, All Rights Reserved

CS200: Recursion and induction (recap from cs161)

20-CS Programming Languages Fall 2016

Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi

Superior University. Department of Electrical Engineering CS-115. Computing Fundamentals. Experiment No.6

05-15/17 Discussion Notes

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CS2141 Software Development using C/C++ C++ Basics

Recursion. ! When the initial copy finishes executing, it returns to the part of the program that made the initial call to the function.

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Recursion. Example 1: Fibonacci Numbers 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,

CS 31 Discussion 1A, Week 8. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

Linked List using a Sentinel

1a Computers, Problem Solving!

Same idea, different implementation. Is it important?

LAB #8. Last Survey, I promise!!! Please fill out this really quick survey about paired programming and information about your declared major and CS.

Reading from and Writing to Files. Files (3.12) Steps to Using Files. Section 3.12 & 13.1 & Data stored in variables is temporary

Chapter 15: Recursion

Recursive Definitions

Summary of basic C++-commands

As an example using arrays, let s write some code to get started with the Upthrust game. We can use a 2D array to represent the game board.

Computer Programming

CS 241 Computer Programming. Introduction. Teacher Assistant. Hadeel Al-Ateeq

EE 368. Weeks 4 (Notes)

Reading 8 : Recursion

Lab Instructor : Jean Lai

Ch 6. Functions. Example: function calls function

Problem Solving: Storyboards for User Interaction

Run Time Environment

Lab 1: Introduction to C++

LAB #8. GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

Sorting & Searching (and a Tower)

C++ Scope Resolution Operator ::

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Lecture 23: Pointer Arithmetic

C++ For Science and Engineering Lecture 12

CmpSci 187: Programming with Data Structures Spring 2015

CS302 - Data Structures using C++

Threads. Threads (continued)

Apllications. March 03, Indian Institute of Space Science and Technology. MA122 - Computer Programming and. Apllications.

CMSC 202 Midterm Exam 1 Fall 2015

EEE2020 Data Structures and Algorithms Abstract Data Types: Stacks and Queues

CSCE Practice Midterm. Data Types

Induction and Recursion. CMPS/MATH 2170: Discrete Mathematics

Below is the simple C program with recursive approach for Towers of Hanoi problem

C C C C++ 2 ( ) C C++ 4 C C

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

MM1_ doc Page E-1 of 12 Rüdiger Siol :21

Review Questions for Final Exam

Fundamentals of Programming. Lecture 19 Hamed Rasifard

CSC 126 FINAL EXAMINATION FINAL Spring 2012 B. Name (last, First) Instructor. Total Possible. Received

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

1.7 Recursion. Department of CSE

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

UNIT 5A Recursion: Basics. Recursion

COMP-202. Recursion. COMP Recursion, 2013 Jörg Kienzle and others

a. a * c - 10 = b. a % b + (a * d) + 7 =

Scientific Computing

Programming Language. Functions. Eng. Anis Nazer First Semester

Week 3: Pointers (Part 2)

Recursion Chapter 17. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Programming challenges in C++

CS 216 Fall 2007 Midterm 1 Page 1 of 10 Name: ID:

Functions. CS111 Lab Queens College, CUNY Instructor: Kent Chin

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

CMPS 221 Sample Final

do { statements } while (condition);

Fundamentals of Programming CS-110. Lecture 2

Odds and Ends. Think about doing research Programming grading. Assignment 3 due Tuesday

Integer Data Types. Data Type. Data Types. int, short int, long int

Transcription:

CS302 - Data Structures using C++ Topic: Kostas Alexis

Introduction to the problem The Towers of Hanoi is a mathematical puzzle where one has three pegs and n disks and the goal is to move the entire stack to another rod, obeying a set of rules. The Rules of the Towers of Hanoi Only one disk may be moved at a time Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack: a disk can only be moved if it is the uppermost disk on a stack. No disk may be placed on top of a smaller disk.

Problem Statement Beginning with n disks on pole A and zero disks on poles B and C, solve towers(n,a,b,c)

Problem Statement Beginning with n disks on pole A and zero disks on poles B and C, solve towers(n,a,b,c) Solution With all disks on A, solve towers(n-1,a,c,b) With the largest disk on pole A and all the others on pole C, solve towers(n-1,a,b,c) With the largest disk on pole B and all the other disks on pole C, solve towers(n- 1,C,B,A)

n+1

n+1

n+1

n+1

n+1

#include <iostream> using namespace std; void solvetowers(int n, char from_rod, char to_rod, char aux_rod) { if (n == 1) { // Move from SRC to DST cout << "Move disk 1 from rod " << from_rod << " to rod " << to_rod << endl; return; } solvetowers(n-1, from_rod, aux_rod, to_rod); // Move from SRC: FROM_ROD to DST: TO_ROD with SPARE: AUX_ROD cout << "Move disk " << n << " from rod " << from_rod << " to rod " << to_rod << endl; } // Move from SRC: AUX_ROD to DST: TO_ROD with SPARE: FROM_ROD solvetowers(n-1, aux_rod, to_rod, from_rod); int main() { int n = 3; // Number of disks solvetowers(n, 'A', 'C', 'B'); // A, B and C are names of rods return 0; }

#include <iostream> using namespace std; void solvetowers(int n, char from_rod, char to_rod, char aux_rod) { if (n == 1) { // Move from SRC to DST cout << "Move disk 1 from rod " << from_rod << " to rod " << to_rod << endl; return; } solvetowers(n-1, from_rod, aux_rod, to_rod); // Move from SRC: FROM_ROD to DST: TO_ROD with SPARE: AUX_ROD cout << "Move disk " << n << " from rod " << from_rod << " to rod " << to_rod << endl; } // Move from SRC: AUX_ROD to DST: TO_ROD with SPARE: FROM_ROD solvetowers(n-1, aux_rod, to_rod, from_rod); int main() { int n = 3; // Number of disks solvetowers(n, 'A', 'C', 'B'); // A, B and C are names of rods return 0; }

#include <iostream> using namespace std; void solvetowers(int n, char from_rod, char to_rod, char aux_rod) { if (n == 1) { // Move from SRC to DST cout << "Move disk 1 from rod " << from_rod << " to rod " << to_rod << endl; return; } Move disk 1 from rod A to rod C Move disk 2 from rod A to rod B Move disk 1 from rod C to rod B Move disk 3 from rod A to rod C Move disk 1 from rod B to rod A Move disk 2 from rod B to rod C Move disk 1 from rod A to rod C solvetowers(n-1, from_rod, aux_rod, to_rod); // Move from SRC: FROM_ROD to DST: TO_ROD with SPARE: AUX_ROD cout << "Move disk " << n << " from rod " << from_rod << " to rod " << to_rod << endl; } // Move from SRC: AUX_ROD to DST: TO_ROD with SPARE: FROM_ROD solvetowers(n-1, aux_rod, to_rod, from_rod); int main() { int n = 3; // Number of disks solvetowers(n, 'A', 'C', 'B'); // A, B and C are names of rods return 0; }

Order of recursive calls that results from solvetowers(3,a,b,c)

Thank you