LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   this code segment sticks in a loop when it enters the push() function.... (https://www.linuxquestions.org/questions/programming-9/this-code-segment-sticks-in-a-loop-when-it-enters-the-push-function-895881/)

mscoder 08-06-2011 05:34 AM

this code segment sticks in a loop when it enters the push() function....
 
int stack[100] and int top=-1 have been defined globally and "*array_stncnt" accepts pointer to an array of integers of size accepted by variable "count"...thanks in advance for your help...
Code:

void function(int count,int *array_stncnt)
{
        int i,n,index;
        int *p=array_stncnt;
        for(i=0;i<count;i++)
        {
                if(p[i]>=i)
                {
                        push(i);  /*Push called*/
                        push(p[i]);
                }
                else{}

        }

        while(top!=-1)
        {
                n=pop();
                index=pop();
                n=n-index;
                if(n>=index)
                {
                        push(index);
                        push(n);
                }
                else{}
                count++;
        }
        if(count%2==0)
        {
                printf("\nBOB WINS");
        }
        else
        {
                printf("\nALICE WINS");
        }

}



void push(int data)
{

                printf("\Push fn called\n");
                top++;
                stack[top]=data;
                printf("\nThe data aded to stack is %d",stack[top]);
}

int pop()
{
                int data;
                data=stack[top];
                top--;
                return(data);
}


crts 08-06-2011 07:10 AM

This thread is a duplicate of:
http://www.linuxquestions.org/questi...nction-895886/


All times are GMT -5. The time now is 10:53 AM.