Sum of Array: Difference between revisions

From Rosetta Code
Content added Content deleted
mNo edit summary
(revert spam)
Line 1: Line 1:
{{task}}
[http://amohseni.info/img/styles/www-life/ www life electronics com] [http://saibso.org/tiziano-crudeli.htm tiziano crudeli] [http://chaba.info/un-sorriso/ un sorriso, uno schiaffo, un bacio in bocca] [http://chaba.info/cabina-armadio/ cabina armadio foppapedretti] [http://kinunia.cn/ford-fiesta.htm ford fiesta newport 1.3] [http://davte.info/library/html/meteo-biella.htm meteo biella] [http://bloprofeldi.info/lib/mainz.htm mainz] [http://amohseni.info/img/styles/duncan-james/ duncan james naked] [http://nobinters.org/zhoukoudian.htm zhoukoudian] [http://chaba.info/e-gps/ e2 gps] [http://helmed.info/topic/dizionario-religioni/ dizionario religioni] [http://ustall.org/web/rasaerba-elettrico.htm rasaerba elettrico] [http://psisemiya.com/images/small/fodero-lg/ fodero lg] [http://helmed.info/topic/heaven-is/ heaven is a halfpipe] [http://nobinters.org/asus-av.htm asus a8v e deluxe via k8t890] [http://psisemiya.com/images/small/inni-e/ inni e canti fascisti] [http://helmed.info/topic/bambola-club/ bambola club pornostar] [http://ashythro.info/library/html/enima/ enima] [http://chaba.info/vite-rubate/ vite rubate] [http://bloprofeldi.info/lib/modem-router.htm modem router adsl d-link] [http://ustall.org/web/gjellerup-karl.htm gjellerup, karl adolf] [http://kinunia.cn/leggero-cellulari.htm leggero cellulari] [http://psisemiya.com/images/small/houseboat/ house-boat] [http://bloprofeldi.info/lib/fascia-cardio.htm fascia cardio] [http://nobinters.org/basciano-teramo.htm basciano teramo] [http://nobinters.org/adattatore-svideo.htm adattatore s-video a scart] [http://psisemiya.com/images/small/lacie-hard/ lacie hard drive 250 gb] [http://amohseni.info/img/styles/picture-woman/ picture woman] [http://amohseni.info/img/styles/fotocamera-ed/ fotocamera ed mp3] [http://ustall.org/web/canon-mm.htm canon 50mm 1 8] [http://saibso.org/sexual.htm sexual] [http://bloprofeldi.info/lib/eurodance.htm eurodance 13] [http://ustall.org/web/cuba-libre.htm cuba libre - velocipedi ai tropici] [http://saibso.org/kinetographie.htm kinetographie] [http://bloprofeldi.info/lib/pleasure-from.htm pleasure from the bass dj tiga] [http://psisemiya.com/images/small/toyota-pd/ toyota pd] [http://davte.info/library/html/graduatorie-ata.htm graduatorie ata messina 3 fascia 2002] [http://psisemiya.com/images/small/odontites/ odontites] [http://ustall.org/web/i-vitelloni.htm i vitelloni] [http://saibso.org/sito-di.htm sito di al qaeda] [http://nobinters.org/junco.htm junco] [http://bloprofeldi.info/lib/la-madunina.htm la madunina] [http://davte.info/library/html/esame-per.htm esame per la patente] [http://bloprofeldi.info/lib/vgn-shp.htm vgn s4hp b] [http://amohseni.info/img/styles/angel-guard/ angel guard] [http://chaba.info/fimp-it/ fimp it] [http://kinunia.cn/amd-sempron.htm amd sempron 3300] [http://ashythro.info/library/html/fighe-nude/ fighe nude americane] [http://helmed.info/topic/uomo-primitivi/ uomo primitivi] [http://chaba.info/tesina-appunto/ tesina appunto] [http://nobinters.org/di-simone.htm di simone] [http://saibso.org/la-magnifica.htm la magnifica preda] [http://helmed.info/topic/mouse-trust/ mouse trust wireless] [http://amohseni.info/img/styles/fogo-na/ fogo na saia] [http://nobinters.org/left-outside.htm left outside alone su anastacia] [http://psisemiya.com/images/small/voli-riga/ voli riga] [http://chaba.info/oregon-scientific/ oregon scientific quicklink] [http://ustall.org/web/monitor-lcd.htm monitor lcd dvi 17] [http://helmed.info/topic/hotel-principe/ hotel principe di savoia] [http://nobinters.org/alano-iv.htm alano iv] [http://amohseni.info/img/styles/www-ambasciata/ www ambasciata greca it] [http://nobinters.org/ragazzo-della.htm ragazzo della notte] [http://davte.info/library/html/concessionari-kawasaki.htm concessionari kawasaki] [http://helmed.info/topic/jeans-nero/ jeans nero vita bassa] [http://chaba.info/immagini-dell/ immagini dell isola di albarella] [http://ustall.org/web/forma-parole.htm forma parole] [http://saibso.org/palmare-ipaq.htm palmare ipaq] [http://kinunia.cn/zen-portable.htm zen portable] [http://amohseni.info/img/styles/driver-free/ driver free motorola c550 download] [http://helmed.info/topic/www-dolphin/ www dolphin club it] [http://davte.info/library/html/www-rdi.htm www rdi com] [http://amohseni.info/img/styles/dv-multimedia/ dv multimedia card] [http://ashythro.info/library/html/mannoia-concerti/ mannoia concerti] [http://psisemiya.com/images/small/i-rolling/ i rolling stones secondo godard] [http://chaba.info/vendita-moto/ vendita moto usate] [http://amohseni.info/img/styles/una-nuova/ una nuova playstation 2 per il giappone] [http://kinunia.cn/computer-windows.htm computer windows xp media center] [http://chaba.info/simboli-dwg/ simboli dwg] [http://psisemiya.com/images/small/vaio-vgnfsb/ vaio vgn-fs215b] [http://kinunia.cn/configurazione-mms.htm configurazione mms philips 350] [http://bloprofeldi.info/lib/mp.htm mp2] [http://chaba.info/stampante-epson/ stampante epson stilus c64 stilus] [http://chaba.info/attrice-italiana/ attrice italiana varia] [http://chaba.info/correggere-un/ correggere un annuncio] [http://ustall.org/web/sessso-gratis.htm sessso gratis] [http://bloprofeldi.info/lib/rtype-dx.htm r-type dx] [http://ustall.org/web/sony-mavica.htm sony mavica mvc fd200] [http://amohseni.info/img/styles/politecnico-milano/ politecnico milano] [http://nobinters.org/hony-ball.htm hony ball] [http://saibso.org/toshiba.htm toshiba 740] [http://helmed.info/topic/dieci-cento/ dieci cento mille dei brothers] [http://nobinters.org/trolley-tucano.htm trolley tucano] [http://kinunia.cn/i-belong.htm i belong to yuo] [http://amohseni.info/img/styles/istruzione-ricarica/ istruzione ricarica inchiostro cartuccia] [http://saibso.org/stampante-laser.htm stampante laser e fax] [http://davte.info/library/html/severina-video.htm severina video] [http://bloprofeldi.info/lib/vacanze-nel.htm vacanze nel mondo] [http://psisemiya.com/images/small/bayliner/ bayliner 2855] [http://helmed.info/topic/www-mininter/ www mininter gob pe] [http://kinunia.cn/p-diddy.htm p diddy ill be missing you] [http://saibso.org/biglietto-aereo.htm biglietto aereo lamezia-roma] [http://nobinters.org/walzel-oskar.htm walzel, oskar] [http://saibso.org/wrestiling-femminile.htm wrestiling femminile] [http://nobinters.org/punto-zero.htm punto zero] [http://nobinters.org/luca-napoli.htm luca napoli] [http://chaba.info/valva/ valva] [http://chaba.info/la-legione/ la legione invincibile libri] [http://amohseni.info/img/styles/mp-samsung/ mp3 samsung 128] [http://kinunia.cn/foto-di.htm foto di loredana lecciso] [http://bloprofeldi.info/lib/torakiki.htm torakiki] [http://saibso.org/lupen-lll.htm lupen lll] [http://ustall.org/web/gazzettino-padova.htm gazzettino padova] [http://kinunia.cn/tema-desktop.htm tema desktop windows xp] [http://amohseni.info/img/styles/detroit-new/ detroit new york] [http://kinunia.cn/luigi-boccherini.htm luigi boccherini] [http://kinunia.cn/viridiana.htm viridiana] [http://nobinters.org/brother-inkjet.htm brother inkjet multifunzione con fax] [http://helmed.info/topic/internet-e/ internet e reti di calcolatori] [http://ustall.org/web/john-sena.htm john sena] [http://psisemiya.com/images/small/hotel-aosta/ hotel aosta trapani] [http://bloprofeldi.info/lib/karin-schubert.htm karin schubert and paola senatore] [http://davte.info/library/html/hd-ultra.htm hd ultra ata] [http://helmed.info/topic/guta-so/ guta so daniela] [http://ustall.org/web/fifi-and.htm fifi and romeo] [http://nobinters.org/riassunti-i.htm riassunti i promessi sposi] [http://kinunia.cn/chaplin-charlie.htm chaplin charlie] [http://davte.info/library/html/carmine-gallone.htm carmine gallone] [http://chaba.info/corso-computer/ corso computer] [http://chaba.info/vancouver/ vancouver] [http://ustall.org/web/hyphessobrycon.htm hyphessobrycon] [http://psisemiya.com/images/small/sodomia/ sodomia] [http://amohseni.info/img/styles/linsey-dawn/ linsey dawn mckenzie] [http://nobinters.org/ritch-bitch.htm ritch bitch] [http://chaba.info/tenda-casetta/ tenda casetta campeggio] [http://psisemiya.com/images/small/thierry-mugler/ thierry mugler cologne] [http://helmed.info/topic/graduatorie-insegnanti/ graduatorie insegnanti di religione] [http://ustall.org/web/km-suzuki.htm km0 suzuki gpl auto km 0] [http://kinunia.cn/decodificatore-dvd.htm decodificatore dvd gratis] [http://helmed.info/topic/incontro-novi/ incontro novi ligure] [http://chaba.info/carpi-moto/ carpi moto] [http://davte.info/library/html/modem-interno.htm modem interno analogico] [http://ustall.org/web/valletta-roma.htm valletta roma] [http://saibso.org/grand-cherokee.htm grand cherokee 4.0] [http://amohseni.info/img/styles/gagliano-del/ gagliano del capo] [http://ustall.org/web/come-stai.htm come stai di vasco rossi] [http://amohseni.info/img/styles/hong-nhunh/ hong nhunh] [http://helmed.info/topic/veronese-flli/ veronese flli srl] [http://helmed.info/topic/lindy-dvi/ lindy dvi 2 mt] [http://saibso.org/stampante-hp.htm stampante hp 8750] [http://amohseni.info/img/styles/www-byblos/ www byblos it] [http://davte.info/library/html/dscp.htm dsc-p12] [http://ashythro.info/library/html/sesso-foto/ sesso foto disabili] [http://chaba.info/umts-cellulari/ umts cellulari samsung] [http://davte.info/library/html/dr-alte.htm dr alte holz] [http://bloprofeldi.info/lib/background-colours.htm background colours] [http://kinunia.cn/conaju.htm conaju] [http://kinunia.cn/wem-j.htm wem j] [http://helmed.info/topic/yu-gi/ yu gi ho gioco da scaricare] [http://saibso.org/riobamba.htm riobamba] [http://helmed.info/topic/la-maledizione/ la maledizione della prima luna il disco] [http://ustall.org/web/elettrostimolatore-babyliss.htm elettrostimolatore babyliss] [http://bloprofeldi.info/lib/antifumo.htm antifumo] [http://kinunia.cn/trio-chicco.htm trio chicco autofix] [http://nobinters.org/then-servant.htm then servant] [http://bloprofeldi.info/lib/vino-giordano.htm vino giordano] [http://helmed.info/topic/thesis-diesel/ thesis diesel] [http://amohseni.info/img/styles/sito-telefonia/ sito telefonia] [http://psisemiya.com/images/small/thoitrang/ thoitrang] [http://kinunia.cn/macromedia-flash.htm macromedia flash] [http://ashythro.info/library/html/www-amiloidosi/ www amiloidosi it] [http://ustall.org/web/marcello-fondato.htm marcello fondato] [http://davte.info/library/html/il-killer.htm il killer - tactical assassin] [http://helmed.info/topic/kawasaki-zx/ kawasaki zx 6r] [http://psisemiya.com/images/small/bmw-compact/ bmw compact] [http://psisemiya.com/images/small/ferrar/ ferrar] [http://psisemiya.com/images/small/paola-perego/ paola perego foto] [http://bloprofeldi.info/lib/remington-hc.htm remington hc363] [http://nobinters.org/sottoveste-che.htm sottoveste che passione] [http://psisemiya.com/images/small/la-ginestra/ la ginestra] [http://helmed.info/topic/terorema/ terorema] [http://helmed.info/topic/lavatrici-/ lavatrici 33 cm] [http://ustall.org/web/adsl-firewall.htm adsl2 firewall router] [http://davte.info/library/html/online-personal.htm online personal ads] [http://bloprofeldi.info/lib/la-grande.htm la grande fuga] [http://psisemiya.com/images/small/dutilleux/ dutilleux] [http://chaba.info/venezia-parigi/ venezia parigi] [http://nobinters.org/midi-gigi.htm midi gigi finizio] [http://nobinters.org/nuova-mercedes.htm nuova mercedes 320 s diesel auto nuove] [http://amohseni.info/img/styles/gestion-privee/ gestion privee] [http://chaba.info/clearvue-worksheet/ clearvue worksheet] [http://saibso.org/focus-c.htm focus c max 1.8] [http://nobinters.org/u-s.htm u s beheading] [http://bloprofeldi.info/lib/hub-base.htm hub 100base fx] [http://chaba.info/agri/ agri 3] [http://kinunia.cn/hd-seagate.htm hd seagate 160gb] [http://ustall.org/web/parnassia.htm parnassia] [http://ustall.org/web/www-ibox.htm www ibox] [http://nobinters.org/shemale-hentai.htm shemale hentai] [http://helmed.info/topic/che-fico/ che fico] [http://saibso.org/anna-dei.htm anna dei mille giorni] {{task}}
Compute the sum of the elements of an Array
Compute the sum of the elements of an Array


Line 11: Line 11:
$var:=0
$var:=0
For ($i;1;Size of array($list))
For ($i;1;Size of array($list))
$var:=$var $list{$i}
$var:=$var+$list{$i}
End for
End for


Line 20: Line 20:
Sum : Integer := 0;
Sum : Integer := 0;
for I in Int_Array'range loop
for I in Int_Array'range loop
Sum := Sum Int_Array(I);
Sum := Sum + Int_Array(I);
end loop;
end loop;


Line 30: Line 30:
repeat with i in array
repeat with i in array
-- very important -- list index starts at 1 not 0
-- very important -- list index starts at 1 not 0
set product to product i
set product to product + i
end repeat
end repeat


Line 42: Line 42:
50 REM Find the sum of elements in the array
50 REM Find the sum of elements in the array
60 SUM = 0
60 SUM = 0
70 FOR I = 1 TO 5: SUM = SUM ARRAY(I): NEXT I
70 FOR I = 1 TO 5: SUM = SUM + ARRAY(I): NEXT I
80 PRINT "The sum is ";SUM
80 PRINT "The sum is ";SUM


==[[C plus plus|C ]]==
==[[C plus plus|C++]]==
[[Category:C plus plus]]
[[Category:C plus plus]]


Line 55: Line 55:
int list[] = { 1, 2, 3 } ;
int list[] = { 1, 2, 3 } ;
int sum = 0 ;
int sum = 0 ;
for( int i = 0 ; i < 3 ; i )
for( int i = 0 ; i < 3 ; i++ )
{
{
sum = list[i];
sum += list[i];
}
}
}
}
Line 69: Line 69:
{
{
int list[] = { 1, 2, 3 } ;
int list[] = { 1, 2, 3 } ;
std::accumulate(list, list 3, 0);
std::accumulate(list, list + 3, 0);
return 0;
return 0;
}
}
Line 78: Line 78:
{
{
T accum = 0;
T accum = 0;
for (unsigned i=0; i<n; i )
for (unsigned i=0; i<n; i++)
accum = array[i];
accum += array[i];
return accum;
return accum;
}
}
Line 103: Line 103:
int arg_length = arg.Length;
int arg_length = arg.Length;


for( int i = 0; i < arg_length; i )
for( int i = 0; i < arg_length; i++ )
value = arg[i];
value += arg[i];




Line 111: Line 111:
int sum = 0;
int sum = 0;
int[] arg = { 1, 2, 3, 4, 5 };
int[] arg = { 1, 2, 3, 4, 5 };
foreach (int value in arg) sum = value;
foreach (int value in arg) sum += value;


==[[Clean]]==
==[[Clean]]==
Line 128: Line 128:


(defparameter *data* #(1 2 3 4 5))
(defparameter *data* #(1 2 3 4 5))
(reduce #' *data*)
(reduce #'+ *data*)


==[[Delphi]]==
==[[Delphi]]==
Line 146: Line 146:


pragma.enable("accumulator")
pragma.enable("accumulator")
accum 0 for x in [1,2,3,4,5] { _ x }
accum 0 for x in [1,2,3,4,5] { _ + x }


==[[Erlang]]==
==[[Erlang]]==
Line 168: Line 168:
0;
0;
sum_rec([Head|Tail]) ->
sum_rec([Head|Tail]) ->
Head sum_rec(Tail).
Head + sum_rec(Tail).


% tail-recursive definition:
% tail-recursive definition:
Line 176: Line 176:
Acc;
Acc;
sum_tail([Head|Tail], Acc) ->
sum_tail([Head|Tail], Acc) ->
sum_tail(Tail, Head Acc).
sum_tail(Tail, Head + Acc).


==[[Forth]]==
==[[Forth]]==
Line 183: Line 183:
: sum ( addr cnt -- n )
: sum ( addr cnt -- n )
0 -rot
0 -rot
cells bounds do i @ cell loop ;
cells bounds do i @ + cell +loop ;




Line 193: Line 193:
dim sum as integer = 0
dim sum as integer = 0
for index as integer = lbound(array) to ubound(array)
for index as integer = lbound(array) to ubound(array)
sum = array(index)
sum += array(index)
next
next


Line 201: Line 201:
let values = [1..10]
let values = [1..10]
sum values -- the easy way
sum values -- the easy way
foldl ( ) 0 values -- the hard way
foldl (+) 0 values -- the hard way


==[[IDL]]==
==[[IDL]]==
Line 214: Line 214:
int[] arg = new int[] { 1,2,3,4,5 };
int[] arg = new int[] { 1,2,3,4,5 };
for (int i: arg)
for (int i: arg)
value = i;
value += i;


==[[JavaScript]]==
==[[JavaScript]]==
Line 222: Line 222:
var sum = 0;
var sum = 0;
for(var i in array)
for(var i in array)
sum = array[i];
sum += array[i];


==[[Perl]]==
==[[Perl]]==
Line 230: Line 230:
my $var;
my $var;
my @list = (1, 2, 3);
my @list = (1, 2, 3);
$var = $_ for (@list);
$var += $_ for (@list);


Alternate
Alternate
Line 245: Line 245:
my $acc = 0;
my $acc = 0;
my @list = qw(1 2 3)
my @list = qw(1 2 3)
map { $acc = $_ } @list
map { $acc += $_ } @list


==[[PHP]]==
==[[PHP]]==
Line 260: Line 260:
lvars i, sum = 0, ar = {1 2 3 4 5 6 7 8 9};
lvars i, sum = 0, ar = {1 2 3 4 5 6 7 8 9};
for i from 1 to length(ar) do
for i from 1 to length(ar) do
ar(i) sum -> sum;
ar(i) + sum -> sum;
endfor;
endfor;


Line 266: Line 266:


lvars sum = 0, ar = {1 2 3 4 5 6 7 8 9};
lvars sum = 0, ar = {1 2 3 4 5 6 7 8 9};
appdata(ar, procedure(x); x sum -> sum; endprocedure);
appdata(ar, procedure(x); x + sum -> sum; endprocedure);


==[[Prolog]]==
==[[Prolog]]==
[[Category:Prolog]]
[[Category:Prolog]]
sum([],0).
sum([],0).
sum([H|T],X) :- sum(T,Y), X is H Y.
sum([H|T],X) :- sum(T,Y), X is H + Y.
test
test
:- sum([1,2,3,4,5,6,7,8,9],X).
:- sum([1,2,3,4,5,6,7,8,9],X).
Line 286: Line 286:


ary = [1,2,3,4,5]
ary = [1,2,3,4,5]
sum = ary.inject{|currentSum,element|currentSum element}
sum = ary.inject{|currentSum,element|currentSum+element}
# => 15
# => 15


Line 292: Line 292:
[[Category:Scala]]
[[Category:Scala]]
val array = Array(1,2,3,4,5)
val array = Array(1,2,3,4,5)
val sum = array.foldLeft(0)(_ _)
val sum = array.foldLeft(0)(_ + _)


This is a shortcut for
This is a shortcut for
val sum = array.foldLeft(0){(currentSum, element) => currentSum element}
val sum = array.foldLeft(0){(currentSum, element) => currentSum + element}


==[[Seed7]]==
==[[Seed7]]==
Line 307: Line 307:
begin
begin
for value range valueArray do
for value range valueArray do
sum := value;
sum +:= value;
end for;
end for;
end func;
end func;
Line 319: Line 319:


val array = [1,2,3,4,5];
val array = [1,2,3,4,5];
foldl op 0 array;
foldl op+ 0 array;


==[[Tcl]]==
==[[Tcl]]==
Line 326: Line 326:
Assuming the values are in a list named <tt>listname</tt>:
Assuming the values are in a list named <tt>listname</tt>:


set result [expr [join $listname ]]
set result [expr [join $listname +]]


==[[Toka]]==
==[[Toka]]==
Line 332: Line 332:


[ ( array size -- sum )
[ ( array size -- sum )
>r 0 r> [ over i swap get-element ] iterate nip ] is sum-array
>r 0 r> [ over i swap get-element + ] iterate nip ] is sum-array


==[[UNIX Shell]]==
==[[UNIX Shell]]==
Line 344: Line 344:
list="1 2 3"
list="1 2 3"
for n in $list
for n in $list
do sum="$(($sum $n))"
do sum="$(($sum + $n))"
done
done
echo $sum
echo $sum
Line 352: Line 352:
sum=0
sum=0
for n
for n
do sum="$(($sum $n))"
do sum="$(($sum + $n))"
done
done
echo $sum
echo $sum
Line 360: Line 360:
sum=0
sum=0
while read n
while read n
do sum="$(($sum $n))"
do sum="$(($sum + $n))"
done
done
echo $sum
echo $sum
Line 371: Line 371:
SUM=0;
SUM=0;
for i in $LIST; do
for i in $LIST; do
SUM=$[$SUM $i];
SUM=$[$SUM + $i];
done;
done;
echo $SUM
echo $SUM

Revision as of 01:19, 18 August 2007

Task
Sum of Array
You are encouraged to solve this task according to the task description, using any language you may know.

Compute the sum of the elements of an Array

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

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

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

 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++

Compiler: 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#

 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

array = {1, 2, 3, 4, 5}
Start = sum [x \\ x <-: array]

ColdFusion

 <cfset myArray = listToArray("1,2,3,4,5")>
 #arraySum(myArray)#

Common Lisp

(defparameter *data* #(1 2 3 4 5))
(reduce #'+ *data*)

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

pragma.enable("accumulator")
accum 0 for x in [1,2,3,4,5] { _ + x }

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

 : sum ( addr cnt -- n )
   0 -rot
   cells bounds do i @ + cell +loop ;


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

 let values = [1..10]
 sum values          -- the easy way
 foldl (+) 0 values  -- the hard way

IDL

 result = total(array)

Java

 int value = 0;
 int[] arg = new int[] { 1,2,3,4,5 };
 for (int i: arg)
   value += i;

JavaScript

var array = [1, 2, 3, 4, 5];
var sum = 0;
for(var i in array)
  sum += array[i];

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

 $list = array(1,2,3,4,5,6,7,8,9);
 echo array_sum($list);

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

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

Interpeter: Python 2.5

 total = sum([1, 2, 3, 4, 5, 6, 7, 8, 9])

Ruby

 ary = [1,2,3,4,5]
 sum = ary.inject{|currentSum,element|currentSum+element}
 # => 15

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

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)));

Standard ML

 val array = [1,2,3,4,5];
 foldl op+ 0 array;

Tcl

Assuming the values are in a list named listname:

 set result [expr [join $listname +]]

Toka

[ ( array size -- sum )
  >r 0 r> [ over i swap get-element + ] iterate nip ] is sum-array

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