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