LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-06-2011, 05:51 AM   #1
mscoder
LQ Newbie
 
Registered: Jun 2011
Posts: 18

Rep: Reputation: Disabled
this code segment sticks in a loop when it enters the push() function....


int stack[100] and int top=-1 have been defined globally...thanks in advance for your help...
Code:
#include<stdio.h>



int pop();
void push(int);
void function(int,int *);
int stack[1000];
int top=-1;
int count=0;


int main()
{
        int t;

        int pile_count[100];
        int stone_count[100];
        printf("\nEnter the number of test cases");
        scanf("%d",&t);
        int i,j;
        for (i=0;i<t;i++)
        {
                count=0;


                scanf("\n%d",&pile_count[i]);


                for(j=0;j<pile_count[i];j++)
                {

                        scanf(" %d",&stone_count[j]);

                }

        function(pile_count[i],stone_count);
        }

     
        return(0);
}

void function(int count1,int *array_stncnt)
{ 
        int i,n,index;
        int *p=array_stncnt;
        for(i=0;i<count1;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);
}

Last edited by mscoder; 08-06-2011 at 07:14 AM.
 
Old 08-06-2011, 06:20 AM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
You haven't provided enough info to diagnose the problem.

The code you posted looks OK, so I think the bug is in some of the code you didn't post.

How big is the count input to function()? If it is more than 50, then you might be exceeding the end of the stack array.

Whatever is going wrong, "sticks in a loop when it enters the push() function" is a very unlikely result. What makes you think that is happening?

Last edited by johnsfine; 08-06-2011 at 06:21 AM.
 
Old 08-06-2011, 06:53 AM   #3
mscoder
LQ Newbie
 
Registered: Jun 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
@jonsfine....i posted the entire code..the count input i was giving for testing was 3...i cant find any reason for the prblm..

Last edited by mscoder; 08-06-2011 at 07:12 AM.
 
Old 08-06-2011, 07:18 AM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by mscoder View Post
Code:
                printf("\Push fn called\n");
I didn't notice that typo until I tried to compile the code.

What is the \P doing?

But once I ran the program I understood the bug. In fact you had posted enough the first time and I just didn't notice the right detail before running the program:

Quote:
for(i=0;i<count1;i++)
...
push(i); /*Push called*/
Note your first index is 0.
Quote:
n=pop();
index=pop();
n=n-index;
if(n>=index)
So when the index is 0, n=n-index does nothing, so there is the cause of your infinite loop.

Last edited by johnsfine; 08-06-2011 at 07:26 AM.
 
1 members found this post helpful.
Old 08-06-2011, 07:23 AM   #5
Peverel
Member
 
Registered: May 2009
Location: Chelmsford, England
Distribution: OpenSuse 12.2 and 13.2, Leap 4.2
Posts: 128

Rep: Reputation: 24
I suggest you trace pop() by putting in a suitable printf(). You might find that it is not push() which is looping.
 
1 members found this post helpful.
Old 08-06-2011, 07:24 AM   #6
mscoder
LQ Newbie
 
Registered: Jun 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
@johnsfine...the "\" was a stray one added during editing the post...it has nothing to do with the code which showed the prblm in push()...sorry about that
 
Old 08-06-2011, 07:29 AM   #7
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by mscoder View Post
@johnsfine...the "\" was a stray one added during editing the post...it has nothing to do with the code which showed the prblm in push()...sorry about that
Reread my post, above. I was editing it at the time you first read it, so you missed the important part.
 
1 members found this post helpful.
Old 08-06-2011, 07:32 AM   #8
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by Peverel View Post
I suggest you trace pop() by putting in a suitable printf(). You might find that it is not push() which is looping.
The loop is actually in function(). It is the statement while(top!=-1)
and that loops forever because
if(n>=index)
is true forever in the case that index is zero and n is positive.
 
1 members found this post helpful.
Old 08-06-2011, 07:36 AM   #9
mscoder
LQ Newbie
 
Registered: Jun 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Quote:
I suggest you trace pop() by putting in a suitable printf(). You might find that it is not push() which is looping.
thanks..i got the prblm.....it was pop() indeed that was looping in the very first function call with array index 0...
 
Old 08-06-2011, 07:37 AM   #10
Peverel
Member
 
Registered: May 2009
Location: Chelmsford, England
Distribution: OpenSuse 12.2 and 13.2, Leap 4.2
Posts: 128

Rep: Reputation: 24
Johnsfine: exactly so. But I hoped he might find it himself!
 
  


Reply

Tags
printf



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
this code segment sticks in a loop when it enters the push() function.... mscoder Programming 1 08-06-2011 07:10 AM
Not able to push arguments to a function from entry_32.S kamalsivadas Linux - Kernel 1 03-28-2011 10:56 AM
Kernel code segment write protection eagle_soaring_1 Linux - Kernel 1 02-28-2011 02:14 PM
how to print function names & parmaters each time control enters the function? tanniru Linux - Networking 1 09-11-2008 01:21 AM
Write to code segment Earp Programming 4 04-22-2004 01:49 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration