home *** CD-ROM | disk | FTP | other *** search
- /* Listing 3 - C function called by Turbo Prolog to find the mean.*/
-
- struct ilist { /* Declare a Turbo Prolog list in C */
- char functor;
- double val;
- struct ilist *next;
- };
-
- struct ifunc { /* Declare a Turbo Prolog functor in C */
- char type;
- double value;
- };
-
- void mean_0(struct ilist *in, struct ifunc **out) {
- int count = 0;
- double y = 0, z = 0;
-
- if (in->functor !=1)
- fail_cc(); /* 1 indicates a list element. */
-
- while(in->functor !=2) { /* 2 indicates an empty list. */
- y = y + in->val; /* Keep a running sum of the list. */
- count = count+1;
- in = in->next; /* Get the next member of the list. */
- }
-
- z = y/count; /* z = the mean. */
-
- *out = (struct ifunc *) palloc (sizeof(struct ifunc));
- (*out)->value = z;
- (*out)->type = 1;
- }
-