Assignment two questions and answers

Here are some questions and answers about assignment two. Suggestions for additions to this list are welcome (via e-mail).


Q: Can we use the shuffling algorithm on page 150 of the textbook?

A: The problem here is a bit different and you should use the shuffling algorithm I specified rather than the code on page 150.

The code on page 150 is actually pretty bad in that it is not guaranteed to make progress each time around the loop. If it happens to pick a card which is already in the hand, it just loops around and hopes for better luck next time. For a hand which is a small number of cards this might be acceptable (although the algorithm specified in the assignment 2 handout is better), but for dealing out the entire deck as in the war.c program, getting that 52nd card could take a very long time. You will find the algorithm on page 150 infeasible for the "war" program.


Q: I don't understand that seed.c thing.

A: Sorry, I guess I made it too complex. The important bit is that it calls srandom(). You can just take the srandom() call from seed.c. Put it near the top of your main() somewhere. Here's a simpler version which you can use instead.

Your program is not compiled with my seed.c. It has to stand on its own. You can copy from that seed.c file, or you can ignore it.

Here's the idea behind "seeding" a pseudo-random number generator:

A pseudo-random number generator (PRNG) is not random. It is a deterministic algorithm, like everything on a computer. So if it starts in the same initial state, it yields the same series of numbers.

"Seeding" a PRNG constitutes giving it some starting data which is different every time. In this case I suggest using the time in seconds, which will be different for different runs of the program.

You don't have to seed the random number generator at all. In that case, you will find that the game turns out the same every time, at least every time on the same computer. But this is ok for this assignment.


Q: Can we just use the seed method on page 150 of the textbook?

A: Well, you can just call srandom(time(NULL)) if you like, in accordance with the example there; that's the main thing which seed.c does. I suppose you can use srand and rand instead of srandom and random, but random is better, so why not use it.


Q: Can I call seed() (or srandom()) more often to get better random numbers?

A: No, only call it once, at the beginning of the program, not in a loop. After that, you get better random numbers by not calling it again. Seeding the random number generator with the same seed starts the sequence over again. More is not better.


Q: When the users' hands are displayed, is the top of the pile the left or right of the display?

A: Either one is fine. The important thing is that the side of the hand to which the cards are added when you win cards is opposite to the side of the hand from which cards are turned up to play the next round. That is, if you put down 10H and win it, then you don't immediately put 10H back down again.


Q: What happens if one of the players' cards run out while they are putting down cards for the "war", or if they don't have a card left to be compared?

A: When the children play this game, they often let the player just show their last card; but for this program, let's just say that that player loses at that point, which makes the program simpler.


Q: If the two players have the same top card value and go to war, and then the next cards they compare also have the same value, what happens?

A: It's another war, and your cousin gets very giggly. This can repeat for any number of times, although it's rare to be more than one or two. The eventual winner takes all the cards, which can be a couple dozen (or even more).


Q: Is there a limit to the number of wars-within-wars you can have? If not, does this need to be a recursive function?

A: There is no limit, but it does not need to be a recursive function. I think you will find it most straightforward to write it with a loop. On the other hand, there do exist recursive solutions. In any case, this is indeed one of the more difficult parts of the assignment.

There is now a suggestion for an outline of an algorithm for dealing with this on the algorithm suggestions page.


Q: When there's a "war", does this all count as part of the same "turn"? This affects the output at the end of the game.

A: Since the assignment handout didn't specify, either interpretation is acceptable, but I propose that we say that a war counts as a continuation of the same turn.


Q: What is the error "warning: implicit declaration of the function 'random'"?

A: random() and srandom() are declared in stdlib.h. You need to #include <stdlib.h>.

Unfortunately, "gcc -ansi" seems to turn off the declaration of random() in stdlib.h! I'm not sure of the best way to deal with this right now. We will be compiling your program with "gcc -Wall" anyway, so it will indeed get the definition in stdlib.h when we compile it for testing. Just make sure you #include <stdlib.h>.


Q: My randomization doesn't seem to be very good.

A: I wrote the wrong randomization algorithm on the handout. Sorry. There is a correction at http://www.dgp.toronto.edu/~ajr/180/a2/correction.html. It's too late for me to change the assignment now, so you can use either the algorithm on the handout or the correct algorithm on this correction web page.


Q:

A: All array use in C involves pointers, so yes. But you may be making this unnecessarily difficult.

I wonder if you are writing something like this (to assign 38 to array element 3, where the call is f(a)):

    void f(int *p)
    {
        *(p + 3) = 38;
    }
If so, you should be writing
    void f(int *p)
    {
        p[3] = 38;
    }
Remember that a[e] is short for *(a+e). We normally use the array-subscripting notation for arrays rather than writing the more explicit pointer arithmetic. They're equivalent, but the array syntax is slightly easier, or perhaps much easier if you're uneasy (hee hee) about pointers.


[main course page]