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

// this works for odd n #include <iostream> #include<vector> // NOTE: This progr

ID: 3833851 • Letter: #

Question

// this works for odd n #include <iostream> #include<vector> // NOTE: This program uses the vector container from the stl. using namespace std; void main(){ vector<int> t[3]; // three towers A,B,C represented as an array of 3 vectors int n, candidate,to, from, move=0; // move counts the move number cout<<"Please enter an ODD number of rings to move: "; cin>>n; cout<<endl; //intitialize the 3 towers for(int i=n+1;i>=1;i--) t[0].push_back(i); t[1].push_back(n+1); t[2].push_back(n+1); // initialize towers and candidate from=0; to=1; candidate=1; while( t[1].size()<n+1){ // there are still rings to transfer to tower B = t[1] //write it cout<<"move number "<<++move<<": Transfer ring <<candidate<< " from tower "<< char(from+65)<<" to tower "<<char(to+65)<<endl; //do it: move the disks around 1. Push the top of the “from” tower to the “to” tower 2. Remove the ring from the “from” tower //get next “from tower” It’s not the most recent “to” – so which is it? if( ) from=; else from=; // get next “to tower” if( ) to= ; else to= ; //get next candidate candidate= ; // it’s on the top of the current “from tower” } } // this works for odd n #include <iostream> #include<vector> // NOTE: This program uses the vector container from the stl. using namespace std; void main(){ vector<int> t[3]; // three towers A,B,C represented as an array of 3 vectors int n, candidate,to, from, move=0; // move counts the move number cout<<"Please enter an ODD number of rings to move: "; cin>>n; cout<<endl; //intitialize the 3 towers for(int i=n+1;i>=1;i--) t[0].push_back(i); t[1].push_back(n+1); t[2].push_back(n+1); // initialize towers and candidate from=0; to=1; candidate=1; while( t[1].size()<n+1){ // there are still rings to transfer to tower B = t[1] //write it cout<<"move number "<<++move<<": Transfer ring <<candidate<< " from tower "<< char(from+65)<<" to tower "<<char(to+65)<<endl; //do it: move the disks around 1. Push the top of the “from” tower to the “to” tower 2. Remove the ring from the “from” tower //get next “from tower” It’s not the most recent “to” – so which is it? if( ) from=; else from=; // get next “to tower” if( ) to= ; else to= ; //get next candidate candidate= ; // it’s on the top of the current “from tower” } } please help me finish the complete program! Non-recursive solution for Towers of Hanoi Using the algorithm discussed in class, write an iterative program to solve the Towers of Hanoi problem. The problem: You are given three towers a, b, and c. We start with n rings on tower a and we need to transfer them to tower b subject to the following restrictions: l. We can only move one ring at a time, and 2. We may never put a larger numbered ring on top of a smaller numbered One There are always 3 towers. Your program will prompt the user for the number of rings. Here is the algorithm. Definition: A ring is "available" if it is on the top of one of the towers. Definition: The "candidate" is the smallest available ring that has not been moved on the most recent move. The first candidate is ring l. The Algorithm: 1. Find the candidate. 2. Move the candidate (right or left, depending if the number of rings is odd or even) to the closest tower on which it can be placed. Move "around the circle" ifnecessary. 3. If not done, go back to step i. The output should be a set of "commands" of the following form "Move ring x from tower y to tower z" for each move In addition, your program should take 2 -1 moves for any n

Explanation / Answer

#include #include void tower(int a,char from,char aux,char to){ if(a==1){ cout