Sum of Array: Difference between revisions
Content deleted Content added
m →[[Java]]: Use Java header instead |
m Switch to header template |
||
Line 2:
Compute the sum of the elements of an Array
==
ARRAY INTEGER($list;0)
For ($i;1;5)
Line 14 ⟶ 12:
End for
==
Int_Array : array(1..10) of Integer := (1,2,3,4,5,6,7,8,9,10);
Sum : Integer := 0;
Line 23 ⟶ 19:
end loop;
==
set array to {1, 2, 3, 4, 5}
set product to 0
Line 32 ⟶ 26:
end repeat
==
10 REM Create an array with some test data in it
20 DIM ARRAY(5)
Line 94 ⟶ 86:
}
==
int value = 0;
int[] arg = { 1,2,3,4,5 };
Line 111 ⟶ 101:
foreach (int value in arg) sum += value;
==
array = {1, 2, 3, 4, 5}
Start = sum [x \\ x <-: array]
==
<cfset myArray = listToArray("1,2,3,4,5")>
#arraySum(myArray)#
==
(reduce #'+ #(1 2 3 4 5))
==
'''Compiler:''' [[All]]
Line 139 ⟶ 122:
end;
==
pragma.enable("accumulator")
accum 0 for x in [1,2,3,4,5] { _ + x }
==
Using the standard libraries:
Line 175 ⟶ 154:
sum_tail(Tail, Head + Acc).
==
: sum ( addr cnt -- n )
0 -rot
Line 183 ⟶ 160:
==
dim array(4) as integer = { 1, 2, 3, 4, 5 }
Line 193 ⟶ 168:
next
==
[1,2,3,4,5].sum()
Line 227 ⟶ 200:
value += i;
==
var array = [1, 2, 3, 4, 5];
var sum = 0;
Line 235 ⟶ 206:
sum += array[i];
==
arr = #(1, 2, 3, 4, 5)
result = 0
for i in arr do result += i
==
let a = [| 1; 2; 3; 4; 5 |] in
Array.fold_left (+) 0 a
==
my $var;
my @list = (1, 2, 3);
Line 271 ⟶ 235:
map { $acc += $_ } @list
==
$list = array(1,2,3,4,5,6,7,8,9);
echo array_sum($list);
==
Simple loop:
Line 292 ⟶ 252:
appdata(ar, procedure(x); x + sum -> sum; endprocedure);
==
sum([],0).
sum([H|T],X) :- sum(T,Y), X is H + Y.
Line 300 ⟶ 259:
X =45;
==
'''Interpeter:''' [[Python]] 2.5
total = sum([1, 2, 3, 4, 5, 6, 7, 8, 9])
==
0 [ 1 2 3 ] each +
==
arr = [1,2,3,4,5]
sum = arr.inject { |sum, item| sum + item }
==
val array = Array(1,2,3,4,5)
val sum = array.foldLeft(0)(_ + _)
Line 325 ⟶ 277:
val sum = array.foldLeft(0){(currentSum, element) => currentSum + element}
==
const func integer: sumArray (in array integer: valueArray) is func
result
Line 343 ⟶ 293:
writeln(sumArray([](1, 2, 3, 4, 5)));
==
#(1 2 3 4 5) inject: 0 into: [:sum :number | sum + number]
==
val array = [1,2,3,4,5];
foldl op+ 0 array;
==
Assuming the values are in a list named <tt>listname</tt>:
set result [expr [join $listname +]]
==
[ ( array size -- sum )
>r 0 r> 0 [ over i swap array.get + ] countedLoop nip ] is sum-array
==
'''Interpreter:''' NetBSD 3.0's ash
|