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.
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;
}
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) )
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;
}
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.
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.
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;
}