#1
  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. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    > 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.
    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
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    149
    Rep Power
    139
    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?
  6. #4
  7. 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!
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    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.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. 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.

    where is your link dear ????
    use debugger ????
    i use turbo C and it has trace into in Run menu.....
    but i dont know how it works .....
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    I usually extend chains with a link. Sometimes the trailer safety chains are just TOO short!
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

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

IMN logo majestic logo threadwatch logo seochat tools logo