need help quick

This is a discussion on need help quick within the C Programming forums, part of the General Programming Boards category; i need help quick, how do i get my program to print the total moves????? thank you. here is my ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    18

    need help quick

    i need help quick, how do i get my program to print the total moves????? thank you. here is my code:

    int towers(int, int, int, int);

    int level[3];
    int main()
    {
    /* Gets input from user */
    int n;
    printf("Enter the number of disks on peg # 1: " );
    fflush(stdout);
    scanf("%d",&n);
    printf("\n");
    /* Levels the disks are on or moved to */
    level[0] = n;
    level[1] = 0;
    level[2] = 0;
    printf("Total moves: %d\n", towers(1, 3, 2, n));
    return 0;
    }
    /* Recursion part that determines how and where the disks are moved */
    int towers(int fromTower, int toTower, int auxTower, int n)
    {
    if (n==1) /* For when only 1 disk is entered */
    {
    printf("Move disk 1 from level %d of tower %d to level %d of tower %d\n",level[f
    romTower - 1],fromTower,level[toTower-1] + 1,toTower );
    level[fromTower - 1] -= 1;
    level[toTower - 1] += 1;
    }
    else /* For more than one disk */
    {
    towers(fromTower, auxTower, toTower, n-1);
    printf("Move disk %d from level %d of tower %d to level %d of tower %d\n",n,leve
    l[fromTower - 1],fromTower,level[toTower-1] + 1,toTower );
    level[fromTower - 1] -= 1;
    level[toTower - 1] += 1;
    towers(auxTower, toTower, fromTower, n-1);
    }
    }

  2. #2
    Unregistered
    Guest
    I think you need add a "return" statement that will return the number of moves from each function call. Then, add them number returned from the "lower" call to the number from the "current" call and pass that total to the next "higher" call until finally you return the total to main...?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  2. recursive quick sort - stack overflow
    By Micko in forum C Programming
    Replies: 9
    Last Post: 01-01-2005, 04:51 PM
  3. Questions on basic Quick Sort
    By Weng in forum C++ Programming
    Replies: 4
    Last Post: 12-16-2003, 09:06 AM
  4. Quick Sort Help
    By NavyBlue in forum C Programming
    Replies: 1
    Last Post: 03-02-2003, 09:34 PM
  5. Replies: 0
    Last Post: 04-30-2002, 07:24 PM

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