home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / nvdc87 / lcv1n1 / mean-c.c < prev    next >
Text File  |  1987-08-12  |  916b  |  34 lines

  1. /* Listing 3 - C function called by Turbo Prolog to find the mean.*/
  2.  
  3. struct ilist {           /* Declare a Turbo Prolog list in C */
  4.     char functor;
  5.     double val;
  6.     struct ilist *next;
  7. };
  8.                 
  9. struct ifunc {          /* Declare a Turbo Prolog functor in C */
  10.     char type;
  11.     double value;
  12. };
  13.  
  14. void mean_0(struct ilist *in, struct ifunc **out) {
  15.     int count = 0;
  16.     double y = 0, z = 0;
  17.     
  18.     if (in->functor !=1)
  19.     fail_cc();              /* 1 indicates a list element. */
  20.  
  21.     while(in->functor !=2) {   /* 2 indicates an empty list.  */
  22.        y = y + in->val;        /* Keep a running sum of the list.  */
  23.        count = count+1;
  24.        in = in->next;          /* Get the next member of the list. */
  25.     }
  26.  
  27.     z = y/count;            /* z = the mean.  */
  28.  
  29.     *out = (struct ifunc *) palloc (sizeof(struct ifunc));
  30.     (*out)->value = z;
  31.     (*out)->type = 1;
  32. }
  33.  
  34.