0

I need to make a towers of hanoi program using vectors. the code I came up with works fine without the vectors. I have no clue how to make it work with vectors. any help?
thanks

#include <iostream>
#include <vector>
using namespace std;

void towers( vector <int> &disks, int start, int end, int temp)
{
	if( disks == 1 )  // base case
		cout <<start <<"---> "<< end <<"\n";

		else //recursion step
		{
			//move disks - 1 disk from start to temp
		    towers(disks - 1, start, temp, end);
			
			//move last disk from start to end
			cout<<start<< "---> "<< end << "\n";
			
			//move disks - 1 disk from temp to end
			towers(disks - 1, temp, end, start);
			
		} // end else
} // end function towers

int main() 
{
	int n;
    cout<<"Enter number of Discs: ";
	cin >> n;
    towers(n, 1, 3, 2);
    return 0;
}
3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by WaltP
3

wow, 123 views, no response, wow...

No response? Really? What about

Do You Have any idea on how a vector works?

It seems the no response was on your end, bucko.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.