Thread: problem of garbage values

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

    stack problem

    here i know that the array 's' used for stack is providing garbage
    also it is better to implement stack using linklists because of no size limit
    but suppose that i want to implement a fixed size stack
    how do i do it?
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int s[50];
    int max;
    int top = -1,n = 20;
    int i;
    
    int size(void) {
            return n;
    }
    
    void push(int x) {
            if (top==max)
                    printf("Stack overflow\t");
            else
            if (top==max)
    	{}
            else
            {
                    n++;
                    top++;
                    s[top] = x;
            }
    }
    
    int pop(void) {
        int popped = 0;
            if (top==-1)
            {
                    printf("Stack underflow\t");
                    return -1;
            }    
            else
            {
                popped = s[top];
                    n--;
                    top--;
            }
            return popped;
    }
    
    int search(int x) {
            for (i=0;i<=top;i++)
                    if (s[i] == x)
                    return i;
            return -1;
    }
    
    int prints(void)
    {
            if (top==-1)
                    printf("Stack is empty ! ");
    //		{}
            else
            {
                    for (i=0;i<=top;i++)
                            printf("%d",s[i] + " ");
            }    
            printf("\n");
            return 0;
    }
    
    int main(void)
    {
        //just to get a fair idea of what's happening
        printf("STACK ");
        printf("\npush 1\t");    push(1);    prints();
        printf("push 2\t");		 push(2);    prints();
        printf("push 3\t");      push(3);    prints();
        printf("push 4\t");      push(4);    prints();
        printf("push 5\t");      push(5);    prints();
        printf("push 6\t");      push(6);    prints();
        printf("push 7\t");      push(7);    prints();
        printf("push 8\t");      push(8);    prints();
        printf("push 9\t");      push(9);    prints();
        printf("pop\t");         pop();      prints();
        printf("pop\t");         pop();      prints();
        printf("push 9\t");      push(9);    prints();
        printf("push 8\t");      push(8);    prints();
        printf("pop\t");         pop();      prints();
        printf("pop\t");         pop();      prints();
        printf("pop\t");         pop();      prints();
        printf("pop\t");         pop();      prints();
      
        getch();
        return 0;
    }
    OUTPUT :
    STACK
    push 1 4199355
    push 2 Stack overflow 4199355
    push 3 Stack overflow 4199355
    push 4 Stack overflow 4199355
    push 5 Stack overflow 4199355
    push 6 Stack overflow 4199355
    push 7 Stack overflow 4199355
    push 8 Stack overflow 4199355
    push 9 Stack overflow 4199355
    pop Stack is empty !
    pop Stackunderflow Stack is empty !
    push 9 4199363
    push 8 Stack overflow 4199363
    pop Stack is empty !
    pop Stackunderflow Stack is empty !
    pop Stackunderflow Stack is empty !
    pop Stackunderflow Stack is empty !
    Last edited by aldajlo; 10-02-2004 at 02:32 PM. Reason: just like that
    Syra
    Amateur's urge to master C/C++

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
            if (top==max)
                    printf("Stack overflow\t");
            else
            if (top==max)
    	{}
    ...
    If you understand what you're doing, you're not learning anything.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    and this:
    Code:
    printf("%d",s[ i ] + " ");
    I think what you meant to do was:
    Code:
    printf("%d ",s[ i ] );
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    30
    i tried those corrections but only a minor diffrence in output

    OUTPUT :
    STACK
    push 1 1-------------------------------------->here pushes 1
    push 2 Stack overflow 1-------------------> why not here?
    push 3 Stack overflow 1
    push 4 Stack overflow 1
    push 5 Stack overflow 1
    push 6 Stack overflow 1
    push 7 Stack overflow 1
    push 8 Stack overflow 1
    push 9 Stack overflow 1
    pop Stack is empty !------------------------->here it pops it out
    pop Stackunderflow Stack is empty !-----> ok
    push 9 9---------------------------------------->pushes 9
    push 8 Stack overflow 9
    pop Stack is empty !
    pop Stackunderflow Stack is empty !
    pop Stackunderflow Stack is empty !
    pop Stackunderflow Stack is empty !

    it seems that it is considering the stacksize to be 1
    why is it not taking the second element ??
    Syra
    Amateur's urge to master C/C++

  5. #5
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    well what is max... max is never defined...

    this should solve it

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    int s[50];
    int max=49;
    max should be defined as 49 instead of 50 because of the way you are validating your code...

    and whats with this
    Code:
            if (top==max)
                    printf("Stack overflow\t");
            else
            if (top==max)
    	{}
            else
            {
                    n++;
                    top++;
                    s[top] = x;
            }



    Why is there an extra if condition... if(top==max) and else if(top==max) the 2nd condition will never be validated.. i think it should be


    Code:
            if (top==max)
                    printf("Stack overflow\t");
     
            else
            {
                    n++;
                    top++;
                    s[top] = x;
            }
    and what is the variable n doing....

    another suggection.. instead of
    Code:
    if (top==max)
    make max=50 and use
    Code:
    if(top<max)
    because this is a better way of checking it.. what happens due to some logic error if top is made equal to something above 50 your program will never identify it because its always trying to detect overflow by checking if it is only equal to 50.. actually 49
    Last edited by vasanth; 10-02-2004 at 04:36 PM.

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    30

    thanks anyway

    actually i understood that problem after i last posted my message and have rectified it . such a silly mistake!

    well thanks a lot still for providing help

    the output now is

    OUTPUT :
    STACK
    push 1 1
    push 2 1 2
    push 3 1 2 3
    push 4 1 2 3 4
    push 5 1 2 3 4 5
    push 6 1 2 3 4 5 6
    push 7 1 2 3 4 5 6 7
    push 8 1 2 3 4 5 6 7 8
    push 9 1 2 3 4 5 6 7 8 9
    pop 1 2 3 4 5 6 7 8
    pop 1 2 3 4 5 6 7
    push 9 1 2 3 4 5 6 7 9
    push 8 1 2 3 4 5 6 7 9 8
    pop 1 2 3 4 5 6 7 9
    pop 1 2 3 4 5 6 7
    pop 1 2 3 4 5 6
    pop 1 2 3 4 5

    and it's right .
    Now i can use it in my problem.
    thanks vasanth!!!
    u r gr8 & i am ..........
    Syra
    Amateur's urge to master C/C++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. List iteration problem, it resets values
    By Mike Borozdin in forum C++ Programming
    Replies: 4
    Last Post: 11-30-2006, 09:42 AM
  2. Garbage Collection
    By Orborde in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2005, 11:18 PM
  3. linked list problem
    By kzar in forum C Programming
    Replies: 8
    Last Post: 02-05-2005, 04:16 PM
  4. Replies: 1
    Last Post: 02-03-2005, 03:33 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM