Greatest subsequential sum: Difference between revisions

m
Moved Wren entry into correct alphabetical order.
m (→‎{{header|Wren}}: Changed to Wren S/H)
m (Moved Wren entry into correct alphabetical order.)
Line 4,018:
<pre>
<3,5,6,-2,-1,4></pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">include c:\cxpl\codes;
int Array, Size, Sum, Best, I, Lo, Hi, BLo, BHi;
 
[Array:= [-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1];
Size:= 11;
Best:= -100000;
for Lo:= 0 to Size-1 do
for Hi:= Lo to Size-1 do
[Sum:= 0;
for I:= Lo to Hi do
Sum:= Sum + Array(I);
if Sum > Best then
[Best:= Sum; BLo:= Lo; BHi:= Hi];
];
Text(0, "Sequence = ");
for I:= 0 to Size-1 do
[IntOut(0, Array(I)); Text(0, " ")];
CrLf(0);
Text(0, "Greatest = ");
for I:= BLo to BHi do
[IntOut(0, Array(I)); Text(0, " ")];
CrLf(0);
Text(0, "Sum = "); IntOut(0, Best); CrLf(0);
]</syntaxhighlight>
 
{{out}}
<pre>
Sequence = -1 -2 3 5 6 -2 -1 4 -4 2 -1
Greatest = 3 5 6 -2 -1 4
Sum = 15
</pre>
 
=={{header|Wren}}==
Line 4,108 ⟶ 4,075:
Sub seq: []
Sum: 0
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">include c:\cxpl\codes;
int Array, Size, Sum, Best, I, Lo, Hi, BLo, BHi;
 
[Array:= [-1, -2, 3, 5, 6, -2, -1, 4, -4, 2, -1];
Size:= 11;
Best:= -100000;
for Lo:= 0 to Size-1 do
for Hi:= Lo to Size-1 do
[Sum:= 0;
for I:= Lo to Hi do
Sum:= Sum + Array(I);
if Sum > Best then
[Best:= Sum; BLo:= Lo; BHi:= Hi];
];
Text(0, "Sequence = ");
for I:= 0 to Size-1 do
[IntOut(0, Array(I)); Text(0, " ")];
CrLf(0);
Text(0, "Greatest = ");
for I:= BLo to BHi do
[IntOut(0, Array(I)); Text(0, " ")];
CrLf(0);
Text(0, "Sum = "); IntOut(0, Best); CrLf(0);
]</syntaxhighlight>
 
{{out}}
<pre>
Sequence = -1 -2 3 5 6 -2 -1 4 -4 2 -1
Greatest = 3 5 6 -2 -1 4
Sum = 15
</pre>
 
9,488

edits