Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Towers of Hanoi matlab ( non recursive) I want someone to help me writing the Non recursive Algorithms code for Towers of Hanoi in matlab
Towers of Hanoi matlab ( non recursive)
I want someone to help me writing the Non recursive Algorithms code for Towers of Hanoi in matlab syntax
Here is the code using recursive function if that helps
function Towers of Hanoi (n, initial pea, final pea,temp peg If n=-1 S1 S2 S3 S4 message = [S1, S2,S3, S4 ]; disp (message); - , Move one disk from , ; = num2str(initial-peg); to'i num2str (final-peg); = else % move n-1 disk from peg 1 to peg 2 and use peg 3 as a temporarily % holding peg. Towers_of_Hanoi ( n-1, initial_peg, temp_peg, final_peg) % move the last disk from peg 1 to peg 3. Towers_of_Hanoi ( 1, initial_peg, final_peg ,temp_peg) % move the n-1 disk from peg 2 to peg 3 using peg 1 as the temporarily % holding peg. Towers_of_Hanoi (n-1, temp_peg, final_peg, initial_peg) end end % To get the instruction for 3 disks type Towe rs of Hanoi (3,1,3,2) in your % command window
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started