home *** CD-ROM | disk | FTP | other *** search
- /* Listing 5 - Turbo C function to be called from Turbo Prolog
- to calculate the variance. */
-
- struct ilist { /* Declare a Turbo Prolog list in C. */
- char functor;
- double val;
- struct ilist *next;
- };
-
- struct ifunc {
- char type;
- double value;
- };
-
- void variance_0(struct ilist *in, struct ifunc **out) {
- int count = 0;
- double y = 0, dev = 0, square = 0, squares=0, z = 0, var = 0;
- struct ilist *dup_list;
-
- if (in->functor !=1) fail_cc();
- dup_list = in; /* Save the address of the head */
- /* of the list. */
-
- while(in->functor !=2) { /* Find mean first. */
- y = y + in->val;
- count = count+1;
- in = in->next;
- }
- z = y/count;
- /* Then find variance. */
- while(dup_list->functor !=2) {
- dev = z - dup_list->val;
- square = dev * dev;
- squares = squares + square;
- dup_list = dup_list->next; /* Get next list element. */
- }
-
- var = squares/count; /* Get var and return it. */
-
- *out = (struct ifunc *) palloc (sizeof(struct ifunc));
- (*out)->value = var;
- (*out)->type = 1;
- }
-