# Need a little help

• 09-15-2002
Joes108
Need a little help
This is the problem
You are walking along on a nice sunny day and stumble upon a very odd bag of beans. This bag contains a few black beans and a few white beans. You want to use the bag, but you don't have a use for the beans. Therefore, you decide to take all the beans out. Being an odd bag, you discover only two beans can be removed at a time. Furthermore, the following rules apply:

If both the beans you remove are the same color, one more black bean magically appears in the bag.
If the beans you remove differ in color, one more white bean magically appears in the bag.
Since you are a curious computer science student, you decide to write a program. This program will input the number of black beans and the number of white beans in the bag. The output of this program will be the color of the last bean in the bag.

this is what I have so far
Code:

```#include <iostream.h> #include <conio.h> #include <stdlib.h> #include <time.h> void main() {   srand((unsigned)time(NULL));   int blk, white, ran, ran1;   cout<<"How many black beans ";   cin>>blk;   cout<<"How many white bean ";   cin>>white;   do{   ran=rand()%2;   ran1=rand()%2;   if (ran == ran1)   blk += 1;   if (ran != ran1)   white += 1;   if (ran = 1)   blk -=1;   if (ran = 2)   white -=1;   }while (blk == 0 || white == 0);   cout<<"Black : "<<blk<<"  White : "<<white<<endl;   getch(); }```
my problem is that none of the numbers go to zero
Thanks Joe
• 09-15-2002
Hammer
A couple of simple mistakes:

>if (ran = 1)
You need two = signs to do a comparison:
>if (ran == 1)

>void main()
main returns an int, so declare it like this
>int main(void)
and return 0; at the end.

>if (ran == 1) blk -= 1;
>if (ran == 2) white -= 1;
I presume these two lines represent the point at which you are taking the beans out of the bag? If so, you are supposed to remove 2 beans. So you'll need some logic like this
Code:

```if (bean1 == black) black--; else white--; if (bean2 == black) black--; else white--;```
And, what if there's two black beans left in the bag, and your code that rand()'s a black or white bean returns white? That'll be a problem, because you'll decrement the white count to -1. Some more thought is needed there, I feel!

Also, your loop control logic doesn't appear to do what you want, but I'll let you fix the above first. :)
• 09-15-2002
Joes108
ok this is what I did
Code:

```#include <iostream.h> #include <conio.h> #include <stdlib.h> #include <time.h> int main(void) {   srand((unsigned)time(NULL));   int black, white, bean1, bean2;   cout<<"How many black beans ";   cin>>black;   cout<<"How many white bean ";   cin>>white;   do{   bean1=rand()%2;   bean2=rand()%2;   if (bean1 == bean2)   black += 1;   if (bean1 != bean2)   white += 1;   /*if (ran == 1)   blk -=1;   if (ran == 2)   white -=1;*/   if (bean1 == black)           black--;   else white--;   if (bean2 == black)           black--;   else white--;   }while (black == 0 || white == 0);   cout<<"Black : "<<black<<"  White : "<<white<<endl;   getch();   return 0; }```
thanks Joe
• 09-15-2002
Hammer
OK, so now you've still got some work to do. The loop control is this bit:
>while (black == 0 || white == 0);
... which is incorrect. If you start with 10 beans (5b, 5w) and follow the removal rules, at the end of the first loop the total bean count will be 9. This means the loop control you have coded will never be true, so the loop exits on the first run.

A better logic might be:
>while (black+white>0);
but I haven't really thought it through, I'll leave that to you.

Also, don't forget my other comment about taking white beans out of the bag when there weren't any in there in the first place. ;)