...

View Full Version : perfect shuffle error



mivec
01-30-2005, 10:16 AM
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:




#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...

Dr. Evil
01-30-2005, 10:34 AM
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.

mivec
01-30-2005, 11:16 AM
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:



strsize = sizeof(strnum);


thanks again

shmoove
01-30-2005, 12:39 PM
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

mivec
01-30-2005, 02:23 PM
hi all,
the thing is that i have an array, lets say:



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......

rynox
01-31-2005, 02:14 PM
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.

abhishek.in
01-31-2005, 03:22 PM
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.

Dr. Evil
01-31-2005, 05:27 PM
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?



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

mivec
02-13-2005, 05:57 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum