Like Tree3Likes

Futurama Theorem

This is a discussion on Futurama Theorem within the C Programming forums, part of the General Programming Boards category; Originally Posted by MK27 Here's an example of the Fisher-Yates shuffle algorithm: Code: #include <stdio.h> #include <stdlib.h> #include <time.h> void ...

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by MK27 View Post
    Here's an example of the Fisher-Yates shuffle algorithm:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    void FYshuffle (int *ray, int len) {
            int i, tmp, x;
            for (i=len-1; i>1; i--) {
                    x = rand()%i; 
                    if (x==i) continue;
            // now swap
                    tmp = ray[i];
                    ray[i] = ray[x];
                    ray[x] = tmp;
            }
    }
    
    int main(void) {
         int ray[10] = {0,1,2,3,4,5,6,7,8,9}, i;
    
            srand(time(0));
            FYshuffle(ray,10);
            for (i=0;i<10;i++) printf("%d\n",ray[i]);
    
            return 0;
    }
    But you can't do that with this:
    Code:
    char Names [9] [15];
    However, you can do it with this:
    Code:
    char *Names[9];
    Which can be initialized the same way. You'd then need to modify the shuffle function to work with a char *ray[] (which means changing the first parameter and the type of the tmp variable).
    Please see my post #13 ... If we follow the shows, the problem begins with a single swap and you have to re-oder the array without repeating any swaps...

  2. #17
    Registered User
    Join Date
    Nov 2011
    Posts
    13
    Okay, now, before scrambling, I decided to let the user input the names of each character of the swapping with this code:
    Code:
    main (void) {
    int N;
    char n1 [10],n2 [10],n3 [10],n4 [10],n5 [10],n6 [10],n7 [10],n8 [10],n9 [10];
    printf ("Give me the 1st name\n");
    scanf ("%s", n1);
    printf ("Give me the 2nd name\n");
    scanf ("%s", n2);
    printf ("Give me the 3rd name\n");
    scanf ("%s", n3);
    printf ("Give me the 4th name\n");
    scanf ("%s", n4);
    printf ("Give me the 5th name\n");
    scanf ("%s", n5);
    printf ("Give me the 6th name\n");
    scanf ("%s", n6);
    printf ("Give me the 7th name\n");
    scanf ("%s", n7);
    printf ("Give me the 8th name\n");
    scanf ("%s", n8);
    printf ("Give me the 9th name\n");
    scanf ("%s", n9);
    char Names [9] [15] = {&n1, &n2, &n3, &n4, &n5, &n6, &n7, &n8, &n9};
    printf ("Give me a number from 1-9\n");
    scanf ("%d", &N);
    printf ("%s", Names[N-1]);
    system ("PAUSE");
    }
    But, no matter what I try, I can't get the names in the array. Is there a specific command to do that, or am I just doing it wrong?

    P.S.- the "void" in main (void) isn't really necesary according to our compiler and our teacher, that's why I didn't put it there before.

    P.P.S.- Thanks for the scrambling algorithms, I'll tell you how it goes when I try them.

  3. #18
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    Quote Originally Posted by CommonTater View Post
    It's actually a logic puzzle, Lase...

    Two people's minds are swapped
    They cannot be swapped back because you can only swap each pair once
    You can use additional bodies as needed
    The problem is to get all the right minds back into the right bodies.

    They did the same thing on Stargate, if you're not a Futurama fan...
    Isn't this really a variation on Tower of Hanoi - Wikipedia, the free encyclopedia

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I don't follow. In the Tower of Hanoi, you repeat the order of two of the disks, many times.

  5. #20
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Ferreres93 View Post
    But, no matter what I try, I can't get the names in the array. Is there a specific command to do that, or am I just doing it wrong?
    create the array first...then input your data directly into the array..
    Code:
    char Names[10][20];
    
    for(int i = 0; i < 10; i++)
      scanf("%19s",Names[i]);
    P.S.- the "void" in main (void) isn't really necesary according to our compiler and our teacher, that's why I didn't put it there before.
    That's right the void parameter isn't absolutely necessary (arguably, it is more secure)... but the integer return value is! The operating system uses that returned value to determine if you code is returning an error or not. Moreover; some parent programs and batch files also use the return values for various programmatic reasons. Your code may work perfectly but it will be incorrectly interfaced with the operating system.

    More than once I've watched batch files crash because some idiot let his program return garbage instead of paying attention to details.
    Last edited by CommonTater; 12-02-2011 at 12:19 PM.

  6. #21
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    I don't follow. In the Tower of Hanoi, you repeat the order of two of the disks, many times.
    Hey my friend... check message #13 ... I've been working on this a bit and I've set out a starting point, if you want to take it on as a challenge...

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's a run errands day today. Likely you'll have it finished before I get back. Sounds like a fun one to ponder though.

  8. #23
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Adak View Post
    It's a run errands day today. Likely you'll have it finished before I get back. Sounds like a fun one to ponder though.
    Actually, I've been messing with this pretty much since the OP gave it to us... still no closer to a codeable solution...
    It's an excellent brain teaser!

  9. #24
    Registered User
    Join Date
    Nov 2011
    Posts
    13
    Okay, so far, I've come up with this (and since my partner is pretty much only doing what I tell him to do, my brains are about to explode by this point). I've Input the 9 names into an string array and copied the original array so that I can compare who's mind is in who's body after the scramble. I figured I could try to create a pointer to each of the positions in the array, and then scramble them, but I've got no idea how to do it. Is there a command for it? 'Cause all i can find is malloc, calloc and realloc, and they're no use (or I'm using them wrong, also a distinct posibility xD). Anyway, do you have any ideas how to do it?
    Code:
    #include<stdio.h>
    #include<string.h>
    
    
    main (void) {
    int N, i;
    char Names [9] [15];
    
    
            //Input Names
    printf("Give me 9 Names\n");
        for (i = 0; i < 9; i++)
        {
        scanf ("%s", Names[i]);
        }
            //Copy Original Array
    char names [9] [15];
        strcpy (names, Names)
            //Scramble Array (Names)
        
        
            //Return Scrambled Array
    int j;
        for (j = 0; i < 9; j++)
        {
        printf ("%s's mind is in %s's body", names[j], Names[j]);
        }
            //Unscramble, step by step, the array

  10. #25
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Ferreres93 View Post
    Okay, so far, I've come up with this (and since my partner is pretty much only doing what I tell him to do, my brains are about to explode by this point). I've Input the 9 names into an string array and copied the original array so that I can compare who's mind is in who's body after the scramble. I figured I could try to create a pointer to each of the positions in the array, and then scramble them, but I've got no idea how to do it. Is there a command for it? 'Cause all i can find is malloc, calloc and realloc, and they're no use (or I'm using them wrong, also a distinct posibility xD). Anyway, do you have any ideas how to do it?
    I would not suggest trying to compare two string arrays... that's a lot of coding.

    Take a look at my suggestion in message 13 for a starting point... if you want to print names instead of numbers, take the suggestion in message 14 and just use the names array to print with...

    And you are still not setting up your main() function correctly... If your teacher is telling you that you don't need to return an integer value from main, he's wrong... use... int main (void) or int main() don't count on your compiler to correct it for you.

    Like this...
    Code:
    // tested
    
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    void swap(int x, int y, int* array)
      {
          int temp;
         temp = array[x];
         array[x] = array[y];
         array[y] = temp;
      }
    
    // Entry
    int main (void)
      {
         int array[9];
         char Names[9][16] = {"Zoidberg", "Fry", "Leela", "Profesor", "Bender", "Amy", "Hermes", "Washbucket", "E. Nikolai"};
         int i,x,y;
    
         srand(time(NULL));
    
        // initialize
        for (i = 0; i < 9; i++)
           array[i] = i;
    
        // start problem
        do
          {
            x = rand() %9;
            y = rand() %9;
          }
        while( x == y );
    
        swap(x,y,array);
    
        // correct names
        printf("Players : \n");
        for (i = 0; i < 9; i++)
          printf("%s ", Names[i]);
        printf("\n\nTo start: \n");
        // initial status
        for (i = 0; i < 9; i++)
          printf("%s is in %s body\n", Names[array[i]], Names[i]);
     
        printf("\n\n");
        getchar();
        return 0;
    }
    Last edited by CommonTater; 12-02-2011 at 01:47 PM.

  11. #26
    Registered User
    Join Date
    Nov 2011
    Posts
    13
    Okay, scratch what I said before. I just had an idea, but I wanted to run it through you before I started trying it out.
    What I thought was this, more or less:
    Code:
    #include<stdio.h>
    #include<string.h>
    
    
    int main (void) {
    int N, i;
    char Body [9] [15];
    
    
            //Input Names
    printf("Give me 9 Names\n");
        for (i = 0; i < 9; i++)
        {
        scanf ("%s", Body[i]);
        }
            //Copy Original Array
    char Mind [9] [15];
        strcpy (Mind, Body)
            //Scramble Array (Mind)
    //RANDOM NUMBER GENERATOR (random number is x) (9 times)
        int k, count=0
        for (k=0; k<9; k++)
            Mind [x]= Body [count]
            count = count+1
    Would something like that (well done of course) work??

  12. #27
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Are you reading any of my suggestions?

    No it won't work... Go back to the show... the problem begins with 1 initial swap... you would swap all 9 of them.

  13. #28
    Registered User
    Join Date
    Nov 2011
    Posts
    13
    Yes, I do read your suggestions, I just forgot, and was looking for a way to delete the comment, sorry. Now I'm trying to do pretty much the same thing by changing a bit the card shuffle code you gave me before. I'll tell you how it goes in a bit. Thanks again.

  14. #29
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Here's a thought... copy/paste the code in message 25 and try it out... I compiled it here and tested it... I know it works.

  15. #30
    Registered User
    Join Date
    Nov 2011
    Posts
    13
    Okay, so I tried with the shuffle deck code, and it pretty much works, but when it's going to return the scrambled array, it stops working. Can you see anything wrong with the code?
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<time.h>
    #include<stdlib.h>
    
    
    #define RANDS 9
    
    
    int main (void) {
    int N, i;
    char Body [9] [15];
    
    
            //Input Names
    printf("Give me 9 Names\n");
        for (i = 0; i < 9; i++)
        {
        scanf ("%s", Body[i]);
        }
            //Scramble Array (Names)
    
    
        int Mind[RANDS];
        int item = 0;
        int temp;         
        int idx = 0;
     
        srand(time(NULL));
     
        for(idx = 0; idx < RANDS; idx++)
          Mind[idx] = idx + 1;
     
        for (idx = RANDS - 1; idx > 0; idx--)
          { item = rand() % idx;
            temp = Mind[idx];
            Mind[idx] = Mind[item];
            Mind[item] = temp; }
            //Return Scrambled Array
    int j;
        for (j = 0; j < 9; j++)
        {
        printf ("%s's mind is in %s's body\n", Mind[j], Body[j]);
        }
            //Unscramble, step by step, the array
        
        
        
            //Return Names
    }

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Separating Axis Theorem
    By Astra in forum Game Programming
    Replies: 10
    Last Post: 07-29-2011, 04:27 PM
  2. Separating Axis Theorem
    By Kenki in forum Game Programming
    Replies: 20
    Last Post: 05-22-2005, 12:49 PM
  3. Pythagorans theorem
    By Dummies102 in forum C++ Programming
    Replies: 2
    Last Post: 02-20-2002, 01:46 PM
  4. Galois Theorem
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-06-2002, 06:20 PM
  5. David's CProgramming Theorem
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 08-14-2001, 01:29 AM

Tags for this Thread


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