[LCP]senet.c - returning a ptr to a function

Ritesh Kala riteshr at vsnl.com
Sun Jul 29 14:54:05 UTC 2001


Always remember that main should return an int . Otherwise the behaviour is
undefined.
Secondly make it a habit to use as few glabal variables as possible.(x in
your program).
It is a much better option to pass thr data to the function.
Thirdly always try to make your code as general as possible. It will help
yuo in the long run.
I think a better program would be as follows:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

/*pass an array and the value to be set if total is zero to the function */
int sticks (int stic[],int size, int value_for_total_if_equals_zero)
{
 int i;
     int total =0;
     srand (time(NULL));

     for (i = 0; i < size; i++)
         stic[i] = (int)(2.0*(rand()/(RAND_MAX+1.0)));

     for (i = 0; i < size; i++)
         total = total + stic[i];



     return (total);
}
int main(void)
{
   int x;
   int stic[4];
   x = sticks(stic ,4 ,6);
   printf("\nThrow value: %d. \n", x);

   return 0;
}
> 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
>











More information about the linuxCprogramming mailing list