@q Define a macro so that the pound character can go in a URL. @> @q This should be in separate macro file and included here using @> @q the TeX \input macro but it seems a shame to have a separate @> @q file for a single macro! @> {\catcode`\#=12 \gdef\POUND/{#}} % # in a URL @* Introduction. This is an adaptation of \pdfURL{Mike~Burn's~Sneaker~Program}{http://netgeek.ws/programs/sneaker/\POUND/c} using Knuth's \pdfURL{CWEB}{http://www-cs-staff.stanford.edu/\TILDE/knuth/cweb.html} tool for ``literate programming''. For information on ``literate programming'' see \pdfURL{Daniel~Mall's~website}{http://www.literateprogramming.com}. @q'@> To produce a \CEE/ program file run the command {\tt ctangle} on this file. To produce a \TeX\ file run the program {\tt cweave} on this file. To \TeX\ this file run the program {\tt pdftex} on the \TeX\ file that was output by {\tt cweave}. @*2 Include the usual headers. @c #include #include #include @*2 Declare an array that lists the various sneaker colours. @d SNEAKERS 7 @d LAST_SNEAKER 6 @c static char *colours[SNEAKERS] = {"red","orange","yellow","green","blue","khaki","black"}; @* Main Program. This program picks two random sneaker colours from the list in |colours| and outputs the result. @c int main (int argc, char **argv) { int sneaker_one; int sneaker_two; @; @; @; @; return 0; } @*2 Pick the first colour. Select a random number with a maximum value that is less than or equal to the maximum number of colors in the list. @= sneaker_one = rand() % LAST_SNEAKER; @*2 Pick the second colour. To get the second random number we will reduce the maximum size of the random number by one. If we happen to get the value that was already chosen then |sneaker2| will be set to the value |LAST_SNEAKER|. @= sneaker_two = rand() % (LAST_SNEAKER - 1); if (sneaker_two == sneaker_one) sneaker_two = LAST_SNEAKER; @*2 Output the results. @= printf("Sneakers %s, %s\n", colours[sneaker_one], colours[sneaker_two]); @*2 Generate a new random number seed each time the program is run. This is down by seeding the random number generator with the epoch number returned by the time command. @= srand(time (0));