IN C++ Welcome to the Toothpick Puzzle, where the goal is to get an equal number
ID: 3879856 • Letter: I
Question
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.
Explanation / Answer
PLEASE REFER BELOW CODE
#include<iostream>
using namespace std;
int main()
{
char from,to;
int a,b,c,loop_counter=1;
//given stack
a = 11;
b = 7;
c = 6;
cout<<"Hello"<<endl;
//displaying given stack variables and value
cout<<"Stack: A B C"<<endl;
cout<<"Number of Toothpicks: "<<a<<" "<<b<<" "<<c<<endl;
//while loop for three moves
while(loop_counter < 4)
{
//asking user input from and to of stack
cout<<loop_counter<<". Enter FROM and TO stack letters: ";
cin>>from>>to;
//if from = 'a' and to='c'
if(from == 'a' && to == 'c')
{
//checking a is having sufficient toothpick
if(a > c)
{
//updating a and c values
a = a - c;
c *= 2;
//displaying stack
cout<<"Stack: A B C"<<endl;
cout<<"Number of Toothpicks: "<<a<<" "<<b<<" "<<c<<endl;
}
//if a is not having sufficient toothpic hence displaying error
else
{
cout<<"Sorry, not enough toothpicks in A. Retry..."<<endl;
continue;
}
}
//if from = 'a' and to='b'
else if(from == 'a' && to == 'b')
{
if(a > b)
{
a = a - b;
b *= 2;
cout<<"Stack: A B C"<<endl;
cout<<"Number of Toothpicks: "<<a<<" "<<b<<" "<<c<<endl;
}
else
{
cout<<"Sorry, not enough toothpicks in A. Retry..."<<endl;
continue;
}
}
//if from = 'b' and to='a'
else if(from == 'b' && to == 'a')
{
if(b > a)
{
b = b - a;
a *= 2;
cout<<"Stack: A B C"<<endl;
cout<<"Number of Toothpicks: "<<a<<" "<<b<<" "<<c<<endl;
}
else
{
cout<<"Sorry, not enough toothpicks in B. Retry..."<<endl;
continue;
}
}
//if from = 'b' and to='c'
else if(from == 'b' && to == 'c')
{
if(b > c)
{
b = b - c;
c *= 2;
cout<<"Stack: A B C"<<endl;
cout<<"Number of Toothpicks: "<<a<<" "<<b<<" "<<c<<endl;
}
else
{
cout<<"Sorry, not enough toothpicks in B. Retry..."<<endl;
continue;
}
}
//if from = 'c' and to='a'
else if(from == 'c' && to == 'a')
{
if(c > a)
{
c = c - a;
a *= 2;
cout<<"Stack: A B C"<<endl;
cout<<"Number of Toothpicks: "<<a<<" "<<b<<" "<<c<<endl;
}
else
{
cout<<"Sorry, not enough toothpicks in C. Retry..."<<endl;
continue;
}
}
//if from = 'c' and to='b'
else if(from == 'c' && to == 'b')
{
if(c > b)
{
c = c - b;
c *= 2;
cout<<"Stack: A B C"<<endl;
cout<<"Number of Toothpicks: "<<a<<" "<<b<<" "<<c<<endl;
}
else
{
cout<<"Sorry, not enough toothpicks in C. Retry..."<<endl;
continue;
}
}
else
{
continue;
}
loop_counter++;
}
cout<<"Nope, sorry, that's not it. Try again."<<endl;
cout<<"Ending program.."<<endl;
return 0;
}
PLEASE REFER BELOW OUTPUT
Hello
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: a b
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..
Process returned 0 (0x0) execution time : 33.170 s
Press any key to continue.