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 />

Routine definition:<br />

function POLYN given A, XVAL and K<br />

define A as a 1-dimensional real array<br />

define XVAL as a real variable<br />

define K as an integer variable<br />

if K eq 0<br />

return with 0<br />

otherwise<br />

return with A(K) + XVAL * POLYN(A(*), X, K-1)<br />

end<br />

Notice that the interior call to POLYN uses K-1 as an argument instead of K. To illustrate how the<br />

routine works, the evaluation of the polynomial is described:<br />

3.3x 2 + 2.1x + 9.2 .<br />

Assume that the coefficients 3.3, 2.1, and 9.2 are stored in an array COEF, in the order COEF(1),<br />

COEF(2), and COEF(3), respectively. The polynomial is to be evaluated for the value x = 0.5.<br />

The function is called by the statement:<br />

let VALUE=POLYN(COEF(*), 0.5, 3)<br />

The polynomial is evaluated as:<br />

POLYN(COEF(*), 0.5, 3)<br />

= 9.2 + 0.5 * POLYN(COEF(*), 0.5, 2)<br />

= 9.2 + 0.5 * ( 2.1 + 0.5 * POLYN(COEF(*), 0.5, 1) )<br />

= 9.2 + 0.5 * ( 2.1 + 0.5 * ( 3.3 + 0.5 * POLYN(COEF(*), 0.5, 0) ) )<br />

= 9.2 + 0.5 * ( 2.1 + 0.5 * ( 3.3 + 0.5 * 0.0 ) )<br />

which evaluates to 11.075.<br />

A commonly used data structure in computing is the "tree" structure. One form of such a structure<br />

is a "binary tree," where each node within the tree may have a "left branch" and a "right branch"<br />

that represents links to other nodes in the tree. One node is chosen to represent the root. No node<br />

is directly linked to/from more than one other node. Any node may be reached by starting from the<br />

root and taking successive links at each node encountered. At the end of each possible path are<br />

"leaf" nodes that have no successor links. Binary trees are well suited to processing with recursive<br />

routines, since each node in such a tree may be considered as a "root" for a tree at a lower level within<br />

the hierarchy of nodes.<br />

An example of a recursive routine for destroying all the nodes of binary tree is shown below. The<br />

tree is constructed of two-element, one-dimensional arrays that point to each other. Data storage at<br />

the nodes may be disregarded for this example. To illustrate the tree-building process, the following<br />

program segment forms the root of a binary tree named TREE:<br />

78

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

Saved successfully!

Ooh no, something went wrong!