Last list item: Difference between revisions

→‎{{header|ALGOL 68}}: Revised to meet the task requirements re the positioning of the new element
(→‎{{header|ALGOL 68}}: Revised to meet the task requirements re the positioning of the new element)
Line 10:
 
=={{header|ALGOL 68}}==
{{improve|Algol 68|Task specifically requires that the interim sum be appended to the '''end''' of the list.}}
Was a translation of the sorted Wren version but the sorting has been removed as per the revised task requirements.
<lang algol68>BEGIN # find the last element after repeatedlyrepeatedely adding the sum #
# of the two smallest elements and removing them #
[ 1 : 9 ]INT a := ( 6, 81, 243, 14, 25, 49, 123, 69, 11 );
Line 35 ⟶ 34:
)
);
a[INT s1posm ]pos := sum0;
FOR i FROM s2pos TO a count - 1 DO a[ i ] := a[ i + 1 ] OD;
IF i /= s1pos AND i /= s2pos THEN a[ m pos +:= 1 ] := a[ i ] FI
OD;
a[ m pos + 1 ] := sum;
a count -:= 1
OD;
print( ( "Last item is ", whole( a[ 1 ], 0 ), ".", newline ) )
END</lang>
</lang>
{{out}}
<pre>
List: 6 81 243 14 25 49 123 69 11; two smallest: 6 @ 1 and 11 @ 9; sum = 17
List: 17 81 243 14 25 49 123 69 17; two smallest: 14 @ 43 and 17 @ 18; sum = 31
List: 81 243 31 25 49 123 69 31; two smallest: 25 @ 43 and 31 @ 37; sum = 56
List: 81 243 56 49 123 69 56; two smallest: 49 @ 43 and 56 @ 36; sum = 105
List: 81 243 105 123 69 105; two smallest: 69 @ 54 and 81 @ 1; sum = 150
List: 243 105 123 105 150; two smallest: 105 @ 23 and 123 @ 32; sum = 228
List: 243 228 150 228; two smallest: 150 @ 32 and 228 @ 23; sum = 378
List: 243 378; two smallest: 243 @ 1 and 378 @ 2; sum = 621
Last item is 621.
3,021

edits