Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Feb 2004
    Location
    Malaysia
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile perfect shuffle error

    hi all,
    i've tried making simple perfect shuffle where lets say the array of numbers 1,2,3,4,5,6,7,8. so after going through a perfect shuffle, the order will be 5,1,6,2,7,3,8.4. however, i don't know went wrong as my output gives me funny characters(even smileys are in). here is my code:

    Code:
    	#include <stdio.h>
    	#include <stdlib.h>
    	#define size 15 
    
    	void perfectShuffle (char strnum[]);
    
    	void main()
    	{
    		char strnum[size] = {1,2,3,4,5,6,7,8};
    
    		perfectShuffle(strnum);
    	}
    
    	void perfectShuffle(char strnum[])
    	{
    		int strsize, elehld, streven[size], strodd[size], strcmb[size], i;
    
    		strsize = sizeof(strnum);
    
    		for (i = 0; i < strsize; i++)
    			{
    				elehld = i % 2;
    
    				if (elehld == 0)
    					{
    						streven[i] = strnum[i];
    						strcmb[i] = streven[i];
    					}
    				else if (elehld != 0)
    					{
    						strodd[i] = strnum[i];
    						strcmb[i+1] = strodd[i];
    					}
    				else
    					printf("\nElements Don't Exist!\n");
    			}
    
    		printf("Array before perfect shuffle is %s\n",strnum);
    		printf("Array after the perfect shuffle is : %s\n", strcmb);
    		
    	  }
    thanking all of u in advance...
    Warm Regards,
    Mivec

  • #2
    New Coder
    Join Date
    Nov 2004
    Location
    Netherlands
    Posts
    87
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, the reason for all the weird characters is the fact that you declare a character array with the ASCII values of those characters and not the numbers you want to print. In other words, you specify 1, 2, 3, etc., but those won't print 1, 2, 3 because the ASCII character values for 1, 2 and 3 are 48, 49, and 50, respectively. You also try to print an array of integers as a string, which most likely won't work out either. It's probably best if declare the string like it would print (ie. char string[] = "12345678") and subtract 48, or '0', from each character when calculating, for this would also allow the user to enter a possibility. For the answer you could use another string and when you need to print you could add 48 to all the values, thus increasing them to their proper ASCII character values.
    Last edited by Dr. Evil; 01-30-2005 at 10:39 AM.

  • #3
    Regular Coder
    Join Date
    Feb 2004
    Location
    Malaysia
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy

    thanks for the prompt reply.well, that helped in the character problem but it still doesn't output the correct output. i was wondering if the sizeof is used the correct way:

    Code:
    strsize = sizeof(strnum);
    thanks again
    Warm Regards,
    Mivec

  • #4
    Regular Coder
    Join Date
    Dec 2003
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's not going to return the size of the array. It just returns the size of the strnum variable, which is a pointer to the first element of the array (so it will be 4 on most platforms). You need to pass in the size of the array as an argument to the function because there is no way in C to find out the size of an array just from it's pointer.

    shmoove

  • #5
    Regular Coder
    Join Date
    Feb 2004
    Location
    Malaysia
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hi all,
    the thing is that i have an array, lets say:

    Code:
    array = {1,2,3,4,5,6,7,8};
    how am i gonna split it into half. and then how am i going to recognize it to come from which half of the splited array to combine it into 5,1,6,2,7,3,8,4???
    very lost here......
    Warm Regards,
    Mivec

  • #6
    New Coder
    Join Date
    Aug 2002
    Posts
    73
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I can't help with C, but maybe I can help with some of your logic.

    I think I would start with an array "x". And create a new temporary array "y" with length x.length. Choose random element from x and add it to y. Now remove the element from the x array, randomize, choose another element from x, add to y until you're done.

    I guess the trickiest part will be removing the element from the x array. Not sure if C has a mechanism for doing that.

  • #7
    New Coder
    Join Date
    Jan 2005
    Location
    INDIA
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well, removing a randomized element from the array, i dont thing is too difficult.
    why dont we just randomize the index, which is to be removed.
    next time, after generating a new random index,we can check for the element, whether already removed or not.
    go on until the last element is removed.
    we hav the 'random()' func in c for that.
    but do u think that it will generate a truely shuffeled array?

    u may try however.
    bye.

  • #8
    New Coder
    Join Date
    Nov 2004
    Location
    Netherlands
    Posts
    87
    Thanks
    0
    Thanked 0 Times in 0 Posts
    how am i gonna split it into half. and then how am i going to recognize it to come from which half of the splited array to combine it into 5,1,6,2,7,3,8,4???
    very lost here......
    Is this kind of what you were thinking of?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
    	char array[11] = "1234567890\0";
    	char arr_0[6], arr_1[6];
    	int len, i, j;
    	
    	puts(array);
    	len = strlen(array);
    	len /= 2;
    	
    	strncpy(arr_0, array, len);
    	strcpy(arr_1, (array+len));
    	
    	len = strlen(arr_0);
    	
    	for(i=0, j=0; i<len; i++, j++)
    	{
    		array[j] = arr_1[i];
    		j++;
    		array[j] = arr_0[i];
    	}
    	if(i < strlen(arr_1)) strcat(array, arr_1);
    	
    	puts(array);
    	
    	return 0;
    }
    Last edited by Dr. Evil; 01-31-2005 at 06:29 PM.

  • #9
    Regular Coder
    Join Date
    Feb 2004
    Location
    Malaysia
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    thanks dr.evil for posting and i din reply to u any sooner....i actually constructed my way and wanted to post it way earlier but i was too bz with other stuff....thanks anyway dr.evil
    Warm Regards,
    Mivec


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •