0

I'm writing some code for the Towers of Hanoi for a homework problem in my Intro to C class. I've got the actual program working but as an addendum, I'd like to have the code print out the number of steps taken i.e. "The total number of steps is 31". I then want to compare them to theoretical number of steps (e.g. steps = (2^n)-1 where n is the number of disks) and be able to verify the program is running correctly for larger values of n.

So far I have been able to implement code to print out the expected number of steps required but I am unsure how to proceed with getting to console to print the actual number of steps.

void towers(int, char, char, char);
int main()
{
    int n;
    printf("Number of disks:");
    scanf("%d", &n);
    int result = (pow(2,n)-1);
    printf("\nExpected number of moves required is %d", result);
    towers(n, 'A', 'C', 'B'); //moving n disks from A to C using B as the intermediate
    return 0;
}

result calls the power function from and returns to expected number of steps before the towers function runs.

void towers(int n, char from_peg, char to_peg, char other_peg)
{
    if (n == 1) //breaking condition
    {
        printf("\n Move disk 1 from peg %c to peg %c", from_peg, to_peg);
        return;
    }
    towers(n-1, from_peg, other_peg, to_peg); //moving n-1 disks from A to B using C as the intermediate
    printf("\n Move disk %d from peg %c to peg %c", n, from_peg, to_peg);
    towers(n-1,other_peg,to_peg,from_peg); //moving n-1 disks from B to C using A as the intermediate
}

The loop returns the solution to the towers puzzle printing the move each time.

Ideally I would like to have some line run after the loop is completely stating "The total number of moves required is (number of steps)" and then compare the expected result to the actual result (i.e. expected step number == actual step number is True).

PC Luddite
  • 5,883
  • 6
  • 23
  • 39
TheBigDig
  • 39
  • 1
  • 4
  • 3
    Easiest would be to add a global `int` variable outside of the `main()` and then in the function `towers()` increment that variable each time you do a move. – Richard Chambers Oct 04 '19 at 12:16
  • you should check out [this answer in - Tower of Hanoi: Recursive Algorithm](https://stackoverflow.com/a/58259294/7541700) – costargc Oct 06 '19 at 17:51

2 Answers2

1

You have a few options. The easiest is to just declare a global variable, initialize it to zero and then increment it every step. Afterwards you just print it.

If you want to avoid global variables, which in general is a good thing, you could pass a variable via a pointer. Something like this:

void towers(int n, char from_peg, char to_peg, char other_peg, int * no_steps)
{
    (*no_steps)++;
    ...

Another way of avoiding globals is using static.

int towers(int n, char from_peg, char to_peg, char other_peg)
{
    static int no_steps = 0;
    no_steps++;
    ...
    return no_steps;
klutt
  • 30,332
  • 17
  • 55
  • 95
  • Thanks! That seems to have worked! My only question now is where do I place printf(no_steps)? – TheBigDig Oct 04 '19 at 13:55
  • Be aware that the static variable means that the outer call of this function from `main` (or wherever) will only return the correct value the first time it is called, because there is no way provided to reset the counter. – Ian Abbott Oct 04 '19 at 16:25
1

One way to do it is to make towers() return the number of steps taken:

int towers(int n, char from_peg, char to_peg, char other_peg)
{
    int steps = 0;

    if (n > 0)
    {
        steps += towers(n-1, from_peg, other_peg, to_peg); //moving n-1 disks from A to B using C as the intermediate
        steps += 1;
        printf("\n Move disk %d from peg %c to peg %c", n, from_peg, to_peg);
        steps += towers(n-1,other_peg,to_peg,from_peg); //moving n-1 disks from B to C using A as the intermediate
    }
    return steps;
}

Usage:

int actual = towers(n, 'A', 'C', 'B');
printf("\nActual number of moves used is %d", actual);

(I kept your usage of putting \n at the start of the printed output, but it is more conventional to put it at the end!)

Ian Abbott
  • 15,083
  • 19
  • 33