Greatest element of a list: Difference between revisions

no edit summary
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
imported>Lacika7
No edit summary
 
(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|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,788 ⟶ 1,853:
</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 &lt;- ` '''formal''' ls;
'''begin''' '''new''' L; '''new''' i; '''new''' result; '''label''' iLoop;
L &lt;- ls;
result &lt;- '''undefined''';
i &lt;- 0;
iLoop: '''if''' [ i &lt;- i + 1 ] &lt;= '''length''' L '''then''' '''begin'''
'''if''' '''isu''' result '''or''' '''real''' L[ i ] &gt; '''real''' result
'''then''' result &lt;- L[ i ] '''else''' 0;
'''goto''' iLoop
'''end''' '''else''' 0;
result
'''end'''
&apos;;
'''out''' greatest( ( '''false''', 99.0, -271, "b", 3, 4 ) )
'''end''' $
 
=={{header|Euler Math Toolbox}}==
Line 2,082 ⟶ 2,166:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Greatest_element_of_a_list}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
Fōrmulæ provides a built-in expression "Max" that reduces to the maximum element of a given list:
In '''[https://formulae.org/?example=Greatest_element_of_a_list this]''' page you can see the program(s) related to this task and their results.
 
'''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,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 2,402 ⟶ 2,511:
 
=={{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,787:
}
]</pre>
 
=={{header|Joy}}==
<syntaxhighlight lang="joy">DEFINE maximum == unswons [max] fold.</syntaxhighlight>
 
=={{header|jq}}==
Line 2,699 ⟶ 2,820:
 
=={{header|K}}==
<syntaxhighlight lang="k"> |/ 6 1 7 4
7</syntaxhighlight>
 
Line 2,729 ⟶ 2,850:
 
=={{header|Kotlin}}==
Kotlin already has a 'max'() function in its standard library sothat weapplies useto collection of Iterable that:
<syntaxhighlight lang="scala">// version 1.0.5-2
fun main(args: Array<String>) {
print("Number of values to be input = ")
val n = readLine()!!.toInt()
val array = DoubleArray(n)
for (i in 0 until n) {
print("Value ${i + 1} = ")
array[i] = readLine()!!.toDouble()
}
println("\nThe greatest element is ${array.max()}")
}</syntaxhighlight>
Example of use:
{{out}}
<pre>
Number of values to be input = 4
Value 1 = 70.5
Value 2 = 23.67
Value 3 = 150.2
Value 4 = 145
 
<syntaxhighlight lang="kotlin">
The greatest element is 150.2
fun main() {
</pre>
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,896:
{{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,190:
 
=={{header|min}}==
{{works with|min|0.1937.30}}
<syntaxhighlight lang="min">(() ('> 'pop 'nip if) map-reduce) ^max
 
'bool ;does the list have any elements?
(5 3 2 7 4) max puts!</syntaxhighlight>
(-inf ('> 'pop 'nip if) reduce) ;do if so
({"empty seq" :error "Cannot find the maximum element of an empty sequence" :message} raise) ;do if not
if
) :seq-max</syntaxhighlight>
 
=={{header|MiniScript}}==
Line 4,116 ⟶ 4,260:
<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,659:
for strings (with configurable ignore-case):
<syntaxhighlight lang="vim">function! Max(list, ...)
" {list} list of strings
" {a:1} 'i': ignore case, 'I': match case, otherwise use 'ignorecase' option
if empty(a:list)
return 0
endif
let gt_op = a:0>=1 ? get({'i': '>?', 'I': '>#'}, a:1, '>') : '>'
Line 4,518 ⟶ 4,670:
let idx = 1
while idx < len
if eval(cmp_expr)
let maxval = a:list[idx]
endif
let idx += 1
endwhile
return maxval
Line 4,602 ⟶ 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,715 ⟶ 4,867:
> max(foo)
9</pre>
 
=={{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}}==
Anonymous user