| lcdiv2.mos | 
| (!******************************************************
   Mosel User Guide Example Problems
   ================================= 
   file lcdiv2.mos 
   ``````````````` 
   Recursive function calls.
 
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2001
*******************************************************!)
model Lcdiv2
 function lcdiv(A,B:integer):integer
  if(A=B) then
   returned:=A
  elif(A>B) then
   returned:=lcdiv(B,A-B)
  else
   returned:=lcdiv(A,B-A)
  end-if   
 end-function
 declarations
  A,B: integer
 end-declarations
 write("Enter two integer numbers:\n  A: ")
 readln(A)
 write("  B: ")
 readln(B)
 writeln("Largest common divisor: ", lcdiv(A,B))
end-model
 | 
| 
 | 
| primefct.mos | 
| (!******************************************************
   Mosel User Guide Example Problems
   ================================= 
   file primefct.mos 
   `````````````````
   Working with sets. Function returning a set.
 
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, Nov. 2005
*******************************************************!)
model "Prime (fct)"
 parameters
  LIMIT=100                     ! Search for prime numbers in 2..LIMIT
 end-parameters
 forward function calc_prime(l: integer): set of integer
 declarations
  P: set of integer             ! Set of prime numbers
 end-declarations
 
 writeln("Prime numbers between 2 and ", LIMIT, ":")
 P:= calc_prime(LIMIT)          ! Calculate prime numbers
 writeln(P)
 writeln(" (", getsize(P), " prime numbers.)")
!*****************************************************************
 function calc_prime(l: integer): set of integer
  declarations
   SNumbers: set of integer     ! Set of numbers to be checked
   SPrime: set of integer       ! Set of prime numbers
  end-declarations
  SNumbers:={2..LIMIT} 
  n:=2
  repeat
    while (not(n in SNumbers)) n+=1
    SPrime += {n}               ! n is a prime number
    i:=n
    while (i<=LIMIT) do         ! Remove n and all its multiples
      SNumbers-= {i}
      i+=n
    end-do
  until SNumbers={}    
  returned:= SPrime
 end-function
 
end-model
 | 
| 
 | 
| qsort1.mos | 
| (!******************************************************
   Mosel User Guide Example Problems
   ================================= 
   file qsort1.mos 
   ``````````````` 
   'forward' definition of subroutines.
 
   (c) 2008 Fair Isaac Corporation
       author: Y. Colombani, 2001
*******************************************************!)
model "Quick sort 1"
 parameters
  LIM=50
 end-parameters
 forward procedure qsort_start(L:array(range) of integer)
 declarations
  T:array(1..LIM) of integer
 end-declarations
 forall(i in 1..LIM) T(i):=round(.5+random*LIM)
 writeln(T)
 qsort_start(T)
 writeln(T)
 
 
! Swap the positions of two numbers in an array
 procedure swap(L:array(range) of integer,i,j:integer)
  k:=L(i)
  L(i):=L(j)
  L(j):=k
 end-procedure
! Main sorting routine
 procedure qsort(L:array(range) of integer,s,e:integer)
  v:=L((s+e) div 2)              ! Determine a partitioning value
  i:=s; j:=e
  repeat                         ! Partition the array into two subarrays
   while(L(i)<v) i+=1
   while(L(j)>v) j-=1
   if i<j  then
    swap(L,i,j)
    i+=1; j-=1
   end-if
  until i>=j
                                 ! Recursively sort the two subarrays
  if j<e and s<j then qsort(L,s,j); end-if
  if i>s and i<e then qsort(L,i,e); end-if
 end-procedure
! Start of the sorting process 
 procedure qsort_start(L:array(r:range) of integer)
  qsort(L,getfirst(r),getlast(r))
 end-procedure
end-model
 | 
| 
 | 
| qsort2.mos | 
| (!******************************************************
   Mosel User Guide Example Problems
   ================================= 
   file qsort2.mos 
   ``````````````` 
   Overloading of subroutines.
 
   (c) 2008 Fair Isaac Corporation
       author: Y. Colombani, 2001
*******************************************************!)
model "Quick sort 2"
 parameters
  LIM=50
 end-parameters
 forward procedure qsort(L:array(range) of integer)
 declarations
  T:array(1..LIM) of integer
 end-declarations
 forall(i in 1..LIM) T(i):=round(.5+random*LIM)
 writeln(T)
 qsort(T)
 writeln(T)
 
 
! Swap the positions of two numbers in an array
 procedure swap(L:array(range) of integer,i,j:integer)
  k:=L(i)
  L(i):=L(j)
  L(j):=k
 end-procedure
! Main sorting routine
 procedure qsort(L:array(range) of integer,s,e:integer)
  v:=L((s+e) div 2)              ! Determine a partitioning value
  i:=s; j:=e
  repeat                         ! Partition the array into two subarrays
   while(L(i)<v) i+=1
   while(L(j)>v) j-=1
   if i<j  then
    swap(L,i,j)
    i+=1; j-=1
   end-if
  until i>=j
                                 ! Recursively sort the two subarrays
  if j<e and s<j then qsort(L,s,j); end-if
  if i>s and i<e then qsort(L,i,e); end-if
 end-procedure
! Start of the sorting process 
 procedure qsort(L:array(r:range) of integer)
  qsort(L,getfirst(r),getlast(r))
 end-procedure
end-model
 | 
| 
 | 
| shsortfct.mos | 
| (!******************************************************
   Mosel User Guide Example Problems
   ================================= 
   file shsortfct.mos 
   `````````````````` 
   Combining the 'repeat-until', 'while-do', and 
   'forall-do' loops. Function returning an array.
 
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, Nov. 2005
*******************************************************!)
model "Shell sort (fct)"
 declarations
  N: integer                     ! Size of array ANum
  R: range                       ! Index range of the arrays
  ANum: array(R) of real         ! Unsorted array of numbers
  ASorted: array(R) of real      ! Sorted array of numbers
 end-declarations
 forward function shsort(ANum: array(R) of real): array(R) of real
 N:=50
 forall(i in 1..N)
  ANum(i):=round(random*100)
 writeln("Given list of numbers (size: ", N, "): ")
 forall(i in 1..N) write(ANum(i), " ")
 writeln
 
 ASorted:= shsort(ANum)
 
 writeln("Ordered array: ")
 forall(i in 1..N) write(ASorted(i), " ")
 writeln
!***************************************************************
 function shsort(ANum: array(R) of real): array(R) of real
  returned:=ANum                 ! Copy the array to be sorted 
                                 ! (Return the sorted array) 
  inc:=1                         ! Determine the starting increment
  repeat                         
    inc:=3*inc+1
  until (inc>N)  
 
  repeat                         ! Loop over the partial sorts
    inc:=inc div 3
    forall(i in inc+1..N) do     ! Outer loop of straight insertion
      v:=returned(i)
      j:=i
      while (returned(j-inc)>v) do   ! Inner loop of straight insertion
        returned(j):=returned(j-inc)
        j -= inc
        if j<=inc then break; end-if
      end-do
      returned(j):= v     
    end-do  
  until (inc<=1)
 end-function
 
end-model
 | 
| 
 | 
| subrout.mos | 
| (!*******************************************************
   Mosel User Guide Examples
   =========================
   file subrout.mos
   ````````````````
   Simple subroutines.
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2001, rev. May 2011
  *******************************************************!)
model "Simple subroutines"
 declarations
  a:integer
 end-declarations
 function three:integer
  returned := 3
 end-function
 function times_two(b:integer):integer
  returned := 2*b
 end-function
 procedure print_start
  writeln("The program starts here.")
 end-procedure
 procedure hide_a_1
  declarations
   a: integer
  end-declarations
 
  a:=7
  writeln("Procedure hide_a_1: a = ", a) 
 end-procedure
 procedure hide_a_2(a:integer)
 a:=2
  writeln("Procedure hide_a_2: a = ", a) 
 end-procedure
(! This version generates an error because 'a' is declared twice
 procedure hide_a_3(a:integer)
  declarations
   a: integer
  end-declarations
  a := 15
  writeln("Procedure hide_a_3: a = ", a) 
 end-procedure
!)
! Corrected version of hide_a_3
 procedure hide_a_3(aa:integer)
  declarations
   a: integer
  end-declarations
  a := 15
  writeln("Procedure hide_a_3: a = ", a, ", aa = ", aa) 
 end-procedure
 print_start 
 a:=three
 writeln("a = ", a)
 a:=times_two(a)   
 writeln("a = ", a)
 hide_a_1
 writeln("a = ", a)
 hide_a_2(-10)
 writeln("a = ", a)
 hide_a_3(a)
 writeln("a = ", a)
end-model 
 | 
| 
 |