Thread: a shuffle program

  1. #1
    *Tim*
    Guest

    a shuffle program

    I want to cut a tring in half then take each number from eack half and put them on top like this:


    1 2 3 4 5 6 7

    cut the string in half

    1 2 3 4 5 6 7

    shuffle

    3 7 2 6 1 5 4

    any idea on how to do that....thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There are many ways to do this.
    Code:
    for( w = x = 0, y = z = strlen(a)/2; x < z && y < strlen(a); x++,y++)
    {
        array[w++]=a[x];
        array[w++]=a[y];
    }
    For a quick hack, something like that should work.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    *Tim*
    Guest
    Thanks for the help...

    Now if I want to keep on cutting the deck and shuffling till I get to the original .....how do I do it?


    Here is my code, but for some reason it is not working?

    Code:
     int w,x,y,z,k;
                int count=0;
    
    		char values[]={'1','2','3','4','5','6','7','8'};
    
    		for( w = x = 0, y = z = strlen(values)/2;  x < z  &&  y < strlen(values);   x++,y++ )
    		{
    			theDeck[w++]=values[x];
    		    	theDeck[w++]=values[y];
    		    	count++;
    		}
    
    		int count2=0;
    		for(;;)
    		{
    			for( w = x = 0, y = z = strlen(values)/2;  x < z  &&  y < strlen(values);   x++,y++ )
    			{
    				theDeck2[w++]=theDeck[x];
    		    		theDeck2[w++]=theDeck[y];
    				count2++;
    			}
    		if (values==theDeck2) break;
    		else
    		for( w = x = 0, y = z = strlen(values)/2;  x < z  &&  y < strlen(values);   x++,y++ )
    					{
    						theDeck[w++]=theDeck2[x];
    				    		theDeck[w++]=theDeck2[y];
    						count2++;
    			}
    			if (values==theDeck2) break;
                      else continue;
    		}
    
    puts(count2);

  4. #4
    *Tim*
    Guest
    It seems that my program goes into an endless loop!!!!

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    I'm not too sure what you're up to, but this is definately wrong:
    >if (values==theDeck2) break
    You cannot compare arrays like this.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by *Tim*
    It seems that my program goes into an endless loop!!!!
    One wonders why...

    for(;;)

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    *Tim*
    Guest
    This is what I am trying to do.....

    I want to cut a deck of 52 cards in half then take each number from from the bottom of the half pile and put them on top like this:

    I then want to keep on shuffling in the same manner till I get to the original full deck

    example:
    1 2 3 4 5 6 7 8

    cut the string in half

    1 2 3 4..... 5 6 7 8

    shuffle

    1 5 2 6 3 7 4 8



    any idea on how to do that....thanks

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I then want to keep on shuffling in the same manner till I get to the original full deck
    You want to keep shuffling until it is back to the original array? What for? Anyway, just duplicate the original string, and use strcmp to make sure they end up the same.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM