
Problems with sorting arrays-Please Help
The program I am trying to write (In C) includes a "??? shuffle(???);"
function to shuffle a deck of cards. It does so by filling the deck with
the numbers 0-51. It fills a parallel array with random numbers. It then
sorts the random number array, swapping corresponding positions in the deck.
By the time the random number array is sorted, the deck will be
unsorted---shuffled.
I successfully filled the arrays by writing the following code:
void main(void)
{
int deck[52], index, r_deck[52];
srand((unsigned)time(NULL));
for(index=0; index<52; ++index)
{ deck[index]=index;
r_deck[index]=rand();
}
Quote:
}
The problems I having has to do with the sorting of the r_deck[ ] and how to
declare this shuffle function within main. Everytime I try to add a
separate function to the top of my code I get the following error message
from my compiler: ex: void shuffle(r_deck[ ]);
"Cannot create pre-compiled header: write failed"
Does this have something to do with the library functions I'm using or the
way I'm writing the function?
This is what I've come up with for the sorting code, I haven't been able to
test it yet because of the above errors and was wondering if you think these
could all fit together somehow.
void Shuffle(r_deck[ ])
{ int top=o, position, temp;
for(top=index-1; top>0; --top)
for(position=0; position<top; ++position)
if(r_deck[position+1] < r_deck[position])
{ temp=r_deck[postion];
r_deck[position]=r_deck[position+1];
r_deck[position+1]=temp;
}
Quote:
}
Any suggestions you can give me would be greatly appreciated. I was doing
fine with this C programming class for a while, and then started getting
confused when it came to functions and arrays.
Thanks again in advance.
Sincerely,
Kriss