Greatest element of a list: Difference between revisions

no edit summary
(Add LDPL)
imported>Lacika7
No edit summary
 
(9 intermediate revisions by 8 users not shown)
Line 961:
'''See also:''' [[#BBC BASIC|BBC BASIC]], [[#Liberty BASIC|Liberty BASIC]], [[#PowerBASIC|PowerBASIC]], [[#PureBasic|PureBasic]], [[#Run BASIC|Run BASIC]], [[#TI-89 BASIC|TI-89 BASIC]], [[#Visual Basic|Visual Basic]]
 
==={{header|BASIC256}}===
{{trans|Yabasic}}
<syntaxhighlight lang="freebasic">l$ = "1,1234,62,234,12,34,6"
Line 975:
 
print "Alphabetic order: "; m$; ", numeric order: "; m</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|Minimal BASIC}}===
{{trans|Quite BASIC}}
<syntaxhighlight lang="qbasic">10 PRINT "HOW MANY ITEMS? "
20 INPUT N
30 FOR J = 0 TO N-1
40 PRINT "VALUE OF ITEM #";J
50 INPUT T
60 LET A(J) = T
70 NEXT J
80 LET C = A(0)
90 LET I = 0
100 FOR J = 1 TO N-1
110 IF A(J) > C THEN 130
120 GOTO 150
130 LET C = A(J)
140 LET I = J
150 NEXT J
160 PRINT "THE MAXIMUM VALUE WAS ";C;" AT INDEX ";I;"."
170 END</syntaxhighlight>
 
==={{header|IS-BASIC}}===
<syntaxhighlight lang="is-basic">1000 DEF FINDMAX(REF ARR)
1010 LET MX=ARR(LBOUND(ARR))
1020 FOR I=LBOUND(ARR)+1 TO UBOUND(ARR)
1030 LET MX=MAX(MX,ARR(I))
1040 NEXT
1050 LET FINDMAX=MX
1060 END DEF</syntaxhighlight>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
{{trans|GW-BASIC}}
<syntaxhighlight lang="qbasic">10 INPUT "How many items"; N%
20 DIM ARR(N%)
30 FOR I% = 0 TO N% - 1
40 PRINT "Value of item #"; I%
50 INPUT ARR(I%)
60 NEXT I%
70 CHAMP = ARR(0) : INDEX = 0
80 FOR I% = 1 TO N% - 1
90 IF ARR(I%) > CHAMP THEN CHAMP = ARR(I%): INDEX = I%
100 NEXT I%
110 PRINT "The maximum value was "; CHAMP; " at index "; INDEX; "."
120 END</syntaxhighlight>
 
==={{header|Quite BASIC}}===
{{trans|GW-BASIC}}
<syntaxhighlight lang="qbasic">10 INPUT "How many items? "; n
20 ARRAY a
30 FOR j = 0 TO n-1
40 PRINT "Value of item #"; j
50 INPUT ""; t
60 LET a(j) = t
70 NEXT j
80 LET c = a(0)
90 LET i = 0
100 FOR j = 1 TO n-1
110 IF a(j) > c THEN LET c = a(j) : LET i = j
120 NEXT j
130 PRINT "The maximum value was "; c; " at index "; i; "."
140 END</syntaxhighlight>
 
=={{header|Batch File}}==
Line 1,692 ⟶ 1,757:
 
=={{header|Elena}}==
ELENA 56.0x :
<syntaxhighlight lang="elena">import extensions;
Line 1,704 ⟶ 1,769:
while (en.next())
{
var item := *en.get();
if (nil == maximal)
{
Line 1,791 ⟶ 1,856:
=={{header|Euler}}==
Euler allows hetrogenous lists, the <code>real</code> operator converts boolean and symbol (short character strings) to a number (leaving numeric values unchanged) and the <code>isu</code> operator tests whether its operand is <code>undefined</code> or not.
'''begin''' '''new''' greatest;
<syntaxhighlight lang="euler">
begin new greatest &lt;- ` '''formal''' ls;
'''begin''' '''new''' L; '''new''' i; '''new''' result; '''label''' iLoop;
greatest <- ` formal ls;
begin new L; new i; new result; label iLoop&lt;- ls;
L result <&lt;- ls'''undefined''';
result <i &lt;- undefined0;
iLoop: '''if''' [ i &lt;- i + 1 ] &lt;= '''length''' <-L 0;'''then''' '''begin'''
iLoop: if [ i <- i + 1 ] <= length '''if''' '''isu''' result '''or''' '''real''' L[ i ] &gt; then'''real''' beginresult
if isu result or real'''then''' result &lt;- L[ i ] >'''else''' real result0;
'''goto''' then result <- L[ i ] else 0;iLoop
'''end''' '''else''' goto iLoop0;
end else 0;result
result'''end'''
end&apos;;
'''out''' greatest( ( '''false''', 99.0, -271, "b", 3, 4 ) ';)
'''end''' $
out greatest( ( false, 99.0, -271, "b", 3, 4 ) )
end $
</syntaxhighlight>
 
=={{header|Euler Math Toolbox}}==
Line 2,104 ⟶ 2,167:
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Greatest_element_of_a_list}}
 
'''Solution'''
 
Fōrmulæ provides a built-in expression "Max" that reduces to the maximum element of a given list:
 
'''Case 1. With numbers'''
 
[[File:Fōrmulæ - Greatest element of a list 01.png]]
 
[[File:Fōrmulæ - Greatest element of a list 02.png]]
 
'''Case 2. With strings'''
 
[[File:Fōrmulæ - Greatest element of a list 03.png]]
 
[[File:Fōrmulæ - Greatest element of a list 04.png]]
 
'''Case 3. With time expressions'''
 
[[File:Fōrmulæ - Greatest element of a list 05.png]]
 
[[File:Fōrmulæ - Greatest element of a list 06.png]]
 
=={{header|GW-BASIC}}==
Line 2,389 ⟶ 2,474:
end</syntaxhighlight>
 
=={{headerHeader|IS-BASICInsitux}}==
 
<syntaxhighlight lang="is-basic">1000 DEF FINDMAX(REF ARR)
<code>max</code> is a built-in operation.
1010 LET MX=ARR(LBOUND(ARR))
 
1020 FOR I=LBOUND(ARR)+1 TO UBOUND(ARR)
<syntaxhighlight lang="insitux">
1030 LET MX=MAX(MX,ARR(I))
1040(max 1 2 NEXT3 4)
;or
1050 LET FINDMAX=MX
(... max [1 2 3 4])
1060 END DEF</syntaxhighlight>
</syntaxhighlight>
 
{{out}}
 
<pre>
4
</pre>
 
=={{header|J}}==
Line 4,171 ⟶ 4,263:
 
=={{header|RPL}}==
{{works with|HP|48}}
[ 2 718 28 18 28 46 ] RNRM
{ 2 718 28 18 28 46 } ≪ MAX ≫ STREAM
{{out}}
<pre>
1: 718
</pre>
 
=={{header|Ruby}}==
<tt>max</tt> is a method of all Enumerables
Line 4,660 ⟶ 4,754:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var max = Fn.new { |a| a.reduce { |m, x| (x > m) ? x : m } }
 
var a = [42, 7, -5, 11.7, 58, 22.31, 59, -18]
Line 4,775 ⟶ 4,869:
 
=={{header|Zig}}==
 
<syntaxhighlight lang="zig">fn max( a: []const f32) f32{
'''Works with:''' 0.10.x, 0.11.x, 0.12.0-dev.1389+42d4d07ef
var output = a[0];
 
for(a)|num|{
<syntaxhighlight lang="zig">/// Asserts that `input` is not empty (len >= 1).
output = @max(output, num);
pub fn max(comptime T: type, input: []const T) T {
}
var max_elem: T = input[0];
return output;
for (input[1..]) |elem| {
max_elem = @max(max_elem, elem);
}
return max_elem;
}</syntaxhighlight>
 
=={{header|zkl}}==
<syntaxhighlight lang="zkl">(1).max(1,2,3) //-->3
Anonymous user