C 100 Prisoners riddle, something wrong with my code

765 Views Asked by At

(If you already know what the riddle is about just read the last 2 lines)

I saw a video about a riddle which is called "The 100 prisoners riddle" it essentially tells you that a bunch of prisoners (only one person at a time) get into a room, this room has boxes that are ordered correctly from 1 to a 100 but the numbers inside the boxes are random and each prisoner getting into the room is numbered from 1 to a 100 too, so each prisoner has to pick the box that has his number, each prisoner has a set of tries (50 tries) if he opened 50 boxes and he didn't find his number he loses! for example prisoner number 1 gets in the room and he has to find the box that has his number .. it might be box number 7 or 19 or 27 who knows! so it's just a game of luck .. or is it? the game has strategies and ways to mathematically solve the puzzle but that's not my problem here, I just wanna program the game in C and solve the puzzle for myself, the code has a lot of holes in it so look closely into it and find what's the problem, THANK YOU ALL :)!

#include <stdio.h>
#include <stdlib.h>

int main() {
    int i, j = 0, k = 0, counter = 0;
    int boxes[10];
    int boxEntered;
    for (i = 0; i <= 10; i++) \\ numbering the array
        boxes[i] = i;
    for (i = 0; i <= 10; i++) {    
        int temp = boxes[i];
        int randomIndex = (rand() % 10); \\ shuffling the boxes to put random numbers
        boxes[i] = boxes[randomIndex];
        boxes[randomIndex] = temp;
    }
    for (i = 0; i <= 10; i++) {
        printf("%d : (%d)\n", boxes[i], i); \\ print the boxes randomized and their index ordered
    }
    printf("You only have 5 tries!\n");
    while (k != 5) {
        while (j < 10) {
            printf("Pick a box number between 0 and 10 (You are number %d)\n",counter);
            scanf("%d",&boxEntered);
            if (boxes[boxEntered] == boxes[counter]) {
                printf("\nYou succeded, PROCEED TO NEXT PRISONER\n");
                j++; \\ go to the next iteration
                k = 0; \\ set tries back to 0
                counter++; 
            } else
                printf("Try again\nThe box you entered had number %d\n",boxes[boxEntered]);
            k++;
            if (k == 5) { \\ if player prisoner fails 5 times you break the loop
                break;
            }
        }
    }
    if (counter == 10) { \\ if last prisoner was reached successfully then game is won
        printf("You are freed!");
    } else {
        printf("You are going back heheheheheh!\n")
    }
    return 0;
 }

As you can see in this picture the output doesn't make any sense at all and i have no idea what is wrong here..

enter image description here

3

There are 3 best solutions below

0
Ryan Zhang On

From your code's logic, you should replace

boxes[boxEntered] == boxes[counter]

with

boxes[boxEntered] == counter

This is because counter here seems to represent a prisoner. Taking boxes[counter] will give you a box, which isn't what you want; you're trying to see if the box matches the current prisoner.

Another important note is the following code will go out of bounds for your array, causing undefined behaviour:

for (i = 0; i <= 10; i++) boxes[i] = i;

boxes is declared as having size 10, and therefore taking boxes[10] goes out of bounds; the maximum is boxes[9].

To fix this, you can index your arrays starting from 1. To do this in C, instead of declaring boxes[10], use boxes[11]. This will ensure you can access boxes[10].

You can then change your loops to start from 1, so something like:

for (i = 1; i <= 10; i++) boxes[i] = i;

Be sure to make this change for every array and for loop in your code.

0
user22724223 On

Here you go. I have done some editing to your program (it's actually a great program! - I love it!). I've only made some minor changes but it does work (for one thing I have added the srand() function so that your program will produce a different result each time. Also, the main culprit is that the line: if (boxes[boxEntered] == boxes[counter]) should be if (boxes[boxEntered] == counter).

#include <stdio.h>
#include <stdlib.h>
#include <time.h.>

int main()
  {
  int i, j = 0, k = 0, counter = 0;
  int boxes[10];
  int boxEntered;
  int temp;
  int randomIndex;
  time_t now;//Declaring time-type variable.

  time(&now);//Storing system time into now.
  srand(now);//Seeding the random number generator with system time.

  for (i = 0; i < 10; i++) // numbering the array
    { boxes[i] = i; }

  for (i = 0; i < 10; i++)
    {
    temp = boxes[i];
    randomIndex = (rand() % 10); // shuffling the boxes to put random numbers
    boxes[i] = boxes[randomIndex];
    boxes[randomIndex] = temp;
    }

  for (i = 0; i < 10; i++)
    {
    printf("Box# %d =  %d\n", i, boxes[i]); // print the boxes randomized and their 
                                            //index ordered
    }

  printf("You only have 5 tries!\n");
  while (k != 5)
    {
    while (j < 10)
      {
      printf("Pick a box number between 0 and 10 (You are number %d)\n",counter);
      scanf("%d",&boxEntered);
      if (boxes[boxEntered] == counter)
        {
        printf("\nYou succeded, PROCEED TO NEXT PRISONER\n");
        j++; // go to the next iteration
        k = 0; // set tries back to 0
        counter++;
        }
        else
          {
       printf("Try again\nThe box you entered had number  %d\n",boxes[boxEntered]);
          k++;
          if (k == 5) // if player prisoner fails 5 times you break the loop
          { break; }
          }
      }

  if (counter == 10)  // if last prisoner was reached successfully then game is won
       {
       printf("You are freed!");
       break;
       }
    else
      { printf("You are going back heheheheheh!\n"); }
     }
  return 0;
  }


    
0
user22724223 On

Here is your program organized into functions. Please note that there was some pretty heavy editing. For one thing your statement while (k != 5) was unnecessary so it was removed (removing the loop entailed the relocation of some of your statements).

#include <stdio.h>
#include <stdlib.h>
#include <time.h.>

void seedRandom();
void numbering();
void shuffling();
void printShuffle();
void pickBox();

int boxes[10];

int main()
  {
  seedRandom();
  numbering();
  shuffling();
  printShuffle();
  pickBox();
  return 0;
  }

void seedRandom()
  {
  time_t now;//Declaring time-type variable.
  time(&now);//Storing system time into now.
  srand(now);//Seeding the random number generator with system time.
  }

void numbering() // numbering the array
  {
  int cntr;
  for (cntr = 0; cntr < 10; cntr++)
    { boxes[cntr] = cntr; }
  }

void shuffling() // shuffling the boxes to put random numbers
  {
  int cntr;
  int temp;
  int randomIndex;
  for (cntr = 0; cntr < 10; cntr++)
    {
    temp = boxes[cntr];
    randomIndex = (rand() % 10);
    boxes[cntr] = boxes[randomIndex];
    boxes[randomIndex] = temp;
    }
  }

void printShuffle()
  {
  int cntr;
  for (cntr = 0; cntr < 10; cntr++)
    { printf("Box# %d =  %d\n", cntr, boxes[cntr]); } // print the boxes
                                       //randomized and their index ordered
  }

void pickBox()
  {
  int tries=0, prsnrNumb=0, boxEntered;
  printf("You only have 5 tries!\n");
  while (1)//Infinite loop
    {
    printf("Pick a box number between 0 and 9. You are number %d)\n",prsnrNumb);
    scanf("%d",&boxEntered);
    if (boxes[boxEntered] == prsnrNumb)
      {
      puts("You succeed!");
      if(prsnrNumb<9)
        {puts("PROCEED TO NEXT PRISONER\n");}
      prsnrNumb++; // go to the next iteration
      tries = 0; // set tries back to 0.
      }
      else
        {
        printf("Try again\nThe box you entered had number");
        printf("%d\n",boxes[boxEntered]);
        tries++;
        if (tries == 5)//if player prisoner fails 5 times you break the loop
          {
          printf("You are going back heheheheheh!\n");
          break;
          }
        }
    if (prsnrNumb == 10) //if last prisoner was reached successfully then
                            //game is won
         {
         printf("You are freed!");
         break;
         }
      }
  }