Maximum difference between adjacent elements of list: Difference between revisions

m
→‎{{header|REXX}}: added code to align the output element pairs (for the maximum difference).
m (→‎{{header|REXX}}: fixed an HTML opening tag.)
m (→‎{{header|REXX}}: added code to align the output element pairs (for the maximum difference).)
Line 429:
parse arg $ /*obtain optional arguments from the CL*/
if $='' | S=="," then $= , /*Not specified? Then use the default.*/
'1,8,2,-3,0,1,1,-2.3,0,5.5,8,6,2,9,11,10,3' /*commas are optional,; blanks mayare beOK. */
dw= -10 /*the maximum differencewidth foundof any (soelement far)num. */
$= translate($, , ',') /*elide optional commas between numbers*/
#= words($) /*number of elements in the $ list. */
do i=1 for words($); x= word($, i) /*examine each element, ensure a number*/
@.i= x; w= max(w, length(x) ) /*assign element #; find maximum width.*/
if datatype(x, 'N') then iterate /*Is it numeric? Yes, then continue. */
say '***error*** element #'i " isn't numeric: " x
Line 442 ⟶ 443:
exit 13
end
sayn= 0 /*N: # sets of (difference) elements.*/
say ' list of elements: ' space($) /*show the list of numbers in list.*/
say ' number of elements: ' # /*show " number " elements " " */
aad= -1; bb= /*thed: AAmaximum difference andfound (so BB list(sfar); # pairs. */
d= -1 /*maximum difference found (so far). */
do j=1 for #-1; jp= j + 1; a= @.j /*obtain the "A" element. */
b= @.jp /* " " "B" " */
Line 451 ⟶ 452:
if newD<d then iterate /*Is this not a new max difference ? */
d= newD /*assign new maximum difference, and */
aan= aan a;+ 1 bb= bb b /* the elements that had /*bump the max# diffof (difference) sets. */
aa.n= a; bb.n= b /* the elements that had the max diff.*/
end /*j*/
say
say
say 'The maximum difference of the elements in the list is: ' d
do k=1 for words(aa)n
say 'and is between the elements: ' wordright(aa.k, kw) " and " wordright(bb.k, kw)
end /*k*/
exit 0 /*stick a fork in it, we're all done. */</lang>
Line 465 ⟶ 467:
 
The maximum difference of the elements in the list is: 7
and is between the elements: 1 and 8
and is between the elements: 2 and 9
and is between the elements: 10 and 3
</pre>