looking for a little bit of help with my logic

This is a discussion on looking for a little bit of help with my logic within the C++ Programming forums, part of the General Programming Boards category; i need a little help with this and since its thanksgiving weekend i cant get a hold of my teacher ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    4

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

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,551
    > 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
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    14
    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.
    Last edited by IKnew; 11-25-2006 at 08:41 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit value check efficiency
    By George2 in forum C Programming
    Replies: 5
    Last Post: 11-05-2007, 06:59 AM
  2. porting application from 32 bit to 64 bit error
    By gandalf_bar in forum Linux Programming
    Replies: 1
    Last Post: 09-14-2005, 09:20 AM
  3. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21