Greatest element of a list: Difference between revisions
Add SmallBASIC
(added Zig) |
(Add SmallBASIC) |
||
(21 intermediate revisions by 15 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|SmallBASIC}}===
<syntaxhighlight lang="qbasic">
list = [1,1234,62,234,12,34,6]
print max(list)
</syntaxhighlight>
=={{header|Batch File}}==
Line 1,692 ⟶ 1,763:
=={{header|Elena}}==
ELENA
<syntaxhighlight lang="elena">import extensions;
Line 1,704 ⟶ 1,775:
while (en.next())
{
var item := *en
if (nil == maximal)
{
Line 1,788 ⟶ 1,859:
</syntaxhighlight>
Note: The limit of this program is string variable lenght (255 chars). The advantage is no array use.
=={{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;
greatest <- ` '''formal''' ls;
'''begin''' '''new''' L; '''new''' i; '''new''' result; '''label''' iLoop;
L <- ls;
result <- '''undefined''';
i <- 0;
iLoop: '''if''' [ i <- i + 1 ] <= '''length''' L '''then''' '''begin'''
'''if''' '''isu''' result '''or''' '''real''' L[ i ] > '''real''' result
'''then''' result <- L[ i ] '''else''' 0;
'''goto''' iLoop
'''end''' '''else''' 0;
result
'''end'''
';
'''out''' greatest( ( '''false''', 99.0, -271, "b", 3, 4 ) )
'''end''' $
=={{header|Euler Math Toolbox}}==
Line 2,082 ⟶ 2,172:
=={{header|Fōrmulæ}}==
{{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,372 ⟶ 2,480:
end</syntaxhighlight>
=={{
<code>max</code> is a built-in operation.
<syntaxhighlight lang="insitux">
;or
(... max [1 2 3 4])
</syntaxhighlight>
{{out}}
<pre>
4
</pre>
=={{header|J}}==
Line 2,402 ⟶ 2,517:
=={{header|Java}}==
<syntaxhighlight lang="java">
int max(int[] values) {
int max = values[0];
for (int value : values)
if (max < value) max = value;
return max;
}
</syntaxhighlight>
<br />
The first function works with arrays of floats. Replace with arrays of double, int, or other primitive data type.
<syntaxhighlight lang="java">public static float max(float[] values) throws NoSuchElementException {
Line 2,669 ⟶ 2,793:
}
]</pre>
=={{header|Joy}}==
<syntaxhighlight lang="joy">DEFINE maximum == unswons [max] fold.</syntaxhighlight>
=={{header|jq}}==
Line 2,699 ⟶ 2,826:
=={{header|K}}==
<syntaxhighlight lang="k"> |/ 6 1 7 4
7</syntaxhighlight>
Line 2,729 ⟶ 2,856:
=={{header|Kotlin}}==
Kotlin
<syntaxhighlight lang="kotlin">
fun main() {
listOf(1.0, 3.5, -1.1).max().also { println(it) } // 3.5
listOf(1, 3, -1).max().also { println(it) } // 3
setOf(1, 3, -1).max().also { println(it) } // 3
}
</syntaxhighlight>
=={{header|Lambdatalk}}==
Line 2,789 ⟶ 2,902:
{{out}}
<pre>7344</pre>
=={{header|LDPL}}==
<syntaxhighlight lang="ldpl">data:
values is number list
n is number
procedure:
sub max
parameters:
x is number list
result is number
local data:
i is number
procedure:
store x:0 in result
for each i in values do
if i is greater than result then
store i in result
end if
repeat
end sub
create statement "get maximum of list $ in $" executing max
# Now let's use the sub-procedure.
push 1 to values
push 11 to values
push 5 to values
push 33 to values
push 99 to values
push 13 to values
push 37 to values
push 50 to values
get maximum of list values in n
display "The maximum is " n lf
</syntaxhighlight>
{{out}}
<pre>
The maximum is 99
</pre>
=={{header|LFE}}==
Line 3,043 ⟶ 3,196:
=={{header|min}}==
{{works with|min|0.
<syntaxhighlight lang="min">(() ('> 'pop 'nip if) map-reduce) ^max
(5 3 2 7 4) max puts!</syntaxhighlight>
=={{header|MiniScript}}==
Line 4,116 ⟶ 4,266:
<pre>
10
</pre>
=={{header|RPL}}==
{{works with|HP|48}}
{ 2 718 28 18 28 46 } ≪ MAX ≫ STREAM
{{out}}
<pre>
1: 718
</pre>
Line 4,507 ⟶ 4,665:
for strings (with configurable ignore-case):
<syntaxhighlight lang="vim">function! Max(list, ...)
" {list}
" {a:1}
if empty(a:list)
endif
let gt_op = a:0>=1 ? get({'i': '>?', 'I': '>#'}, a:1, '>') : '>'
Line 4,518 ⟶ 4,676:
let idx = 1
while idx < len
endwhile
return maxval
Line 4,602 ⟶ 4,760:
=={{header|Wren}}==
<syntaxhighlight lang="
var a = [42, 7, -5, 11.7, 58, 22.31, 59, -18]
Line 4,717 ⟶ 4,875:
=={{header|Zig}}==
'''Works with:''' 0.10.x, 0.11.x, 0.12.0-dev.1389+42d4d07ef
<syntaxhighlight lang="zig">/// Asserts that `input` is not empty (len >= 1).
pub fn max(comptime T: type, input: []const T) T {
var max_elem: T = input[0];
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
|