Anonymous user
Maximum difference between adjacent elements of list: Difference between revisions
Maximum difference between adjacent elements of list (view source)
Revision as of 23:44, 18 July 2021
, 2 years ago→{{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
$= 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
say ' list of elements: ' space($) /*show the list of numbers in list.*/
say ' number of elements: ' # /*show " number " elements " " */
▲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 */
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
say 'and is between the elements: '
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>
|