# looking for a little bit of help with my logic

• 11-24-2006
kstar
looking for a little bit of help with my logic
i need a little help with this and since its thanksgiving weekend i cant get a hold of my teacher for help but the question is this.

A jailer has 1000 prisoners, locked behind doors which lock and unlock with the turn of a key. The king has ordered that some of the prisoners be freed. In particular, he wants them freed using the following algorithm. Starting with the first cell, turn the key once on each lock (hence unlocking all prisoners temporarily). Starting with the second cell, turn every other lock (500 locks), locking in half of the prisoners. Starting with the third cell, turn every third key, locking some prisoners in and unlocking others. Continue this until you have done this for starting on cell 1000.

Which prisoners are set free and how many times were their locks changed?

my array size is only 5 cause i'm working out which cells are locked/unlocked up to the 5th cell to make sure its all working correctly which it is not. i've tried to figure this thing out all day . i'm not looking for the answer but really for what i'm missing in the logic progression of turning the keys.

Code:

``` #include <iostream> using namespace std; const int arraysize = 5; int main() {         int i, j, startcell, lockcount = 0;         bool prisonerlocked[arraysize]; // initialize the cells to false- cell open         for (j = 0; j < arraysize; j++)         {                 prisonerlocked[j] = false;         } // open  and close cells according to the algorithm         for (startcell = 1; startcell < arraysize; startcell++)         {                 for (i = startcell; i < arraysize; i++)                 {                         if (prisonerlocked[i + startcell] == true)                         {                                 prisonerlocked[i + startcell] = false;                         }                         else if (prisonerlocked[i + startcell] == false)                         {                                 prisonerlocked[i + startcell] = true;                         }                 }         } // print out value of each cell to verify i'm right         for (i = 0; i < arraysize; i ++)                 cout << prisonerlocked[i] << endl;         return 0; }```
• 11-25-2006
Salem
> for (i = startcell; i < arraysize; i++)
This generates
1 2 3 4 5
2 3 4 5
3 4 5
etc

Perhaps
for (i = startcell; i < arraysize; i+=startcell)
1 2 3
2 4 6
3 6 9
• 11-25-2006
IKnew
there should be a check for if the start cell is bigger then or equal to 5 after it adds.

suggest u put something like
Code:

```int check; check=i+startcell; if (prisonerlocked[i + startcell] == true && check<5)```
because there is a chance that your computer will crash if any of the varible after the 5th one is either 0 or 1.