Question

Use C/C++/Java/VBasic only for writing the code. II Tower of Hanoi Write a recursive function that...

Use C/C++/Java/VBasic only for writing the code.

II Tower of Hanoi

Write a recursive function that implements the Tower of Hanoi problem. Draw a control flow graph for this function.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include<stdio.h>
static int count=0;
void toh(int p, char from, char to, char a)
{
   if(p==0) return;
if (p == 1)
{
printf(" 1: %c to %c",from,to);
count++;
return;
}
toh(p-1,from,a,to);
printf(" %d: %c to %c",p,from,to);
count++;
   toh(p-1,a,to,from);
}
int main()
{
int n;
printf("How many disks:");
scanf("%d",&n);
toh(n,'A','B','C');
printf(" Total moves:%d",count);
return 0;
}

Output:

Control flow graph for input n=3:

Add a comment
Know the answer?
Add Answer to:
Use C/C++/Java/VBasic only for writing the code. II Tower of Hanoi Write a recursive function that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT