Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Modify the below so that it counts the number of steps, rather than printing all

ID: 3779586 • Letter: M

Question

Modify the below so that it counts the number of steps, rather than printing all the steps. For instance, moving four disks took 15 steps. List the number of steps required for stacks of height one, two, three and so on, as high as you can go (you can probably go to height 30. Any number over 30 will take a lot of computer time.) DO NOT PRINT THE STEPS JUST COUNT THE STEPS. Print a table showing height and number of steps for each height. PROGRAM IN C++

Thanks

***************************************************************************

/*
Run the Tower of Hanoi program using height 3, 4 and 5 and print all the steps for each
height.
*/

#include <iostream>

using namespace std;

int steps(0);

void Hanoi(int m, char a, char b, char c);

void Hanoi(int m, char a, char b, char c)
{
   steps++;
   if (m == 1)
   {
       cout << "Move disc " << m << " from " << a << " to " << c << endl;
   }
   else
   {
       Hanoi(m - 1, a, c, b);
       cout << "Move disc " << m << " from " << a << " to " << c << endl;
       Hanoi(m - 1, b, a, c);
   }
}

int main()
{
   int discs;
   cout << "Enter the number of discs: " << endl;
   cin >> discs;
   Hanoi(discs, 'A', 'B', 'C');
   cout << "It took " << steps << " steps. " << endl;

   system("pause");
}

Explanation / Answer

#include <iostream>
using namespace std;
int steps;
void Hanoi(int m, char a, char b, char c);
void Hanoi(int m, char a, char b, char c)
{
steps++;
if (m == 1)
{
return;
}
else
{
Hanoi(m - 1, a, c, b);
Hanoi(m - 1, b, a, c);
}
}
int main()
{
for(int i=3;i<=30;i++){
       steps=0;
Hanoi(i, 'A', 'B', 'C');
cout << "height= " << i<< " steps= "<<steps << endl;
   }
system("pause");
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote