18.10.2014 Views

SIMSCRIPT II.5 Programming Language

SIMSCRIPT II.5 Programming Language

SIMSCRIPT II.5 Programming Language

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>SIMSCRIPT</strong> <strong>II.5</strong> <strong>Programming</strong> <strong>Language</strong><br />

2.27.2 A Data Analysis Program: 2<br />

To illustrate the way in which the logical elements of a program may be separated into routines, this<br />

example repeats the computations of the previous problem, but instead of computing the averages<br />

of the data values, computes the average of a function of the values. As the function to be used may<br />

conceivably vary for different sets of data, or under different circumstances, the function evaluation<br />

is logically partitioned from the main body of the program, and written as a function routine. It may<br />

then be both separately tested and altered without having to modify the main routine. Note that the<br />

function routine must now be declared within the program preamble.<br />

Program 2-3.<br />

_______________________________________________________________________________<br />

preamble<br />

define VALUE as a real function<br />

end<br />

main<br />

define LIST as a 1-dimensional array<br />

define I,J, SEQ.LEN and N as integer variables<br />

define TOLERANCE.VALUE, SUM and AVERAGE as real variables<br />

read N<br />

reserve LIST(*) as N<br />

read LIST<br />

read TOLERANCE.VALUE<br />

for SEQ.LEN = 1 to N-1,<br />

for I = 1 to N-SEQ.LEN<br />

do<br />

let SUM = 0<br />

for J = 0 to SEQ.LEN<br />

add VALUE(LIST(I+J)) to SUM<br />

let AVERAGE = SUM/(SEQ.LEN+1)<br />

if AVERAGE is less than TOLERANCE.VALUE<br />

print 1 line with I, I + SEQ.LEN and AVERAGE thus<br />

ITEMS *** THROUGH *** HAVE AN AVERAGE OF **.***<br />

always<br />

loop<br />

release LIST(*)<br />

stop<br />

end<br />

routine VALUE given VARIABLE<br />

if VARIABLE is less than -1000<br />

return with -1<br />

otherwise<br />

if VARIABLE is greater than 1000<br />

return with 1<br />

otherwise<br />

return with VARIABLE/1000<br />

end<br />

_______________________________________________________________________________<br />

90

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!