How to resolve the algorithm Greatest element of a list step by step in the REXX programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Greatest element of a list step by step in the REXX programming language
Table of Contents
Problem Statement
Create a function that returns the maximum value in a provided set of values, where the number of values may not be known until run-time.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Greatest element of a list step by step in the REXX programming language
Source code in the rexx programming language
/*REXX program finds the greatest element in a list (of the first 25 reversed primes).*/
$ = reverse(2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97)
say 'list of numbers = ' $ /*show the original list of numbers. */
big=word($, 1) /*choose an initial biggest number. */
# = words($); do j=2 to # /*traipse through the list, find max. */
big=max(big, word($, j) ) /*use the MAX BIF to find the biggie.*/
end /*j*/
say /*stick a fork in it, we're all done. */
say 'the biggest value in a list of ' # " numbers is: " big
/*REXX program finds the greatest element in a list (of the first 25 reversed primes).*/
@.=; @.1 = 2; @.2 = 3; @.3 = 5; @.4 = 7; @.5 =11; @.6 =31; @.7 =71
@.8 =91; @.9 =32; @.10=92; @.11=13; @.12=73; @.13=14; @.14=34
@.15=74; @.16=35; @.17=95; @.18=16; @.19=76; @.20=17; @.21=37
@.22=97; @.23=38; @.24=98; @.25=79
big=@.1 /*choose an initial biggest number. */
do #=2 while @.#\=='' /*traipse through whole array of nums. */
big = max(big, @.#) /*use a BIF to find the biggest number.*/
end /*#*/
/*stick a fork in it, we're all done. */
say 'the biggest value in an array of ' #-1 " elements is: " big
/*REXX program finds the greatest element in a list of numbers entered at the terminal*/
say '────────────────── Please enter a list of numbers (separated by blanks or commas):'
parse pull $; #=words($) /*get a list of numbers from terminal. */
$=translate($, , ',') /*change all commas (,) to blanks. */
big=word($,1); do j=2 to # /*traipse through the list of numbers. */
big=max(big, word($,j)) /*use a BIF for finding the max number.*/
end /*j*/
say /*stick a fork in it, we're all done. */
say '────────────────── The biggest value in the list of ' # " elements is: " big
···; do j=2 to #; _=word($,j)
if _>big then big=_
end /*j*/
/* REXX ***************************************************************
* If the list contains any character strings, the following will work
* Note the use of >> (instead of >) to avoid numeric comparison
* Note that max() overrides the builtin function MAX
* 30.07.2013 Walter Pachl
**********************************************************************/
list='Walter Pachl living in Vienna'
Say max(list)
list='8 33 -12'
Say max(list)
Exit
max: Procedure
Parse Arg l
max=word(l,1)
Do i=2 To words(l)
If word(l,i)>>max Then
max=word(l,i)
End
Return max
You may also check:How to resolve the algorithm N-queens problem step by step in the Tcl programming language
You may also check:How to resolve the algorithm Palindrome detection step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Primes - allocate descendants to their ancestors step by step in the C++ programming language
You may also check:How to resolve the algorithm Zero to the zero power step by step in the Sidef programming language
You may also check:How to resolve the algorithm Totient function step by step in the BASIC programming language