towers of hanoi problem

This is a discussion on towers of hanoi problem within the C Programming forums, part of the General Programming Boards category; Hi there all, i am relatively new to C programming and was having problems with the syntax in printing the ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    1

    towers of hanoi problem

    Hi there all,
    i am relatively new to C programming and was having problems with the syntax in printing the towers of hanoi.
    I want to print like below, i.e. a three towers with 3 pegs, tower one has one disc on the top, n the number of disc increase by one every peg.
    |- | |
    |-- | |
    |--- | |

    I am using stacks to do this. I think i have the logic right, to do this but can't figure out where am i doing wrong syntactically.
    Can someone please help me out on this?
    I have included the errors as well,
    Thank You

    AIk_21

    #include<stdio.h>

    typedef char Disc;
    typedef char Bar;

    typedef struct peg{
    Bar bar;
    int pegpos;
    Disc dsc[5];
    }Peg;

    typedef struct towernode{
    Peg pegvar;
    struct TowerNode *next;
    }TowerNode;


    typedef struct tower{
    TowerNode *top;
    }Tower;

    void CreateTower(Tower *s);

    TowerNode *MakeNode(Bar item1, int num, Disc item2);
    void Move(int num, Tower *t1, Tower *t2, Tower *t3);

    void TowerPush(int num, Bar item1, Disc item2, Tower *s);

    int N; /*global variable*/

    void CreateTower(Tower *s){
    s->top=NULL;
    }

    TowerNode *MakeNode(Bar item1, int num, Disc item2){
    TowerNode *nodepointer;
    if((nodepointer=malloc(sizeof(TowerNode)))==NULL)
    printf("Out of memory\n");
    else{
    nodepointer->pegvar->bar=item1;
    nodepointer->pegvar->pegpos=num;
    nodepointer->pegvar->dsc[num]=item2;
    }
    return nodepointer;
    }

    /*to be used initially*/
    void TowerPush(int num, Bar item1, Disc item2, Tower *s){
    TowerNode *np=MakeNode(item1, num, item2);
    if(np==NULL)
    printf("Can't push on an non existing node\n");
    else
    {
    np->next=s->top;
    s->top=np;
    }
    }

    void Move(int count, Tower *one, Tower *two, Tower *three)
    {
    if (count > 0) {
    Move(count-1, one->top, two->top, three->top);
    printf("Move a disk from %d to %d.\n", one, three);
    Move(count-1, two->top, three->top, one->top);
    }
    }

    int main(int argc, char *argv[]){
    N=atoi(argv[1]); /*cast from char to int*/

    Tower *t1,*t2,*t3;

    CreateTower(t1);
    CreateTower(t2);
    CreateTower(t3);

    TowerPush(N,'|','_',t1);

    /* for(i=0;i<5;i++)

    {

    printf("%c|%c %c| %c|\n ",t1->peg[i],t1->disc[i],t2,t3);

    }

    Move(N,t1,t2,t3);

    */ return 0;

    }

    "to.c", line 33: warning: improper pointer/integer combination: op "="

    "to.c", line 36: warning: improper pointer/integer combination: op "="

    "to.c", line 37: warning: improper pointer/integer combination: op "="

    "to.c", line 38: warning: improper pointer/integer combination: op "="

    "to.c", line 52: warning: assignment type mismatch:

    pointer to struct TowerNode {} "=" pointer to struct towernode {pointer to struct peg {..} pegvar, pointer to struct TowerNode {..} next}

    "to.c", line 66: warning: argument #2 is incompatible with prototype:

    prototype: pointer to struct tower {pointer to struct towernode {..} top} : "to.c", line 64

    argument : pointer to struct towernode {pointer to struct peg {..} pegvar, pointer to struct TowerNode {..} next}

    "to.c", line 66: warning: argument #3 is incompatible with prototype:

    prototype: pointer to struct tower {pointer to struct towernode {..} top} : "to.c", line 64

    argument : pointer to struct towernode {pointer to struct peg {..} pegvar, pointer to struct TowerNode {..} next}

    "to.c", line 66: warning: argument #4 is incompatible with prototype:

    prototype: pointer to struct tower {pointer to struct towernode {..} top} : "to.c", line 64

    argument : pointer to struct towernode {pointer to struct peg {..} pegvar, pointer to struct TowerNode {..} next}

    "to.c", line 68: warning: argument #2 is incompatible with prototype:

    prototype: pointer to struct tower {pointer to struct towernode {..} top} : "to.c", line 64

    argument : pointer to struct towernode {pointer to struct peg {..} pegvar, pointer to struct TowerNode {..} next}

    "to.c", line 68: warning: argument #3 is incompatible with prototype:

    prototype: pointer to struct tower {pointer to struct towernode {..} top} : "to.c", line 64

    argument : pointer to struct towernode {pointer to struct peg {..} pegvar, pointer to struct TowerNode {..} next}

    "to.c", line 68: warning: argument #4 is incompatible with prototype:

    prototype: pointer to struct tower {pointer to struct towernode {..} top} : "to.c", line 64

    argument : pointer to struct towernode {pointer to struct peg {..} pegvar, pointer to struct TowerNode {..} next}

    "to.c", line 82: syntax error before or at: int

    "to.c", line 84: syntax error before or at: *

    "to.c", line 85: undefined symbol: t1

    "to.c", line 85: warning: improper pointer/integer combination: arg #1

    "to.c", line 86: undefined symbol: t2

    "to.c", line 86: warning: improper pointer/integer combination: arg #1

    "to.c", line 87: undefined symbol: t3

    "to.c", line 87: warning: improper pointer/integer combination: arg #1

    "to.c", line 88: warning: improper pointer/integer combination: arg #4

    cc: acomp failed for to.c

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,420
    Step 1 is read the "READ BEFORE POSTING" Post
    Step 2, edit your post to include code tags
    Step 3, your code is written in C++, yet you claim C - Please clarify which language you wish to program in.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. The Hanoi Towers Recursive
    By helpme1234 in forum C Programming
    Replies: 8
    Last Post: 10-15-2006, 04:41 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM

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