Total moves

This is a discussion on Total moves within the C Programming forums, part of the General Programming Boards category; Where and how would I add a part in my program that tells how many moves were done? here is ...

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

    Total moves

    Where and how would I add a part in my program that tells how many moves were done? here is my code:

    #include<stdio.h>
    void towers(int, int, int, int);

    int level[3];
    int main()
    {
    int n;
    printf("Enter the number of disks on peg # 1: " );
    fflush(stdout);
    scanf("%d",&n);
    printf("\n");
    level[0] = n;
    level[1] = 0;
    level[2] = 0;
    towers(1, 3, 2, n);
    return 0;
    }
    void towers(int fromTower, int toTower, int auxTower, int n)
    {
    if (n==1)
    {
    printf("Move disk 1 from level %d of tower %d to level %d of tower %d\n",level[fromTower - 1],fromTower,level[toTower-1] + 1,toTower );
    level[fromTower - 1] -= 1;
    level[toTower - 1] += 1;
    }
    else
    {
    towers(fromTower, auxTower, toTower, n-1);
    printf("Move disk %d from level %d of tower %d to level %d of tower %d\n",n,level[fromTower - 1],fromTower,level[toTower-1] + 1,toTower );
    level[fromTower - 1] -= 1;
    level[toTower - 1] += 1;
    towers(auxTower, toTower, fromTower,
    n-1);
    }
    }

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    You have made no effort to do that....
    cmon ive done 3/4 of it for you already.
    read your other thread for a big hint.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    18
    I tried figuring out how to print the total moves, I tried the following but it always says "Total moves: 1" no matter how many disks i enter.

    #include<stdio.h>
    int towers(int, int, int, int);

    int level[3];
    int main()
    {
    int n;
    printf("Enter the number of disks on peg # 1: " );
    fflush(stdout);
    scanf("%d",&n);
    printf("\n");
    level[0] = n;
    level[1] = 0;
    level[2] = 0;

    printf("Total moves: %d\n", towers(1, 3, 2, n));
    return 0;
    }
    int towers(int fromTower, int toTower, int auxTower, int n)
    {
    int counter = 0;
    if (n==1)
    {
    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 );
    counter++;
    level[fromTower - 1] -= 1;
    level[toTower - 1] += 1;
    }
    else
    {
    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 );
    counter++;
    level[fromTower - 1] -= 1;
    level[toTower - 1] += 1;
    towers(auxTower, toTower, fromTower, n-1);
    return counter;
    }
    }

  4. #4
    Unregistered
    Guest
    # of moves = (2^n) - 1

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with day of the week program
    By Punkakitty in forum C++ Programming
    Replies: 10
    Last Post: 01-14-2009, 05:55 PM
  2. Replies: 8
    Last Post: 11-03-2008, 08:48 PM
  3. long problem
    By loko in forum C Programming
    Replies: 28
    Last Post: 07-22-2005, 09:38 AM
  4. Loops
    By Dt7 in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2004, 06:11 PM
  5. Replies: 4
    Last Post: 04-22-2003, 12:52 PM

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