Last list item: Difference between revisions

Content added Content deleted
(add FreeBASIC)
(Added XPL0 example.)
Line 1,066: Line 1,066:
Two smallest: 243 + 378 = 621
Two smallest: 243 + 378 = 621
Last item is 621.
Last item is 621.
</pre>

=={{header|XPL0}}==
<lang XPL0>int List, End, Sum, Item, Smallest, I, SI;
[List:= [6, 81, 243, 14, 25, 49, 123, 69, 11];
End:= 8; \last index
loop [for I:= 0 to End do \show List
[IntOut(0, List(I)); ChOut(0, ^ )];
CrLf(0);
if End = 0 then quit; \done when only one element
Sum:= 0; \find two smallest items
for Item:= 1 to 2 do
[Smallest:= -1>>1;
for I:= 0 to End do
if List(I) < Smallest then
[Smallest:= List(I); SI:= I];
Sum:= Sum + Smallest; \add them
for I:= SI to End-1 do \remove them
List(I):= List(I+1);
End:= End-1;
];
End:= End+1; \insert Sum at End
List(End):= Sum;
];
]</lang>

{{out}}
<pre>
6 81 243 14 25 49 123 69 11
81 243 14 25 49 123 69 17
81 243 25 49 123 69 31
81 243 49 123 69 56
81 243 123 69 105
243 123 105 150
243 150 228
243 378
621
</pre>
</pre>