Initializing help system before first use

Using counters


Type: Programming
Rating: 2 (easy-medium)
Description: Using 'count' and 'as counter' separately (count1.mos) or in combination (count2.mos).
File(s): count1.mos, count2.mos
Data file(s): count2.dat


count1.mos
(!*******************************************************
   Mosel Example Problems
   ======================

   file count1.mos
   ```````````````
   Using counters (new in Mosel 3).
   
   (c) 2009 Fair Isaac Corporation
       author: S. Heipcke, June 2009
*******************************************************!)
model "counters 1"
 uses "mmsystem"

!**** Count odd numbers in a set and calculate their average value **** 
 declarations
  S: set of integer
 end-declarations
 
 S:= {1, 5, 8, -1, 4, 7, 2}
 
 writeln("Number of odd numbers in S: ", count(i in S | isodd(i)) )
 
 cnt:=0.0
 writeln("Average of odd numbers: ", 
         (sum(cnt as counter, i in S | isodd(i)) i) / cnt) 


!**** Count occurrences of a substring in a list of string ****
 declarations
  L: list of string
 end-declarations
 
 L:= ['a', 'ab', 'abc', 'da', 'bc', 'db']
 
 writeln("Occurences of 'b' in L: ", count(s in L | findtext(s, 'b', 1)>0) )

 scnt:=0
 forall(scnt as counter, s in L | findtext(s, 'b', 1)>0)
  writeln(scnt, ": ", s)

! The "as counter" may appear at any place among the indices
! (conditions are attached to indices), so we might as well have used:
(!
 forall(s in L | findtext(s, 'b', 1)>0, scnt as counter)
  writeln(scnt, ": ", s)
!)
  
end-model

count2.mos
(!*******************************************************
   Mosel Example Problems
   ======================

   file count2.mos
   ```````````````
   Using counters (new in Mosel 3).
   - Create a 'NODE' entry for every node that has
     at least 2 arcs
   - Combining 'count' and 'as counter'   
   
   (c) 2009 Fair Isaac Corporation
       author: S. Heipcke, June 2009
*******************************************************!)
model "counters 2"
 uses "mmsystem"
 
 declarations
  I: set of integer
  ARC: dynamic array(I,I) of boolean
  NODE: dynamic array(NSet: set of integer) of integer 
 end-declarations
 
 initializations from "count2.dat"
  ARC
 end-initializations
 
 ctnode:=0
 forall(ctnode as counter, j in I | 
        count(i in I | exists(ARC(i,j))) +
        count(i in I | exists(ARC(j,i))) >= 2) create(NODE(j))
		   
 writeln("Number of nodes created: ", ctnode) 
 writeln("Nodes: ", NSet)
 writeln("I: ", I)
  
end-model