Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IN C++ Welcome to the Toothpick Puzzle, where the goal is to get an equal number of toothpicks in each stack, in three moves. Stack:

IN C++

Welcome to the Toothpick Puzzle, where the goal is to get an equal

number of toothpicks in each stack, in three moves.

Stack: A B C

Number of Toothpicks: 11 7 6

A move consists of moving toothpicks from one stack to a second stack,

where the number of toothpicks moved is exactly the number that is in the

destination stack. In other words, to move from stack B (7 toothpicks)

to stack C (6) as shown above, we would move 6 from B to C, leaving us

with 1 in B and 12 in stack C.

Here we go...

Stack: A B C

Number of Toothpicks: 11 7 6

1. Enter FROM and TO stack letters: a c

Stack: A B C

Number of Toothpicks: 5 7 12

2. Enter FROM and TO stack letters: ab

Sorry, not enough toothpicks in A. Retry...

2. Enter FROM and TO stack letters: ca

Stack: A B C

Number of Toothpicks: 10 7 7

3. Enter FROM and TO stack letters: ab

Stack: A B C

Number of Toothpicks: 3 14 7

Nope, sorry, that's not it. Try again.

Ending program...

Steps TO FOLLOW

Work on your program one step at a time, following your own instincts on what those steps are. If you get stuck then using the following sequence may be helpful:

Create a program with just an int main() function that prints out "Hello" using a cout statement.

Add the header documentation, so you don't forget later!

Add code inside main() to display your ID information (program, class, author, lab, etc). Add code inside main() to display the instructions.

Inside main() declare two char variables that you will use to store which stack to move from, and which stack to move to. Declare three int variables and initialize them to have the starting number of toothpicks for each stack.

Write the code to display the stacks, using the three stack variables. Thus if the values in the variables were to be changed, the output would change as well.

Prompt for user input for the source and destination stacks. Use cin to read in the user input into the two char variables you previously declared to store which stack you are moving from and to.

Create an if statement to check if the from stack is 'A' and the to stack is 'B'. Inside that if statement ensure that the source stack has enough matches. If it doesn't, display an error message. If there are enough matches, make the move by updating the values in the two stacks. Then again display the stacks, using the three stack variables, which should now reflect the move just made.

Write similar code to handle moves between all the other stack combinations.

Put a while loop around the code that makes moves, and have it loop three times. Each time through the loop display the loop counter variable as the move number 1 - 3.

Within each move, when there is an error, use the continue statement to jump back up to the top of the loop to retry a move.

At the end of your program use the stack values to display an appropriate error message.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

From Herds To Insights Harnessing Data Analytics For Sustainable Livestock Farming

Authors: Prof Suresh Neethirajan

1st Edition

B0CFD6K6KK, 979-8857075487

More Books

Students also viewed these Databases questions

Question

Why do mergers and acquisitions have such an impact on employees?

Answered: 1 week ago

Question

2. Describe the functions of communication

Answered: 1 week ago