home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power Programming
/
powerprogramming1994.iso
/
progtool
/
microcrn
/
issue_38.arc
/
TIDBIT38.FIG
< prev
Wrap
Text File
|
1987-09-21
|
3KB
|
161 lines
/* Figure 1 -- Statistics in PROLOG */
DOMAINS
real_list=real*
DATABASE
answer(real)
answer2(real)
data(integer,real_list)
PREDICATES
main
process(real_list)
process2(real_list)
mean(real_list,real,integer)
sd(real_list,real,real,integer)
CLAUSES
main:-
write("Enter file that contains data."),
readln(Filename),
consult(Filename),
data(1,List),
process(List), /* Pass the list to a function */
process2(List). /* for processing. */
process(List):- /* This function sets up for the mean.*/
write("Start"), /* Start timing. */
N=0,
S=0,
mean(List,S,N), /* Pass the list & variables to mean.*/
answer(Mean), /* Get the answer from storage. */
write(Mean),nl.
process2(List):- /* Set up to calculate variance & */
answer(Mean), /* sd. */
sd(List,Mean,0,0), /* Pass list and mean (which we */
answer2(Var), /* use to calculate the vari & sd.*/
write(Var),nl, /* Get variance from storage. */
Sdev = sqrt(Var), /* Find sd (sqrt of variance). */
write(Sdev).
mean([H|T],S,N):- /* Find the mean recursively. */
Y=H+S,
N2=N+1,
mean(T,Y,N2).
mean([],S,N):-
Z=S/N,
assert(answer(Z)). /* Store the result in a db. */
sd([H|T],Mean,Squares,N):- /* Find vari/sd recursively. */
Dev = Mean - H,
Square = Dev * Dev,
NewSquares = Squares + Square,
N2 = N + 1,
sd(T,Mean,NewSquares,N2).
sd([],Mean,Squares,N2):-
Var = Squares/(N2-1),
assert(answer2(Var)).
GOAL
main.
/* Figure 2 -- Statistics in C */
#include <stdio.h>
void stat(double *list);
main()
{
double list[7000];
double i;
for(i = 0; i < 7000; i++) /* Create a list to process. */
list[i]=i + 1;
stat(list); /* Pass the list to stat function.*/
}
void stat(double *list)
{ int i,c;
double x,z,dev,square,squares,var,sd;
double sqrt(double sd);
x = squares = 0;
puts("Start"); /* Start timing. */
/* Read array elements. */
for(i = 0; i < 7000; i++){
x = list[i] + x;
}
z = x/i; /* Mean = sum of list/count. */
printf("%f\n",z);
/* Calculate variance. */
for(i = 0; i < 7000; i++){
dev = z - list[i];
square = dev * dev;
squares = squares + square;
}
var = squares/(i-1);
printf("%f\n",var);
sd = sqrt(var); /* Find sd. */
printf("%f\n",sd);
}
/* Figure 3 -- Statistics in Pascal */
program stat;
type
L = array[1..7000] of real;
var
I : integer;
List : L;
X,Z,Dev,Square,Squares,Vari,Sd : real;
begin
For I := 1 to 7000 do { Create a list to process. }
List[I] := I;
X := 0;
Squares := 0;
Dev := 0;
Writeln('start'); { Start timing. }
For I := 1 to 7000 do { Read array elements. }
X := X + List[I];
Z := X/I; { Find mean. }
writeln(Z);
For I := 1 to 7000 do { Calculate variance. }
begin
Dev := Z - List[I];
Square := Dev * Dev;
Squares := Squares + Square;
end;
Vari := Squares/(I - 1);
writeln(Vari);
Sd := sqrt(Vari); { Find sd. }
writeln(Sd);
end.