The Hanoi Towers Recursive

This is a discussion on The Hanoi Towers Recursive within the C Programming forums, part of the General Programming Boards category; Hey guys, ive just been doing this coding but im very lost at the debuggings, just can somone help me, ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    6

    The Hanoi Towers Recursive

    Hey guys, ive just been doing this coding but im very lost at the debuggings, just can somone help me, tell me whats wrong and debug it. I tried the debugging for a bout an hour and as a newbie its hard to tell where my problem is.

    It is suposed to be in recurssion and tel the use what steps to take to solve the hanoi problems.

    Here is the Code

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <ctype.h>
    #include <math.h>
    double Hanoi (int numdisks, int start, int goal, int temp);
    main()
    {
              int num, steps, start, temp, goal, numdisks;
          start = 1;
          temp = 2;
          goal = 3;
    
         printf ("          Welcome T0 Kavi'S Towers Of Hanoi\n");
    
        printf("\nEnter a number to calculate the steps for the Towers Of Hanoi: 
    ");
        scanf("%d",&num);
    
         while (num < 1 || num > 15 )
         {
               printf ("disks Have to be above 0 and under 12 Re Enter 
    Value: ");
               scanf("%d",&num);
         }
         steps =  pow(2,num)- 1 ;
         num = numdisks
        printf("\n Number of steps is: %i ",steps);
         printf("\n Move Disk from Peg %i to Peg %i", Hanoi (numdisks, start, 
    goal, temp);
    
    
       fflush(stdin);
        getch();
        return 0;
    }
    {
         Hanoi (numrings,start,goal,temp)
               if numrings = 1
               printf ("move a disk from "start" to "goal")
               else
               Hanoi (numrings-1, start, temp, goal)
               Hanoi (numrings 1, start, goal, temp)
               Hanoi (numrings-1, temp, goal, start)
               }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Don't use fflush(stdin), see the FAQ for why.

    Code:
    printf("\n Move Disk from Peg %i to Peg %i", Hanoi (numdisks, start, 
    goal, temp);
    You're missing a closing parenthesis ).

    Code:
    if numrings = 1
    This isn't BASIC.
    Code:
    if(numrings == 1)
    You're missing curly braces around the Hanoi function . . . or rather they're in the wrong place. (Might want to look at the function tutorial, near here.) You're also missing lots of semicolons.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Look up tutorials on if statements, I have never seen people put a space before the parameters of a function in C or C++. Put a return statement on the Hanoi function. Don't fflush(stdin).

    Oh, and learn decent indenting.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    6
    does any one acutally have the recursive to the Hanoi Towers
    so i just have like a basic thing to look at....please and thanks

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    > does any one acutally have the recursive to the Hanoi Towers
    Probably, but that's not the point.

    If you read some of the previous posts, and spent some of the last 18 hours or so just fixing the syntax errors in your code (or at least posting your progress so far), then perhaps we could help you to making YOUR answer work.

    You're basically on the right track, there's just some detail for you to try and fix.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    6
    I continued working on it, still some flaws some errors bout the pow statement can somone jus clear up some of the mistakes



    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <ctype.h>
    #include <math.h>
    double Hanoi (int numdisks, int start, int goal, int temp);
    main()
    {
              int num, steps, start, temp, goal, numdisks;
          start = 1;
          temp = 2;
          goal = 3;
    
         printf ("          Welcome T0 Kavi'S Towers Of Hanoi\n");
    
        printf("\nEnter a number to calculate the steps for the Towers Of Hanoi: ");
        scanf("%d",&num);
    
         while (num < 1 || num > 15 )
         {
               printf ("Factorials Have to be above 0 and under 12 Re Enter Value: ");
               scanf("%d",&num);
         }
         steps =  pow(2,num)- 1 ;
         num = numdisks
        printf("\n Number of steps is: %i ",steps);
         printf("\n Move Disk from Peg %i to Peg %i", Hanoi (numdisks, start, goal, temp);
    
    
       fflush(stdin);
        getch();
        return 0;
    }
    {
         Hanoi (numrings,start,goal,temp);
               if (numrings = 1);
               printf ("move a disk from "start" to "goal");
               else;
               Hanoi (numrings-1, start, temp, goal);
               Hanoi (numrings 1, start, goal, temp);
               Hanoi (numrings-1, temp, goal, start);
               }

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,422
    I'm wondering how you managed to get this far when the basic mechanics of writing C are beyond you.

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <ctype.h>
    #include <math.h>
    
    /*!! why exactly are you returning a double here? */
    double Hanoi(int numdisks, int start, int goal, int temp);
    
    /*!! always say main returns int */
    int main()
    {
        int num, steps, start, temp, goal, numdisks;
        start = 1;
        temp = 2;
        goal = 3;
    
        printf("          Welcome T0 Kavi'S Towers Of Hanoi\n");
    
        printf("\nEnter a number to calculate the steps for the Towers Of Hanoi: ");
        scanf("%d", &num);
    
        while (num < 1 || num > 15) {
            /*!! your instructions and code do not agree */
            printf("Factorials Have to be above 0 and under 12 Re Enter Value: ");
            scanf("%d", &num);
        }
        steps = pow(2, num) - 1;
        num = numdisks;/*!! missing ; */
    
        printf("\n Number of steps is: %i ", steps);
    
        /*!! 1. This printf() is missing a closing ) */
        /*!! 2. The number of parameters does not match the number of conversions */
        /*!! 3. The types are wrong - you said Hanoi() returns a double */
        printf("\n Move Disk from Peg %i to Peg %i",
               Hanoi(numdisks, start, goal, temp);
    
        /*!! we already said this was bad, you still use it */
        fflush(stdin);
        getch();
        return 0;
    }
    
    { /*!! what is this brace doing here? */
    
    /*!! why have to dropped back to implicitly declaring the return type? */
           Hanoi(numrings, start, goal, temp);  /*!! where is the { ? */
           if (numrings = 1);   /*!! use == for comparison, and drop the ; */
           /*!! you already used printf successfully above, this isn't good at all */
           printf("move a disk from " start " to " goal ");
               else;/*!! more stray ; and no {} */
               Hanoi (numrings-1, start, temp, goal);
               Hanoi (numrings 1, start, goal, temp);   /*!! maybe -1 ? */
               Hanoi (numrings-1, temp, goal, start);
               }
    Lots of comments - follow the !!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Salem you are far too kind... I would have just told him to take it to his teacher or to one of the tutors that are offered for most comp sci courses (even the small cheapo(SIC) colleges off that in my area)

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    6
    Um TO mr Wraithan .....or wtv
    Im in second year high school not college
    im just interested in steppin forwards
    but thanks for the compliment n thinking im in college'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Iterative Towers of Hanoi
    By Mister C in forum C++ Programming
    Replies: 10
    Last Post: 04-11-2009, 01:11 AM
  2. towers of hanoi - what is wrong with this code?
    By kanesoban in forum C Programming
    Replies: 4
    Last Post: 09-17-2007, 01:20 PM
  3. Towers of Hanoi (need help)
    By Loudan in forum C++ Programming
    Replies: 3
    Last Post: 01-30-2006, 09:17 PM
  4. Towers of Hanoi, special output.
    By spoon_ in forum C Programming
    Replies: 3
    Last Post: 03-15-2003, 05:08 PM
  5. Towers of Hanoi
    By janehung in forum C Programming
    Replies: 12
    Last Post: 01-07-2002, 05:40 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21