Boolean help?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Boolean help?

Post by MadPumpkin »

Alright I just need help doing this, I really don't know how to go about writing this program... And if someone might be able to help that'd be awesome.
I was just attempting it in the shell not a graphics API or anything.

Alright so I have 200 chests and 200 pirates, and one other guy named blackbeard, and I dunno lets say it's blackbeards birthday. Alright so the pattern is like this, the first pirate opens every single chest. The second pirate goes and changes the position(if it's open he closes it, if it's closed he opens it) of every OTHER chest (skips 1 each). The third pirate goes and changes the position of every 3 chests (skips 2 each time). And so on and so forth.
after ALL 200 pirates have gone through the chests, blackbeard goes through and takes the treasure out of ONLY the ones that are open. WHICH did he take from?

I no longer have the source code for this because it was just me quickly trying to do it at school, but when I was trying to figure this out, I realized that I didn't really know enough about Boolean functions... Which was how I was trying to do it.
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
nardi11011
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 9
Joined: Thu Jan 28, 2010 3:24 pm

Re: Boolean help?

Post by nardi11011 »

Fuck, I wish there were more questions like this on Stack Overflow. Let's first solve the problem before getting to the code.

200 chests, 200 pirates.
First pirate opens all.
Second changes position of every second chest.
Third changes position of every third chest.

Which could also be read as:

200 booleans, 200 pirates.
First pirate changes position of every boolean.
Second changes position of every second boolean.
Third changes position of every third boolean.

But now, how do we run this in code easily? If we change the wording a bit, it becomes a lot simpler:

200 booleans, 200 pirates.
First pirate changes position of every boolean whose number is divisible by one.
Second changes position of every boolean whose number is divisible by two.
Third changes position of every boolean whose number is divisible by three.

Or, more universally:
n-th pirate changes position of every boolean whose number is divisible by n.

Now, there are two things you need to know to implement this: how to check if x is divisible by y, and how to set the value of a boolean to whatever it is currently not.

The first is done using the modulus operator (%). "x % y" gives you the remainder of the nearest whole multiple of x. So 23 % 10 would return 3, as 20 / 10 == 2, and 23 - 20 = 3. So if x is divisible by y, x % y will be equal to 0. The second is done using the not operator (!). As you may have guessed, !bool returns whatever value bool is currently not. So !true == false, and !false == true.

Now let's get to the code! It's written in C#, but the important stuff is identical in C/++.

Code: Select all

static void Main(string[] args)
{
    bool[] chests = new bool[200];

    for (int numPirate = 1; numPirate <= 200; numPirate++)
    {
        for (int numChest = 1; numChest <= 200; numChest++)
        {
            if (numChest % numPirate == 0)
                chests[numChest - 1] = !chests[numChest - 1]; //Arrays are zero-indexed!
        }
    }

    Console.WriteLine("HARRRR! I gots me some treasure from:");

    for (int numChest = 0; numChest < 200; numChest++)
    {
        if (chests[numChest] == true)
            Console.WriteLine("Chest {0}", numChest + 1);
    }

    Console.ReadLine();
}
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Re: Boolean help?

Post by MadPumpkin »

Oh my god, THANK you you've been a huge help! Maybe now I can actually get the part that was broken of Repo's source fixed too... Thank you again!
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
User avatar
davidthefat
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 529
Joined: Mon Nov 10, 2008 3:51 pm
Current Project: Fully Autonomous Robot
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: California
Contact:

Re: Boolean help?

Post by davidthefat »

Gah my head hurts...
The Great
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 17
Joined: Mon Nov 30, 2009 7:51 pm

Re: Boolean help?

Post by The Great »

This reminds me of the 1000 lockers problem that was given as an assignment in my java class. :lol:
Google 1000 lockers problem if you want to know more.
ClassA
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 5
Joined: Tue Feb 23, 2010 1:48 pm

Re: Boolean help?

Post by ClassA »

The Great wrote:This reminds me of the 1000 lockers problem that was given as an assignment in my java class. :lol:
Google 1000 lockers problem if you want to know more.
Yeah, I was asked to do this at one point I believe - this is also the kind of programming/logic problem which is often given during programming interviews.
User avatar
MrDeathNote
ES Beta Backer
ES Beta Backer
Posts: 594
Joined: Sun Oct 11, 2009 9:57 am
Current Project: cocos2d-x project
Favorite Gaming Platforms: SNES, Sega Megadrive, XBox 360
Programming Language of Choice: C/++
Location: Belfast, Ireland
Contact:

Re: Boolean help?

Post by MrDeathNote »

ClassA wrote:
The Great wrote:This reminds me of the 1000 lockers problem that was given as an assignment in my java class. :lol:
Google 1000 lockers problem if you want to know more.
Yeah, I was asked to do this at one point I believe - this is also the kind of programming/logic problem which is often given during programming interviews.
Had that one myself, didn't turn out to be so bad...
http://www.youtube.com/user/MrDeathNote1988

Image
Image

"C makes it easy to shoot yourself in the foot. C++ makes it
harder, but when you do, it blows away your whole leg." - Bjarne Stroustrup
Post Reply