Problems with sorting arrays-Please Help 
Author Message
 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



Sat, 28 Apr 2001 03:00:00 GMT  
 Problems with sorting arrays-Please Help

Quote:



>> The program I am trying to write (In C)  includes a "??? shuffle(???);"
>> function to shuffle a deck of cards.

> this almost qualifies as a FAQ.

Indeed, though it's currently only in the book-length version:

13.19:  How can I return a sequence of random numbers which don't repeat
        at all?

A:      What you're looking for is often called a "random permutation"
        or "shuffle."  One way is to initialize an array with the values
        to be shuffled, then randomly interchange each of the cells with
        another one later in the array:

                int a[10], i, nvalues = 10;

                for(i = 0; i < nvalues; i++)
                    a[i] = i + 1;

                for(i = 0; i < nvalues-1; i++) {
                    int c = randrange(nvalues-i);
                    int t = a[i]; a[i] = a[i+c]; a[i+c] = t;    /* swap */
                }

        where randrange(N) is rand() / (RAND_MAX/(N) + 1) or one of the
        other expressions from question 13.16.

        References: Knuth Sec. 3.4.2 pp. 137-8

                                                Steve Summit



Tue, 01 May 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. bubble sort problems.Please help

2. Please help-still having problems with bubble sort

3. NEED HELP WITH PRITING AN ARRAY, PLEASE PLEASE HELP

4. Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!Please help!!!!

5. problems with array allocation and destruction::: HELP PLEASE

6. Arrays with no pointers - problem (please help!)

7. Problem with array's PLEASE HELP

8. Problems with array elements from user input, please help

9. array problem PLEASE HELP

10. Urgent please help LPSTR Array function passing problem

11. help please: problems with arrays

12. HELP Please Please (malloc on an array)

 

 
Powered by phpBB® Forum Software