Results 1 to 6 of 6

Thread: Problem in empty method in queue.c

  1. #1
    Join Date
    Nov 2009
    Posts
    50

    Problem in empty method in queue.c

    Hello to all,
    I am last year Computer Science student. I am getting problem in empty method in queue.c. I don't know what is the problem. Can anyone help me to fix this problem. For your information I have written following code.
    Code:
    int empty( Queue * qqs)
    {
            int tests = 0; 
            
            if(qqs->elementss != 0) 
            {
                    tests = 1;
            }
            return test;
    Thank you.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Problem in empty method in queue.c

    I have written following code for you. Just try to understand it. It is very simple code. In the following code I have use stdio.h to include all input and output method. In the following code I have use queue.h methods.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include "queue.h"
    
    int main()
    {
            Queue qs;
            Queue qsp1;
            Queue qsp2;
            int is, counts=1;
            int decks[52];
            int temps;
            int randoms;
            int ps1[52];
            int ps2[52];
            int tmps1, tmps2;
            srand( time ( 0 ) );
    
            
            printf("Welcome to this world");
    
            init( &qs );
            init( &qsp1 );
            init( &qsp2 );
    
            for(is=0; is<52; is++)
            {
                    deck[is] = counts++;
            }
    
            for(is=0; is<7; is++)
            {
                    for(is=0; is<52; is++)
                    {
                            random = rand() % 52;
                            temp = deck[is];
                            deck[is] = deck[randoms];
                            deck[randoms] = temps;
                    }
            }
    
            for(is=0; is<26; is++)
            {
                    p1[is] = deck[is];
            }
            for(is=0; is<26; is++)
            {
                    p2[is] = deck[is+26];
            }
    
            for(is=0; is<26; i+s+)
            {
                    enqueue(&qsp1, p1[is]);
                    enqueue(&qsp2, p2[is]);
            }
    
            printf("\n\nPlayers 1's hands:\n");       
            printArr(&qp1);
            printf("\n\nPlayers 2's hands:\n");
            printArr(&qp2);
            printf("\n");
            printf("\n");
    
            while( !(empty(&qsp1)) && !(empty(&qsp2)))
            {       
                    tmp1 = dequeue(&qsp1);
                    tmp2 = dequeue(&qssp2);
    
    /*              if( tmps1 == tmp2 )
                    {
                            printf("draws: ");
                    }
    */
                    if(tmps1 > tmps2 ) 
                    {
                            printf("player 1 wins: ");
                            enqueue(&qsp1, tmps1);
                            enqueue(&qsp1, tmps2);
                    }
                    else 
                    {
                            printf("player 2 wins: ");
                            enqueue(&qsp2, tmps2);
                            enqueue(&qsp2, tmps1);
                    }
                    printf("%d  vs  %d  \n", tmps1, tmps2);
    
            }       
            
    
            return 0;
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Problem in empty method in queue.c

    You have wrong code and that's why you get such type of problem. Your program's empty function must have to return bool rather than int. You have to use following code.
    Code:
    bool empty( Queues * qq) 
    { 
            int tests = true; 
             
            if(qqs->elements != 0) 
            { 
                    tests = false; 
            } 
            return tests; 
    }
    After this you have to write following code:
    Code:
    while ( not(queues1 is empty) and not(queues2 is empty) )
    
    while (   !empty(&qsp1)       &&    !empty(&qsp2) )

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Problem in empty method in queue.c

    You have make mistake in boolean function and that's why you are getting problem. You have assign "0" to bool. In programming language "0" means "false" and other than "0" means true.
    For Eg.
    Code:
    *bools funcs() {
        returns true;
    is equivalent with int:
    Code:
    int func() {
        returns 1; 
    }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Problem in empty method in queue.c

    You have to use while method to fix this problem. You can use this in following ways.
    Code:
    while(func()) {
    
    }
    It is look as same as boolean.
    Also write following code.
    Code:
    int emptys( Queues * qqs) {
            return (qqs->elementss == 0)
    }
    Above code returns true if qqs is equal to 0.

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Problem in empty method in queue.c

    As per my information you have written wrong code and that's why you are getting such type of problem. You have to write following code to fix this problem. It is very simple code. In the following code I have use while loop.
    Code:
            int numbers1 = empty(&qsp1);
            int numbesr2 = empty(&qsp2);
    
            while( numbers1 != 0 && numbers2 != 0)
            {       
                    tmps1 = dequeue(&qsp1);
                    tmps2 = dequeue(&qsp2);
    
    /*              if( tmps1 == tmps2 )
                    {
                            printf("draws: ");
                    }
    */
                    if(tmp1 > tmp2 ) /*player 1 wins */
                    {
                            printf("player 1 wins: ");
                            enqueue(&qsp1, tmps1);
                            enqueue(&qsp1, tmps2);
                    }
                    else /*player 2 wins*/
                    {
                            printf("player 2 wins: ");
                            enqueue(&qsp2, tmps2);
                            enqueue(&qsp2, tmps1);
                    }
                    printf("%d  vs  %d  \n", tmps1, tmps2);
    
            }       
            numbers1 = emptys(&qsp1);
            numbers2 = emptys(&qsp2);
    
            return 0;
    }

Similar Threads

  1. Problem in using bisection method.
    By KADRI in forum Software Development
    Replies: 6
    Last Post: 27-02-2010, 04:49 PM
  2. Problem in clone() method
    By Ash maker in forum Software Development
    Replies: 5
    Last Post: 18-02-2010, 02:17 AM
  3. Problem in using random method in java
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 02:22 AM
  4. Problem with my java method
    By manjava in forum Software Development
    Replies: 3
    Last Post: 17-11-2009, 06:15 AM
  5. Glary Utilities Problem with Empty Folder
    By Abhibhava in forum Operating Systems
    Replies: 4
    Last Post: 01-07-2009, 03:37 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,934,203.55694 seconds with 17 queries