Re. : [LCP]senet.c - returning a ptr to a function
David Filion
filioda at videotron.com
Thu Jul 26 23:09:16 UTC 2001
Here's two versions I've come up with. One using an array and one without.
There is
probably an even more straight forward solution that soemone with more
experience
can come up with.
/* For option 1
** Use an array to hold the individula numbers then sum then into TOTAL.
*/
int
throw (void)
{
int throws [4];
int total;
srand (time(NULL));
for (int i = 0; i < 4; i++)
throws [i] = (int) (2.0*rand()/(RAND_MAX+1.0));
total = throws[0] + throws[1] + throws[2] + throws[3];
if (total == 0)
total = 6;
return (total);
}
/* Option 2 - No array
** Just keep adding the new result to TOTAL.
*/
int
throw (void)
{
int total = 0; /* Make sure to initialize */
srand (time(NULL));
for (int i = 0; i < 4; i++)
total += (int) (2.0*rand()/(RAND_MAX+1.0));
if (total == 0)
total = 6;
return (total);
}
Nick Croft <nicko at acay.com.au> on 2001-07-25 22:15:40
Pour : linuxcprogramming at lists.linux.org.au
cc : (ccc : David Filion/VSI/GVL)
Objet : [LCP]senet.c - returning a ptr to a function
Hi,
I'm learning C from one of those books that promise a lot in 21 days. One
thing I've learnt from this series is that there's plenty of help getting
started, but the exercises and advice thin out a bit when the going gets tough.
To consolidate my learning I've set myself the goal of writing an ascii game
based on a simple board game - Senet. Instead of sticks this game uses 4
throwing-sticks. Landing one way up gives 0, the other way up gives 1. The four
sticks are totalled, with 4x0 counting as a six. The character of the game
derives from the resulting chances, 2 most often, then 1 or 3, then 4 or 6.
Try as I might, I can't turn the function `sticks' in the following inelegant
piece into a for-loop with an array. I keep getting the address returned.
Unfortunately chapter 14 of the 21 day book has no answers to the few
exercises on returning pointers to an array.
______________________________________________
senet.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int sticks (void);
int x;
int main ()
{
x = sticks();
printf("\nThrow value: %d. \n", x);
return 0;
}
int sticks (void)
{
int throw1, throw2, throw3, throw4, total;
srand(time(NULL));
throw1 = (int) (2.0*rand()/(RAND_MAX+1.0));;
throw2 = (int) (2.0*rand()/(RAND_MAX+1.0));;
throw3 = (int) (2.0*rand()/(RAND_MAX+1.0));;
throw4 = (int) (2.0*rand()/(RAND_MAX+1.0));;
total = throw1 + throw2 + throw3 + throw4;
if (total == 0) { total = 6;
}
return total;
}
___________________________________________________
I'd love to have an nice for-loop and an array instead of the throw1-2-3-4
business.
Sorry for the simplicity, but it's where I'm at. Maybe while there's not
much traffic on the list you might care to help.
Thanks,
Nick
_______________________________________________
linuxcprogramming mailing list
linuxcprogramming at lists.linux.org.au
http://lists.linux.org.au/listinfo/linuxcprogramming
_______________________________________________
This is the Linux C Programming List
: http://lists.linux.org.au/listinfo/linuxcprogramming List
More information about the linuxCprogramming
mailing list