### Thread: Anyone can trace this code for me ????

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
6
Rep Power
0

#### Anyone can trace this code for me ????

hi guys....
this is non-recursive code of h.a.n.o.i tower.......
it work great but i dont understand this code .....
this is my project......please help me if you can .....

#include <stdio.h>

void hanoi(int no, int x, int y)
{
int xstk[100], ystk[100], sstk[100];
int ptr = 0;
int sw = 0;

while (1) {
if (sw == 0 && no > 1) {
xstk[ptr] = x;
ystk[ptr] = y;
sstk[ptr] = sw;
ptr++;
no = no - 1;
y = 6 - x - y;
continue;
}
printf("Moving [%d] from %d to %d.\n", no, x, y);
if (sw == 1 && no > 1) {
xstk[ptr] = x;
ystk[ptr] = y;
sstk[ptr] = sw;
ptr++;
no = no - 1;
x = 6 - x - y;
if (++sw == 2) sw = 0;
continue;
}
do {
if (ptr-- == 0)
return;
x = xstk[ptr];
y = ystk[ptr];
sw = sstk[ptr] + 1;
no++;
} while (sw == 2);
}//end of while
}

int main(void)
{
int n;

printf("Insert number of disks :");
scanf("%d", &n);

hanoi(n, 1, 3);

return (0);
}
2. > this is non-recursive code of h.a.n.o.i tower.......
It surely is

> it work great but i dont understand this code .....
I'm guessing that's because you didn't write it.

> this is my project......please help me if you can .....
Your project is to learn how to use google?

If your course is to learn how to code, then you failed a long time ago - when you first used google to copy/paste your assignment.
3. Originally Posted by caterpillar726
... it work great but i dont understand this code ..... this is my project
What part of the code, do you need to understand?

Originally Posted by caterpillar726
this is my project
So normally you're supposed to be ther person who has written this code, no?
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
6
Rep Power
0
i know that is not prefessional that i used google.
but im beginner on C.
and i just wanna know how this code work ... ok guys ?????
for example :
i dont understand how the first output be : [1] from 1 to 3
i need a guy who trace each line for me

#### Comments on this post

• ptr2void disagrees : So learn C!
5. You could learn to use your debugger (this would involve using your compiler and possibly learning to set compilation flags). You could insert print statements into the program to have it trace itself. Please follow and read the link at my signature to preserve white space in posts.
Code:
```#include <stdio.h>

void hanoi(int no,int x,int y) {
int
xstk[100], ystk[100], sstk[100],
ptr = 0, sw = 0;
for (;;) {
puts("top of forever loop");
if (sw == 0 && no > 1) {
puts("\tsw equals 0 and no exceeds 1");
xstk[ptr] = x;
ystk[ptr] = y;
sstk[ptr] = sw;
ptr++;
no = no - 1;
y = 6 - x - y;
continue;
}
printf("!!!!!!!!!!!!!!!!Moving [%d] from %d to %d.\n", no, x, y);
if (sw == 1 && no > 1) {
puts("\tsw equals 1 and no exceeds 1");
xstk[ptr] = x;
ystk[ptr] = y;
sstk[ptr] = sw;
ptr++;
no = no - 1;
x = 6 - x - y;
if (++sw == 2) sw = 0;
continue;
}
do {
printf("\tdo while loop, sw has value %d\n",sw);
if (ptr-- == 0) {
puts("\t\tfinished!");
return;
}
x = xstk[ptr];
y = ystk[ptr];
sw = sstk[ptr] + 1;
no++;
} while (sw == 2);
}
}

int main(void) {
int n;
printf("Insert number of disks :");
scanf("%d", &n);
hanoi(n, 1, 3);
return 0;
}```
Either way, I'd have you use your compiler.
6. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
6
Rep Power
0
Originally Posted by b49P23TIvg
You could learn to use your debugger (this would involve using your compiler and possibly learning to set compilation flags). You could insert print statements into the program to have it trace itself. Please follow and read the link at my signature to preserve white space in posts.
Code:
```#include <stdio.h>

void hanoi(int no,int x,int y) {
int
xstk[100], ystk[100], sstk[100],
ptr = 0, sw = 0;
for (;;) {
puts("top of forever loop");
if (sw == 0 && no > 1) {
puts("\tsw equals 0 and no exceeds 1");
xstk[ptr] = x;
ystk[ptr] = y;
sstk[ptr] = sw;
ptr++;
no = no - 1;
y = 6 - x - y;
continue;
}
printf("!!!!!!!!!!!!!!!!Moving [%d] from %d to %d.\n", no, x, y);
if (sw == 1 && no > 1) {
puts("\tsw equals 1 and no exceeds 1");
xstk[ptr] = x;
ystk[ptr] = y;
sstk[ptr] = sw;
ptr++;
no = no - 1;
x = 6 - x - y;
if (++sw == 2) sw = 0;
continue;
}
do {
printf("\tdo while loop, sw has value %d\n",sw);
if (ptr-- == 0) {
puts("\t\tfinished!");
return;
}
x = xstk[ptr];
y = ystk[ptr];
sw = sstk[ptr] + 1;
no++;
} while (sw == 2);
}
}

int main(void) {
int n;
printf("Insert number of disks :");
scanf("%d", &n);
hanoi(n, 1, 3);
return 0;
}```
Either way, I'd have you use your compiler.

use debugger ????
i use turbo C and it has trace into in Run menu.....
but i dont know how it works .....
7. I usually extend chains with a link. Sometimes the trailer safety chains are just TOO short!
8. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2012
Posts
6
Rep Power
0
oooooooooops!!!!!!!!!!!!