#REDIRECT [[Sum and product of an array]]
{{task}}
Compute the sum of the elements of an Array
==[[4D]]==
[[Category:4D]]
ARRAY INTEGER($list;0)
For ($i;1;5)
APPEND TO ARRAY($list;$i)
End for
$var:=0
For ($i;1;Size of array($list))
$var:=$var+$list{$i}
End for
==[[Ada]]==
[[Category:Ada]]
Int_Array : array(1..10) of Integer := (1,2,3,4,5,6,7,8,9,10);
Sum : Integer := 0;
for I in Int_Array'range loop
Sum := Sum + Int_Array(I);
end loop;
==[[AppleScript]]==
[[Category:AppleScript]]
set array to {1, 2, 3, 4, 5}
set product to 0
repeat with i in array
-- very important -- list index starts at 1 not 0
set product to product + i
end repeat
==[[BASIC]]==
[[Category:BASIC]]
10 REM Create an array with some test data in it
20 DIM ARRAY(5)
30 FOR I = 1 TO 5: READ ARRAY(I): NEXT I
40 DATA 1, 2, 3, 4, 5
50 REM Find the sum of elements in the array
60 SUM = 0
70 FOR I = 1 TO 5: SUM = SUM + ARRAY(I): NEXT I
80 PRINT "The sum is ";SUM
==[[C plus plus|C++]]==
[[Category:C plus plus]]
'''Compiler:''' [[GNU Compiler Collection|gcc]] 4.0.2
int
main( int argc, char* argv[] )
{
int list[] = { 1, 2, 3 } ;
int sum = 0 ;
for( int i = 0 ; i < 3 ; i++ )
{
sum += list[i];
}
}
Alternate
#include <numeric>
int
main( int argc, char* argv[] )
{
int list[] = { 1, 2, 3 } ;
std::accumulate(list, list + 3, 0);
return 0;
}
template alternative
template <typename T> T sum (const T *array, const unsigned n)
{
T accum = 0;
for (unsigned i=0; i<n; i++)
accum += array[i];
return accum;
}
#include <iostream>
using std::cout;
using std::endl;
int main (void)
{
int aint[] = {1, 2, 3};
cout << sum(aint,3) << endl;
float aflo[] = {1.1, 2.02, 3.003, 4.0004};
cout << sum(aflo,4) << endl;
return 0;
}
==[[C sharp|C#]]==
[[Category:C sharp]]
int value = 0;
int[] arg = { 1,2,3,4,5 };
int arg_length = arg.Length;
for( int i = 0; i < arg_length; i++ )
value += arg[i];
Alternate
int sum = 0;
int[] arg = { 1, 2, 3, 4, 5 };
foreach (int value in arg) sum += value;
==[[Clean]]==
[[Category:Clean]]
array = {1, 2, 3, 4, 5}
Start = sum [x \\ x <-: array]
==[[ColdFusion]]==
[[Category:ColdFusion]]
<cfset myArray = listToArray("1,2,3,4,5")>
#arraySum(myArray)#
==[[Common Lisp]]==
[[Category:Common Lisp]]
(reduce #'+ #(1 2 3 4 5))
==[[Delphi]]==
[[Category:Delphi]
'''Compiler:''' [[All]]
var
Ints : array[1..5] of integer = (1,2,3,4,5) ;
i,Sum : integer = 0 ;
begin
for i := 1 to length(ints) do inc(sum,ints[i]) ;
end;
==[[E]]==
[[Category:E]]
pragma.enable("accumulator")
accum 0 for x in [1,2,3,4,5] { _ + x }
==[[Erlang]]==
[[Category:Erlang]]
Using the standard libraries:
% create the list:
L = lists:seq(1, 10).
% and compute its sum:
S = lists:sum(L).
Or defining our own versions:
-module(list_sum).
-export([sum_rec/1, sum_tail/1]).
% recursive definition:
sum_rec([]) ->
0;
sum_rec([Head|Tail]) ->
Head + sum_rec(Tail).
% tail-recursive definition:
sum_tail(L) ->
sum_tail(L, 0).
sum_tail([], Acc) ->
Acc;
sum_tail([Head|Tail], Acc) ->
sum_tail(Tail, Head + Acc).
==[[Forth]]==
[[Category:Forth]]
: sum ( addr cnt -- n )
0 -rot
cells bounds do i @ + cell +loop ;
==[[FreeBASIC]]==
[[Category:FreeBASIC]]
dim array(4) as integer = { 1, 2, 3, 4, 5 }
dim sum as integer = 0
for index as integer = lbound(array) to ubound(array)
sum += array(index)
next
==[[Haskell]]==
[[Category:Haskell]]
values = [1..10] :: [Int]
answer = sum values -- the easy way
answer' = foldl (+) 0 values -- the hard way
==[[IDL]]==
[[Category:IDL]]
result = total(array)
==[[Java]]==
[[Category:Java]]
int value = 0;
int[] arg = new int[] { 1,2,3,4,5 };
for (int i: arg)
value += i;
==[[JavaScript]]==
[[Category:JavaScript]]
var array = [1, 2, 3, 4, 5];
var sum = 0;
for(var i in array)
sum += array[i];
== [[OCaml]] ==
[[Category:OCaml]]
let a = [| 1; 2; 3; 4; 5 |] in
Array.fold_left (+) 0 a
==[[Perl]]==
[[Category:Perl]]
'''Interpeter:''' [[Perl]]
my $var;
my @list = (1, 2, 3);
$var += $_ for (@list);
Alternate
'''Libraries:''' List::Util
use List::Util 'sum';
my @list = (1, 2, 3);
my $var = sum @list;
Alternate
'''# TMTOWTDI'''
my $acc = 0;
my @list = qw(1 2 3)
map { $acc += $_ } @list
==[[PHP]]==
[[Category:PHP]]
$list = array(1,2,3,4,5,6,7,8,9);
echo array_sum($list);
==[[Pop11]]==
[[Category:Pop11]]
Simple loop:
lvars i, sum = 0, ar = {1 2 3 4 5 6 7 8 9};
for i from 1 to length(ar) do
ar(i) + sum -> sum;
endfor;
One can alternativly use second order iterator:
lvars sum = 0, ar = {1 2 3 4 5 6 7 8 9};
appdata(ar, procedure(x); x + sum -> sum; endprocedure);
==[[Prolog]]==
[[Category:Prolog]]
sum([],0).
sum([H|T],X) :- sum(T,Y), X is H + Y.
test
:- sum([1,2,3,4,5,6,7,8,9],X).
X =45;
==[[Python]]==
[[Category:Python]]
'''Interpeter:''' [[Python]] 2.5
total = sum([1, 2, 3, 4, 5, 6, 7, 8, 9])
==[[Ruby]]==
[[Category:Ruby]]
arr = [1,2,3,4,5]
sum = arr.inject { |sum, item| sum + item }
== [[Scala]]==
[[Category:Scala]]
val array = Array(1,2,3,4,5)
val sum = array.foldLeft(0)(_ + _)
This is a shortcut for
val sum = array.foldLeft(0){(currentSum, element) => currentSum + element}
==[[Seed7]]==
[[Category:Seed7]]
const func integer: sumArray (in array integer: valueArray) is func
result
var integer: sum is 0;
local
var integer: value is 0;
begin
for value range valueArray do
sum +:= value;
end for;
end func;
Call this function with:
writeln(sumArray([](1, 2, 3, 4, 5)));
==[[Smalltalk]]==
[[Category:Smalltalk]]
#(1 2 3 4 5) inject: 0 into: [:sum :number | sum + number]
==[[Standard ML]]==
[[Category:Standard ML]]
val array = [1,2,3,4,5];
foldl op+ 0 array;
==[[Tcl]]==
[[Category:Tcl]]
Assuming the values are in a list named <tt>listname</tt>:
set result [expr [join $listname +]]
==[[Toka]]==
[[Category:Toka]]
[ ( array size -- sum )
>r 0 r> 0 [ over i swap array.get + ] countedLoop nip ] is sum-array
==[[UNIX Shell]]==
[[Category:UNIX Shell]]
'''Interpreter:''' NetBSD 3.0's ash
From an internal variable, $IFS delimited:
sum=0
list="1 2 3"
for n in $list
do sum="$(($sum + $n))"
done
echo $sum
From the argument list (ARGV):
sum=0
for n
do sum="$(($sum + $n))"
done
echo $sum
From STDIN, one integer per line:
sum=0
while read n
do sum="$(($sum + $n))"
done
echo $sum
'''Interpreter:''' GNU bash, version 3.2.0(1)-release (i386-unknown-freebsd6.1)
From variable:
LIST='20 20 2';
SUM=0;
for i in $LIST; do
SUM=$[$SUM + $i];
done;
echo $SUM
|