# Countout (Round Robin) Game

This is a discussion on Countout (Round Robin) Game within the C Programming forums, part of the General Programming Boards category; It's suppose to take n children (imagine they are in a cirlce) and a prompted number (say 5) and starting ...

1. ## Countout (Round Robin) Game

It's suppose to take n children (imagine they are in a cirlce) and a prompted number (say 5) and starting from the 1st child and going round, count out every 5th child untill only one child is left in the ring.
For example for n=11, and coutout=5, the output is suppose to be 8.
for 10, 5, the output is suppose to be 3;

I get wrong numbers and it freezing though. Can't see what is wrong with the code either. The dry runs work well.

Code:
```#include <stdio.h>
#include <conio.h>
int main(void) {
int ring[101]={0};
int x=0, count=0, cv, numchild, j;
int ringactive (int ring[], int size);
clrscr();
printf ("Enter number of chilren and countout value: ");
scanf ("%d %d", &numchild, &cv);

for (j=1; j<=numchild; j++)
ring[j]=1;

while ( ringactive(ring,numchild+1)==1) {
while (count<cv) {
if (x>=numchild) x=0;
x++;
if (ring[x]==0) x++;
else count++;}
ring[x]=0; count=0;}

for (j=1; j<=numchild; j++)
if (ring[j]==1) printf ("%d", j);
return 0;
}

int ringactive (int ring[], int size) {
int i, count=0;
for (i=1; i<size; i++)
if (ring[i]==1) count++;
if (count>1) return 1;
return 0; }```

2. ## Re: Countout (Round Robin) Game

You need to rethink (and reformat) your code. Reformating like this will allow you and others to read the code easily and it's consistant:
Code:
```#include <stdio.h>
#include <conio.h>
int main(void)
{
int ring[101]={0};
int x=0, count=0, cv, numchild, j;
// Following looks like a prototype and goes outside main()
int ringactive (int ring[], int size);
clrscr();
printf ("Enter number of chilren and countout value: ");
scanf ("%d %d", &numchild, &cv);

// Loops should go from 0 to < max -- fewer problems will occur
for (j=1; j<=numchild; j++)
ring[j]=1;

while (ringactive(ring,numchild+1)==1)
{
while (count<cv)
{
if (x>=numchild) x=0;
x++;
if (ring[x]==0) x++;
else count++;
}
ring[x]=0; count=0;
}

for (j=1; j<=numchild; j++)
if (ring[j]==1) printf ("%d", j);
return 0;
}

int ringactive (int ring[], int size)
{
int i, count=0;
for (i=1; i<size; i++)
if (ring[ i]==1) count++;
if (count>1) return 1;
return 0;
}```
Your code seems to break down by ringactive() starting at 1 (first child), looping to beyond the last child, returning a 0 or 1 and having no idea after it returns what child was stopped on. Then starting all over again at 1. Also cv must be used in the routine.

count is a local variable in ringactive() and is never passed back to main() where count is also a local variable and is never changed from 0.

Rethink traversing the kid list (from 0 to < numchild). This includes actually using cv properly to get the proper child.

Also, output values at key places in the code to see if you're getting the values you need.

3. How do I set watches for the array in Turbo C++ ?
I know I can do variables like x cv, but how can I watch the value of the whole array?

ringactive doesn't need to say which is the last stopped on child. It merely checks that there are more than one child (1) in the ring.
and it goes up to the last child. Say the number of children is then, then according to the code it would go up to <10+1, and it would go up to <11 , which is what is it supposed to do. But even if it was to go one more, it wouldn't matter as all the other values after the numchild would be zero and the program would just skip it and go onto the next value, or come back to the beginning of the array and look for the next one value.

Cv is used in the program It is not suppose to be altered. It keeps the value anc check each child to make sure the count does not exceed the coutout value.. The program checks everyone that is in the ring still and add one to them and when it reach the count where the number added IS cv then breaks out the loop and sets that child to out.

It keeps the value of x (rep each active child) and only come back to the begnning if it is at the end of the array.

4. ## Re: Re: Countout (Round Robin) Game

Originally posted by WaltP
// Following looks like a prototype and goes outside main()
int ringactive (int ring[], int size);
Not true. It is perfectly valid to prototype functions within functions. It's not common, but it is perfectly valid.

Quzah.

5. Does it limit the scope of what can invoke it then quzah?

6. Consider the following:
Code:
```int main( void )
{
int x = 0;
int foo( void );

x = foo( );

return 0;
}

int foo( void )
{
return 5;
}```
You can still use 'foo' in any functions declared after it, and, were you to include it in a header file, you could still use it elsewhere.

The only thing prototyping within a function does is grant said function the ability to use the prototyped function even if it hasn't been declared yet. (As illustrated here. Without the prototype in 'main', 'foo' could not be used by main, because it hadn't been prototyped yet.)

Quzah.

7. Originally posted by Hexxx
How do I set watches for the array in Turbo C++ ?
I know I can do variables like x cv, but how can I watch the value of the whole array?
combine printf() and getchar() statements. Unless you know about the debugger, they're easy to use and flexible.

Hexx, by your description of the program, if you have 10 children with CV of five, the children should be checked and removed in this order:
0 1 2 3 4 5 6 7 8 9 0 1 2 3 5 6 7 8 0 1 2 3 6 7 8 0 2 3 6 7 0 2 3 6 0 2 3 6 2 3 6 2 6 2 6 2
Leaving 2 as the chosen child.

But your routines and loops do not count 5 children and remove the last, then continue counting 5 more. You need to rethink your looping structure

After your initialization loop, you have 1's in the appropriate positions in ring. At the bottom of the while(count<cv) first time thru, they are all 0.

Quzah:
Didn't know that about prototypes. I think my life was simpler without that knowledge. I'm still going to put them outside main() -- less confusing

8. but when I use 10 and 5 it freezes, doesn't give anyout put

9. Wrote a quick program to do this, its not the best way but it might help:
Code:
```#include <stdio.h>
int main (void)
{
const int NUMKIDS=30;
int num, count, kidsleft, index;
int kids[NUMKIDS];

for (count=0; count<NUMKIDS; ++count)
{
kids[count]=1;
}

kidsleft = NUMKIDS;

/*get number*/
num = 5;

index = 0;
while (kidsleft > 1)
{
for (count=0;; index=(index+1)%NUMKIDS)
if (kids[index] == 1)
{
++count;
if (count == num)
break;
}

printf("Removing kid #%d\n",index);
kids[index]=0;
--kidsleft;
}
for (count=0; kids[count] == 0; ++count);

printf("Kid %d is the lucky one", count);

puts("");
return 0;
}```
of particular note should be how I increment the index:
Code:
`index=(index+1)%NUMKIDS`
This will make it so it will wrap around and start from the beginning.

10. Gee, I did one too, Thantos, but I'm trying to help Hexx figure it out for himself...

Originally posted by Hexxx
but when I use 10 and 5 it freezes, doesn't give anyout put
Duh!

Hexx, you aren't processing the data correctly. For the third time -- your program has problems and your loops are wrong. Of course you're getting freezes. Just look at my last post! What happens when your entire ring gets filled with 0?

You've never tried testing the program either by outputting data nor desk checking your code. Run thru it by hand.

11. Was hoping he could see one that works and go "OH" and figure out why his doesn't work
Hexx change
Code:
`else count++;}`
to
Code:
`else count++;printf("%d %d\n", x, count);}`
and you'll see why you aren't getting any output.

12. Well my exam finished today so I got time to make it work now.!!!

Oh and WaltP you said I need to rethink it, I'm guessing you talking about the code, cause the logic works (to me anyway), but I think I know where the problem was in my loops and found out how to use watches and trace in the compiler, so I must get it out eventually.

13. Hey all the loops run good and I did test the program how else would I know it's not working??? Anyway the ONLY problem in it was I had a statement that should not have been there, that set the loop off by one and it went downhill (Wrong output from there) Other than that all the logic is perfectly valid.
It works perfect now.

Look at the original program and see if you can find which line, otherwise if anyone wants to see the finished code I'll post upon request.

[i]
Duh!

Hexx, you aren't processing the data correctly. For the third time -- your program has problems and your loops are wrong. Of course you're getting freezes. Just look at my last post! What happens when your entire ring gets filled with 0?

You've never tried testing the program either by outputting data nor desk checking your code. Run thru it by hand. [/B]