MODULE count; (*--------------------------------------------------------- File name: count.pm Author: AG, MM Date: 5/18/05 Problem: This program counts the number of 0's on a line. In this example, the line starts filled with random numbers. Example of Execution: Line is: 4 5 5 7 2 4 2 1 4 5 0 9 3 5 Number of 0: 1 Cycles: 1 assignment + 1 REDUCE *) (*---------------------------------------------------------- CONFIGURATION ----------------------------------------------------------*) CONST MAXCELLS=1024; (* MAXCELLS cells in one line *) CONFIGURATION chain[0..(MAXCELLS - 1)]; CONNECTION left: chain [i] -> chain[(i-1) MOD MAXCELLS]; right: chain[i] -> chain[(i+1) MOD MAXCELLS]; (*---------------------------------------------------------- VARIABLES ----------------------------------------------------------*) VAR L: chain OF INTEGER; (* Line always "L# *) RightLimit, MaxVal, Count, Item: INTEGER; (*---------------------------------------------------------- MAIN ----------------------------------------------------------*) BEGIN (* initializing data*) L := ID( chain ); RightLimit := 15; MaxVal := 10; (* Maximum value for random *) Item:=0; (* Item to count is 0 *) IF L < RightLimit THEN (* work only in selected first 15 cells *) L := RandomInt(chain ) MOD MaxVal; (* Put random numbers in L *) (*printing*) WriteString( "Line is: ");WriteInt( L , 2);WriteLn; IF L = Item THEN L:= 1; Count:= REDUCE.SUM( L ); END; (* IF *) (*printing*) WriteString( "Number of 0: "); WriteInt(Count, 2); END; (* IF *) END count.