Problem
Generate the following output using Write() and Format(), putting random digits into the cells as follows:
+-------+-------+-------+ | 0 8 2 | 2 6 3 | 1 3 4 | | 1 4 1 | 8 1 3 | 8 3 7 | | 3 6 8 | 6 3 1 | 3 4 2 | +-------+-------+-------+ | 7 3 4 | 1 8 3 | 7 9 4 | | 8 7 0 | 1 1 5 | 0 5 0 | | 7 6 7 | 6 5 2 | 6 5 9 | +-------+-------+-------+ | 6 9 2 | 6 3 1 | 7 4 8 | | 5 5 9 | 6 3 0 | 6 9 7 | | 7 2 1 | 8 7 5 | 8 6 3 | +-------+-------+-------+
This output can be used for a sudoku puzzle, for example.
Solution
Listing 1: The Complete Model implemented in LPL [2]
model exerWrite "Exercise in Write I";
set i,j:=1..9;
integer parameter X{i,j} := Rnd(0,9);
Write('+-------+-------+-------+' n');
Write{i}('| %s| %s| %s|' n%s',
Format{j|j<=3}('%1d ',X),
Format{j|4<=j<=6}('%1d ',X),
Format{j|j>=7}('%1d ',X),
if(i=3 or i=6 or i=9,
Format('+-------+-------+-------+' n'),''));
end
References
[1] MatMod. Homepage for Learning Mathematical Modeling : https://matmod.ch.
[2] Hürlimann T. Reference Manual for the LPL Modeling Language, most recent version. https://matmod.ch/lpl/doc/manual.pdf.