Problem Solving with C++ (9th Edition)
9th Edition
ISBN: 9780133591743
Author: Walter Savitch
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 14, Problem 5PP
Program Plan Intro
Towers of Hanoi
Program plan:
- Include required header file.
- Declare the function for tower of Hanoi.
- Define main function.
- Declare variable for “n”.
- Initializes three post names in “char” data type.
- Create prompt statement for disks.
- Read the number of disks from user.
- Call the function “towersOfHanoi” with four parameters “n”, “post1”, “post2” and “post3”.
- Define “towersOfHanoi” function
- If the total count is equal to “1”, then display the movement of disk from given source post to destination post.
- Otherwise, recursively call the function “towersOfHanoi” with corresponding arguments.
Expert Solution & Answer
Trending nowThis is a popular solution!
Students have asked these similar questions
A common memory matching game played by young children is to start with a deck of cards that contain identical pairs. For example, given six cards in the deck, two might be labeled 1, two labeled 2, and two labeled 3. The cards are shuffled and placed face down on the table. A player then selects two cards that are face down, turns them face up, and if the cards match, they are left face up. If the two cards do not match, they are returned to their original face down position. The game continues until all cards are face up. Write a program that plays the memory matching game. Use 16 cards that are laid out in a 4 4 square and are labeled with pairs of numbers from 1 to 8. Your program should allow the player to specify the cards that he or she would like to select through a coordinate system. For example, in the following layout: 1 2 3 4 1 8 * * * 2 * * * * 3 * 8 * * 4 * * * * all of the face down cards are indicated by *. The pairs of 8 that are face up are at coordinates (1,1) and…
The puzzle called the Towers of Hanoi consists of three pegs, one of which contains several rings stacked in order of descending diameter from bottom to top.
The problem is to move the stack of rings to another peg. You are allowed to move only one ring at a time, and at no time is a ring to be placed on top of a smaller one. Observe that if the puzzle involved only one ring, it would be extremely easy.
Moreover, when faced with the problem of moving several rings, if you could move all but the largest ring to another peg, the largest ring could then be placed on the third peg, and then the problem would be to move the remaining rings on top of it.
Using this observation, develop a recursive algorithm for solving the Towers of Hanoi puzzle for an arbitrary number of rings.
Children often play a memory game in which a deck of cards containing
matching pairs is used. The cards are shuffled and placed face down on a table. The players then take turns and select two cards at a time. If both cards match, they are left face up; otherwise, the cards are placed face down at the same positions. Once the players see the selected pair of cards and if the cards do not match, then they can memorize the cards and use their memory to select the next pair of cards. The game continues until all the cards are face up. Write a program to play the memory game. Use a two dimensional array of 4 rows and 4 columns to use a deck of 16 cards with 8 matching pairs and you can use numbers 1 to 8 to mark the cards. (If you use a 6 by 6 array, then you will need 18 matching pairs, and so on.) Use random number generators to randomly store the pairs in the array. Use appropriate functions in your program, and the main program should be merely a call to functions.
Extra credit:…
Chapter 14 Solutions
Problem Solving with C++ (9th Edition)
Ch. 14.1 - Prob. 1STECh. 14.1 - Prob. 2STECh. 14.1 - Prob. 3STECh. 14.1 - Prob. 4STECh. 14.1 - Prob. 5STECh. 14.1 - If your program produces an error message that...Ch. 14.1 - Write an iterative version of the function cheers...Ch. 14.1 - Write an iterative version of the function defined...Ch. 14.1 - Prob. 9STECh. 14.1 - Trace the recursive solution you made to Self-Test...
Ch. 14.1 - Trace the recursive solution you made to Self-Test...Ch. 14.2 - What is the output of the following program?...Ch. 14.2 - Prob. 13STECh. 14.2 - Redefine the function power so that it also works...Ch. 14.3 - Prob. 15STECh. 14.3 - Write an iterative version of the one-argument...Ch. 14 - Prob. 1PCh. 14 - Prob. 2PCh. 14 - Write a recursive version of the search function...Ch. 14 - Prob. 4PCh. 14 - Prob. 5PCh. 14 - The formula for computing the number of ways of...Ch. 14 - Write a recursive function that has an argument...Ch. 14 - Prob. 3PPCh. 14 - Prob. 4PPCh. 14 - Prob. 5PPCh. 14 - The game of Jump It consists of a board with n...Ch. 14 - Prob. 7PPCh. 14 - Prob. 8PP
Knowledge Booster
Similar questions
- Python Please. An interesting puzzler for chess buffs is the Knight’s Tour problem, originally proposed by the mathematician Euler. Can the knight piece move around an empty chessboard and touch each of the 64 squares once and only once? We study this intriguing problem in depth here. The knight makes only L-shaped moves (two spaces in one direction and one space in a perpendicular direction). Thus, as shown in the figure below, from a square near the middle of an empty chessboard, the knight (labeled K) can make eight different moves (numbered 0 through 7). A: Draw an eight-by-eight chessboard on a sheet of paper, and attempt a Knight’s Tour by hand. Put a 1 in the starting square, a 2 in the second square, a 3 in the third, and so on. Before starting the tour, estimate how far you think you’ll get, remembering that a full tour consists of 64 moves. How far did you get? Was this close to your estimate? B: Now let’s develop a script that will move the knight around a chessboard…arrow_forwardChildren often play a memory game in which a deck of cards containing matching pairs is used. The cardsare shuffled and placed face down on a table. The players then take turns and select two cards at a time.If both cards match, they are left face up; otherwise, the cards are placed face down at the same positions.Once the players see the selected pair of cards and if the cards do not match, then they can memorize thecards and use their memory to select the next pair of cards. The game continues until all the cards areface up. Write a program to play the memory game. Use a two-dimensional array of 4 rows and 4 columnsfor a deck of 16 cards with 8 matching pairs. You can use numbers 1 to 8 to mark the cards. (If you use a6 by 6 array, then you will need 18 matching pairs, and so on.) Use random number generators torandomly store the pairs in the array. Use appropriate functions in your program, and the main programshould be merely a call to functions. I need to figure out how to do…arrow_forwardSudoku is a popular logic puzzle that uses a 9 by 9 array of squares that are organized into 3 by 3 subarrays. The puzzle solver must fill in the squares with the digits 1 to 9 such that no digit is repeated in any row, any column, or any of the nine 3 by 3 subgroups of squares. Initially, some squares are filled in already and cannot be changed. For example, the following might be a starting configuration for a Sudoku puzzle: Create a class SudokuPuzzle.java Download SudokuPuzzle.java that has the attributes • board—a 9 by 9 array of integers that represents the current state of the puzzle, where 0 indicates a blank square • start—a 9 by 9 array of boolean values that indicates which squares in board are given values that cannot be changed and the following methods: • SudokuPuzzle—a constructor that creates an empty puzzle • toString—returns a string representation of the puzzle that can be printed • addInitial(row, col, value)—sets the given square to the given value as an…arrow_forward
- In computer science and mathematics, the Josephus Problem (or Josephus permutation) is a theoretical problem. Following is the problem statement: There are n people standing in a circle waiting to be executed. The counting out begins at some point (rear) in the circle and proceeds around the circle in a fixed direction. In each step, a certain number (k) of people are skipped and the next person is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given freedom. Given the total number of persons n and a number k which indicates that k-1 persons are skipped and kth person is killed in circle. The task is to choose the place in the initial circle so that you are the last one remaining and so survive. For example, if n = 5 and k = 2, then the safe position is 3. Firstly, the person at position 2 is killed, then person at position 4 is killed, then person at position 1…arrow_forwardThe classic Eight Queens puzzle is to place eight queens on a chessboard such that no two queens can attack each other (i.e., no two queens are in the same row, same column, or same diagonal). There are many possible solutions. Write a program that displays one such solution.arrow_forwardWrite a program that prints an mxn matrix whose dimensions are specified by the user. Let the matrix values be random variables. You must use it within the repetition cycle. Example format: Enter dimension of matrix mxn: 2 4 The 2x4 matrix is : 1 2 3 4 5 6 7 8arrow_forward
- haskell is the programming languagearrow_forwardUse C++ language Can you please give me a code that runs properly this time Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'.A region is captured by flipping all 'O's into 'X's in that surrounded region.For example, for the following board,X X X XX O O XX X O XX O X XAfter capturing all regions, the board becomesX X X XX X X XX X X XX O X XThe algorithm should take an input file, named “input.txt”, and output the resulting board in the console.Sample input.txt6X X X X X OX O X O O XX X O O O XX X X O X XX O X X O OX X X X X XThe first line is a number indicating the size of the board; in the above example, 6 means the board is .Output for in console:X X X X X OX X X X X XX X X X X XX X X X X XX X X X O OX X X X X Xarrow_forwardThe game of chess was invented a few hundred years ago in India. The story has it, that the ruler of the area was so enchanted with the game, that he called the inventor to his palace, and asked him to name a gift. The seemingly humble man asked the ruler to put a grain of rice on the first square of the chessboard, two grains of rice on the second and so on, doubling the grains each time until all 64 squares of the chessboard were filled. The ruler was thinking about a full sack of rice and happily agreed. I didn't count it myself, but there are 32,000,000 grains of rice in a short ton (2,000 lbs). So do the calculation in Python and make a modern day comparison. Assume that a 50 foot rail car can carry 50 tons of rice. Write a program that would calculate how long the train would have be to carry the inventor's request? First Calculation: How many grain of rice? Second Calculation: How many tons of rice? Third Calculation: How many train car will be needed? BONUS(3pts) If your…arrow_forward
- A 2D matrix can be represented as a list and a column count value in Python. For example, the 3x3 matrix 1 2 34 5 67 8 9 can be row-wise represented as ([1,2,3,4,5,6,7,8,9], 3), where the number 3 represents the number of columns in the matrix. Similarly, 1 3 52 4 6 becomes ([1,3,5,2,4,6], 3). A submatrix can be defined as an (l,r,t,b) tuple, where l and r are left and right column indices, and t and b are top and bottom row indices (all inclusive). Write a function that takes a tuple containing the list representing a matrix, and the column count of the matrix, along with another tuple representing a specific submatrix, and returns the list representation of the submatrix along with its column count as a tuple. For example, given submatrix(([1,2,3,4,5,6,7,8,9,10,11,12], 4), (1,2,0,1)) returns: ([2,3,6,7], 2) because, ([1,2,3,4,5,6,7,8,9,10,11,12], 4) represents: 1 2 3 45 6 7 89 10 11 12 and (1,2,0,1) represents the submatrix between column indices 1 and 2 (both inclusive), and row…arrow_forwardImplement a program that reads two integers (n and k). The program must output the binomial coefficient as follows: Print every coefficient that is before binomial coefficient for given k; Example: Insert n: 5 Insert k: 3 Result: 10 Coefficient-array: 1 5 10 10 Print Pascal Triangle for the given n: Example (n=5): 1 11 121 1331 14641 15 10 10 5 1arrow_forwardimplement C# code to create a list of Card objects, add two Card objects to the list, use a loop to flip the cards in the list and print it.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education