Sum of Array: Difference between revisions
revert spam |
mNo edit summary |
||
Line 1: | Line 1: | ||
[http://joshuaw-wise.ifrance.com/img/styles/mala-tempora/ mala tempora currunt] [http://gabe95.ifrance.com/html/canon/ canon 500] [http://call-shotgun.ifrance.com/view/new/enermax-egaxve/ enermax eg375ax-ve] [http://flawedamythyst.ifrance.com/img/styles/danzer-pump.htm danzer pump it up] [http://alishca-st.ifrance.com/text/directory/foto-con.htm foto con gay che sborrano] [http://horcrionebay.ifrance.com/resources/articles/cellula-vegetale.htm cellula vegetale e animale] [http://flawedamythyst.ifrance.com/img/styles/doctor-zivago.htm doctor zivago] [http://dejablu503.ifrance.com/description/lib/volvo-auto.htm volvo auto] [http://lightmyfire0214.ifrance.com/topic/elezioni-angri/ elezioni angri] [http://flawedamythyst.ifrance.com/img/styles/zorba-mp.htm zorba mp3] [http://gabe95.ifrance.com/html/le-son/ le son qui tue] [http://krychan.ifrance.com/content/view/d-lgs.htm d lgs 276 03] [http://lightmyfire0214.ifrance.com/topic/hust/ hust] [http://zeakk.ifrance.com/resources/articles/lettori-wma.htm lettori wma] [http://gabe95.ifrance.com/html/dvd-sacd/ dvd sacd] [http://dejablu503.ifrance.com/description/lib/olio-carli.htm olio carli] [http://joshuaw-wise.ifrance.com/img/styles/behringer-mixer/ behringer mixer 12] [http://flawedamythyst.ifrance.com/img/styles/lettore-portatile.htm lettore portatile cd usb] [http://horcrionebay.ifrance.com/resources/articles/non-rien.htm non rien de rien] [http://sm4.ifrance.com/lib/foto-king/ foto king arthur] [http://lekusya.ifrance.com/library/html/cristina-grillo/ cristina grillo succhia un cazzone nero] [http://squoi-oop.ifrance.com/topic/pasta-lavamani/ pasta lavamani] [http://call-shotgun.ifrance.com/view/new/digital-media/ digital media pro] [http://lightmyfire0214.ifrance.com/topic/win-megazine/ win megazine] [http://sm4.ifrance.com/lib/il-grande/ il grande prato gem boy] [http://horcrionebay.ifrance.com/resources/articles/saint-george.htm saint george] [http://lekusya.ifrance.com/library/html/scarlati/ scarlati] [http://mfirishka.ifrance.com/view/new/io-ti.htm io ti penso] [http://joshuaw-wise.ifrance.com/img/styles/matrix-online/ matrix online] [http://squoi-oop.ifrance.com/topic/www-winmix/ www winmix it] [http://sm4.ifrance.com/lib/qlq-c/ qlq c30] [http://gabe95.ifrance.com/html/smspronti/ smspronti] [http://squoi-oop.ifrance.com/topic/sara-acampora/ sara acampora] [http://krychan.ifrance.com/content/view/caparezza-habemus.htm caparezza habemus capa] [http://squoi-oop.ifrance.com/topic/the-planetary/ the planetary] [http://nokros.ifrance.com/data/verbatim-dvd/ verbatim dvd r 8 5] [http://lekusya.ifrance.com/library/html/lose-it/ lose it] [http://sm4.ifrance.com/lib/www-movilnet/ www movilnet com] [http://trumanburb.ifrance.com/view/new/amd-dissipatori.htm amd dissipatori 754] [http://alishca-st.ifrance.com/text/directory/mandex.htm mandex] [http://atrayah.ifrance.com/topic/blasco-eusebio.htm blasco, eusebio] [http://alishca-st.ifrance.com/text/directory/sapphire-radeon.htm sapphire radeon 9200] [http://lekusya.ifrance.com/library/html/armadillo-kinght/ armadillo kinght 3] [http://gabe95.ifrance.com/html/ricevitore-gps/ ricevitore gps auto] [http://gajar.ifrance.com/html/lebanese.htm lebanese] [http://horcrionebay.ifrance.com/resources/articles/foto-di.htm foto di cartoni hentai] [http://nokros.ifrance.com/data/king-of/ king of queens] [http://trumanburb.ifrance.com/view/new/b-b.htm b b camilla roma] [http://joshuaw-wise.ifrance.com/img/styles/sanitarios/ sanitarios] [http://alishca-st.ifrance.com/text/directory/microsoft-flight.htm microsoft flight simulator] [http://atrayah.ifrance.com/topic/epl.htm epl 7100] [http://lightmyfire0214.ifrance.com/topic/happy-days/ happy days sister act] [http://smegmacheez.ifrance.com/content/view/la-freccia/ la freccia nera. vol. 02] [http://krychan.ifrance.com/content/view/fuerte-olimpo.htm fuerte olimpo] [http://squoi-oop.ifrance.com/topic/acanthion/ acanthion] [http://dejablu503.ifrance.com/description/lib/volvo-v.htm volvo v50 kinetic 2 0 diesel] [http://mfirishka.ifrance.com/view/new/scarpi-donna.htm scarpi donna] [http://flawedamythyst.ifrance.com/img/styles/mp-usb.htm mp3 usb 1gb] [http://joshuaw-wise.ifrance.com/img/styles/internet-explorer/ internet explorer 5] [http://call-shotgun.ifrance.com/view/new/nec-videoproiettori/ nec videoproiettori] [http://gajar.ifrance.com/html/fate-ed.htm fate ed elfi] [http://smegmacheez.ifrance.com/content/view/bonita-de/ bonita de mas] [http://zeakk.ifrance.com/resources/articles/san-bernardino.htm san bernardino mountains] [http://horcrionebay.ifrance.com/resources/articles/memory-olimpus.htm memory olimpus] [http://call-shotgun.ifrance.com/view/new/adult-dating/ adult dating service online] [http://smegmacheez.ifrance.com/content/view/sapphire-radeon/ sapphire radeon 9600xt ultimate] [http://gabe95.ifrance.com/html/puede-ser/ puede ser] [http://mfirishka.ifrance.com/view/new/la-storia.htm la storia del trasmettitore fm] [http://mfirishka.ifrance.com/view/new/nure.htm nure] [http://lekusya.ifrance.com/library/html/tuner-tv/ tuner tv acquisizione e tv] [http://gajar.ifrance.com/html/midi-musica.htm midi musica italiana] [http://gajar.ifrance.com/html/ipertrofia-benigna.htm ipertrofia benigna prostata] [http://nokros.ifrance.com/data/hooverphonics/ hooverphonics] [http://lekusya.ifrance.com/library/html/reti-letto/ reti letto] [http://joshuaw-wise.ifrance.com/img/styles/salute-dei/ salute dei capello] [http://smegmacheez.ifrance.com/content/view/haydee-raymundo/ haydee raymundo] [http://atrayah.ifrance.com/topic/scaleoview-t.htm scaleoview t17-2] [http://nokros.ifrance.com/data/effeminato/ effeminato] [http://alishca-st.ifrance.com/text/directory/phyllis-haver.htm phyllis haver] [http://sm4.ifrance.com/lib/acufeno/ acufeno] [http://nokros.ifrance.com/data/foame-de/ foame de bani] [http://mfirishka.ifrance.com/view/new/warlock-iii.htm warlock iii] [http://mfirishka.ifrance.com/view/new/gbc-rilegatrici.htm gbc rilegatrici] [http://alishca-st.ifrance.com/text/directory/roma-bruxelles.htm roma bruxelles biglietti aerei] [http://squoi-oop.ifrance.com/topic/asciugabiancheria-san/ asciugabiancheria san giorgio] [http://mfirishka.ifrance.com/view/new/panasonic-lumix.htm panasonic lumix 2] [http://squoi-oop.ifrance.com/topic/car-insurance/ car insurance quotes] [http://dejablu503.ifrance.com/description/lib/eddy-jam.htm eddy jam] [http://gabe95.ifrance.com/html/www-literary/ www literary it] [http://trumanburb.ifrance.com/view/new/lua.htm lua] [http://dejablu503.ifrance.com/description/lib/thoia.htm thoia] [http://dejablu503.ifrance.com/description/lib/cover-vasco.htm cover vasco buoni o cattivi] [http://call-shotgun.ifrance.com/view/new/codici-blocco/ codici blocco nec e313] [http://alishca-st.ifrance.com/text/directory/burana-carmina.htm burana, carmina] [http://trumanburb.ifrance.com/view/new/zecchino-d.htm zecchino d oro 2003] [http://mfirishka.ifrance.com/view/new/asrock-k.htm asrock k7] [http://dejablu503.ifrance.com/description/lib/www-capo.htm www capo carbonara it] [http://mfirishka.ifrance.com/view/new/i-on.htm i on t wonna know] [http://zeakk.ifrance.com/resources/articles/milano-saragozza.htm milano saragozza biglietti aerei] [http://krychan.ifrance.com/content/view/aspirapolvere-parquet.htm aspirapolvere parquet 8296] [http://smegmacheez.ifrance.com/content/view/beni-benassi/ beni benassi hit my heart] [http://cocopuff66.ifrance.com/text/directory/agp-x/ agp 1x] [http://lekusya.ifrance.com/library/html/philips-mp/ philips mp3 key015] [http://call-shotgun.ifrance.com/view/new/calcetto-ridotto/ calcetto ridotto] [http://gabe95.ifrance.com/html/stampante-barcode/ stampante barcode] [http://alishca-st.ifrance.com/text/directory/nuova-opel.htm nuova opel astra] [http://lightmyfire0214.ifrance.com/topic/alternativi/ alternativi] [http://horcrionebay.ifrance.com/resources/articles/la-quindicesima.htm la quindicesima epistola] [http://sm4.ifrance.com/lib/runaway/ runaway] [http://zeakk.ifrance.com/resources/articles/thomas-forsberg.htm thomas forsberg] [http://zeakk.ifrance.com/resources/articles/assicurazione-san.htm assicurazione san giuliano milanese] [http://joshuaw-wise.ifrance.com/img/styles/athlon-/ athlon 64 bit] [http://lekusya.ifrance.com/library/html/summer-sunshine/ summer sunshine midi] [http://lightmyfire0214.ifrance.com/topic/scarpa-donna/ scarpa donna] [http://cocopuff66.ifrance.com/text/directory/converse-all/ converse all star chuck taylor] [http://squoi-oop.ifrance.com/topic/www-satu/ www sat4u org] [http://trumanburb.ifrance.com/view/new/www-figc.htm www figc it] [http://mfirishka.ifrance.com/view/new/ives-frederic.htm ives, frederic eugene] [http://alishca-st.ifrance.com/text/directory/albergo-.htm albergo 4 stelle ginevra] [http://krychan.ifrance.com/content/view/i-mate.htm i mate] [http://horcrionebay.ifrance.com/resources/articles/zoe.htm zoe] [http://lekusya.ifrance.com/library/html/palmare/ palmare 640] [http://gajar.ifrance.com/html/vino-passito.htm vino passito] [http://flawedamythyst.ifrance.com/img/styles/vocabolario-italiano.htm vocabolario italiano russo] [http://lightmyfire0214.ifrance.com/topic/justin-e/ justin e britney insieme] [http://lekusya.ifrance.com/library/html/malattia-di/ malattia di hodgkin] [http://gajar.ifrance.com/html/cerrar.htm cerrar] [http://sm4.ifrance.com/lib/ipaq/ ipaq 3115] [http://lekusya.ifrance.com/library/html/passera-di/ passera di michel hunzicher] [http://lekusya.ifrance.com/library/html/parma-shopping/ parma shopping] [http://joshuaw-wise.ifrance.com/img/styles/video-porno/ video porno di britney spears] [http://cocopuff66.ifrance.com/text/directory/editor-/ editor 2 0 skynet] [http://alishca-st.ifrance.com/text/directory/pubblicita-adidas.htm pubblicita adidas nadia] [http://dejablu503.ifrance.com/description/lib/ricoh-aficio.htm ricoh aficio fx12] [http://gajar.ifrance.com/html/seagate-.htm seagate 7200 8 sata] [http://gajar.ifrance.com/html/rover.htm rover 100] [http://alishca-st.ifrance.com/text/directory/www-polini.htm www polini it] [http://krychan.ifrance.com/content/view/la-guerra.htm la guerra in irak] [http://call-shotgun.ifrance.com/view/new/decreto-immobili/ decreto immobili] [http://smegmacheez.ifrance.com/content/view/lhassa-apso/ lhassa apso] [http://zeakk.ifrance.com/resources/articles/porsche-roma.htm porsche roma] [http://gabe95.ifrance.com/html/fredrikshamn/ fredrikshamn] [http://joshuaw-wise.ifrance.com/img/styles/here-j/ here j go again] [http://squoi-oop.ifrance.com/topic/hp-/ hp 1200 toner] [http://smegmacheez.ifrance.com/content/view/him-dark/ him dark light] [http://gajar.ifrance.com/html/donna-sesso.htm donna sesso animali] [http://lightmyfire0214.ifrance.com/topic/bmw-k/ bmw k 1200 s] [http://atrayah.ifrance.com/topic/gioco-ps.htm gioco ps2 sport] [http://alishca-st.ifrance.com/text/directory/hotel-economico.htm hotel economico marsiglia] [http://gabe95.ifrance.com/html/it-is/ it is cause i m cool] [http://alishca-st.ifrance.com/text/directory/comune-di.htm comune di vicchio] [http://mfirishka.ifrance.com/view/new/troy-trailer.htm troy trailer] [http://alishca-st.ifrance.com/text/directory/j-teme.htm j teme] [http://lightmyfire0214.ifrance.com/topic/scarpe-volley/ scarpe volley] [http://sm4.ifrance.com/lib/hyperballoid/ hyperballoid] [http://gabe95.ifrance.com/html/hazzard-la/ hazzard. la prima stagione completa] [http://call-shotgun.ifrance.com/view/new/frimley-and/ frimley and camberley] [http://sm4.ifrance.com/lib/ho/ ho 945] [http://sm4.ifrance.com/lib/donna-nuda/ donna nuda e pipi] [http://zeakk.ifrance.com/resources/articles/tyan-sli.htm tyan sli] [http://atrayah.ifrance.com/topic/cd-vasco.htm cd vasco] [http://gabe95.ifrance.com/html/ncch-dl/ ncch dl] [http://dejablu503.ifrance.com/description/lib/ottomano.htm ottomano] [http://gajar.ifrance.com/html/jeep-wrangler.htm jeep wrangler 2.5] [http://gajar.ifrance.com/html/mitac-mio.htm mitac mio 168 256 palmari] [http://nokros.ifrance.com/data/gola-profonda/ gola profonda nera] [http://krychan.ifrance.com/content/view/hot-porn.htm hot porn] [http://call-shotgun.ifrance.com/view/new/ccnl-vetrerie/ ccnl vetrerie] [http://joshuaw-wise.ifrance.com/img/styles/piedi-nudi/ piedi nudi femminili fetish] [http://lekusya.ifrance.com/library/html/comune-di/ comune di stella] [http://sm4.ifrance.com/lib/berlinale/ berlinale] [http://atrayah.ifrance.com/topic/traduzione-if.htm traduzione if ain t got you] [http://joshuaw-wise.ifrance.com/img/styles/un-possibile/ un possibile titolo per king of fighters 3d] [http://dejablu503.ifrance.com/description/lib/conseguenze-di.htm conseguenze di una notte] [http://squoi-oop.ifrance.com/topic/schweiz/ schweiz] [http://cocopuff66.ifrance.com/text/directory/prestito-padova/ prestito padova] [http://zeakk.ifrance.com/resources/articles/cavo-dati.htm cavo dati per motorola] [http://krychan.ifrance.com/content/view/mario-catcher.htm mario catcher] [http://zeakk.ifrance.com/resources/articles/kevin-lyttle.htm kevin lyttle music] [http://sm4.ifrance.com/lib/tastiera-musicale/ tastiera musicale usb] [http://squoi-oop.ifrance.com/topic/minch-the/ minch, the-] [http://zeakk.ifrance.com/resources/articles/o-.htm o 190 chrome cromo] [http://squoi-oop.ifrance.com/topic/tv-samsung/ tv samsung lcd 20] [http://lightmyfire0214.ifrance.com/topic/incontro-senigallia/ incontro senigallia] [http://nokros.ifrance.com/data/punto-y/ punto y aparte tego calderon] [http://gajar.ifrance.com/html/le-nozze.htm le nozze di zeus] [http://alishca-st.ifrance.com/text/directory/giochi-di.htm giochi di carte scopa] [http://trumanburb.ifrance.com/view/new/win-it.htm win it] [http://dejablu503.ifrance.com/description/lib/yaesu-vx.htm yaesu vx 150] [http://gabe95.ifrance.com/html/pattinaggio-ghiaccio/ pattinaggio ghiaccio] [http://nokros.ifrance.com/data/nomadi-quaranta/ nomadi quaranta] [http://lightmyfire0214.ifrance.com/topic/costruisci-casa/ costruisci casa] [http://smegmacheez.ifrance.com/content/view/mb-corporation/ mb corporation] [http://trumanburb.ifrance.com/view/new/giochi-su.htm giochi su marry potter] [http://flawedamythyst.ifrance.com/img/styles/rumori-treno.htm rumori treno] [http://lekusya.ifrance.com/library/html/yetisport/ yetisport 1] [http://flawedamythyst.ifrance.com/img/styles/fscaller-code.htm fscaller code] [http://call-shotgun.ifrance.com/view/new/watch-christian/ watch christian tv] [http://squoi-oop.ifrance.com/topic/kaspersky-anti/ kaspersky anti virus] [http://joshuaw-wise.ifrance.com/img/styles/ministero-dell/ ministero dell ambiente] {{task}} |
|||
{{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 |
$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 |
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 |
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 |
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 |
sum = list[i]; |
||
} |
} |
||
} |
} |
||
Line 69: | Line 69: | ||
{ |
{ |
||
int list[] = { 1, 2, 3 } ; |
int list[] = { 1, 2, 3 } ; |
||
std::accumulate(list, list |
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 |
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 |
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 |
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 #' |
(reduce #' *data*) |
||
==[[Delphi]]== |
==[[Delphi]]== |
||
Line 146: | Line 146: | ||
pragma.enable("accumulator") |
pragma.enable("accumulator") |
||
accum 0 for x in [1,2,3,4,5] { _ |
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 |
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 |
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 @ |
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 |
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 ( |
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 |
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 |
sum = array[i]; |
||
==[[Perl]]== |
==[[Perl]]== |
||
Line 230: | Line 230: | ||
my $var; |
my $var; |
||
my @list = (1, 2, 3); |
my @list = (1, 2, 3); |
||
$var |
$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 |
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) |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
>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 |
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 |
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 |
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 |
SUM=$[$SUM $i]; |
||
done; |
done; |
||
echo $SUM |
echo $SUM |
Revision as of 04:00, 24 August 2007
mala tempora currunt canon 500 enermax eg375ax-ve danzer pump it up foto con gay che sborrano cellula vegetale e animale doctor zivago volvo auto elezioni angri zorba mp3 le son qui tue d lgs 276 03 hust lettori wma dvd sacd olio carli behringer mixer 12 lettore portatile cd usb non rien de rien foto king arthur cristina grillo succhia un cazzone nero pasta lavamani digital media pro win megazine il grande prato gem boy saint george scarlati io ti penso matrix online www winmix it qlq c30 smspronti sara acampora caparezza habemus capa the planetary verbatim dvd r 8 5 lose it www movilnet com amd dissipatori 754 mandex blasco, eusebio sapphire radeon 9200 armadillo kinght 3 ricevitore gps auto lebanese foto di cartoni hentai king of queens b b camilla roma sanitarios microsoft flight simulator epl 7100 happy days sister act la freccia nera. vol. 02 fuerte olimpo acanthion volvo v50 kinetic 2 0 diesel scarpi donna mp3 usb 1gb internet explorer 5 nec videoproiettori fate ed elfi bonita de mas san bernardino mountains memory olimpus adult dating service online sapphire radeon 9600xt ultimate puede ser la storia del trasmettitore fm nure tuner tv acquisizione e tv midi musica italiana ipertrofia benigna prostata hooverphonics reti letto salute dei capello haydee raymundo scaleoview t17-2 effeminato phyllis haver acufeno foame de bani warlock iii gbc rilegatrici roma bruxelles biglietti aerei asciugabiancheria san giorgio panasonic lumix 2 car insurance quotes eddy jam www literary it lua thoia cover vasco buoni o cattivi codici blocco nec e313 burana, carmina zecchino d oro 2003 asrock k7 www capo carbonara it i on t wonna know milano saragozza biglietti aerei aspirapolvere parquet 8296 beni benassi hit my heart agp 1x philips mp3 key015 calcetto ridotto stampante barcode nuova opel astra alternativi la quindicesima epistola runaway thomas forsberg assicurazione san giuliano milanese athlon 64 bit summer sunshine midi scarpa donna converse all star chuck taylor www sat4u org www figc it ives, frederic eugene albergo 4 stelle ginevra i mate zoe palmare 640 vino passito vocabolario italiano russo justin e britney insieme malattia di hodgkin cerrar ipaq 3115 passera di michel hunzicher parma shopping video porno di britney spears editor 2 0 skynet pubblicita adidas nadia ricoh aficio fx12 seagate 7200 8 sata rover 100 www polini it la guerra in irak decreto immobili lhassa apso porsche roma fredrikshamn here j go again hp 1200 toner him dark light donna sesso animali bmw k 1200 s gioco ps2 sport hotel economico marsiglia it is cause i m cool comune di vicchio troy trailer j teme scarpe volley hyperballoid hazzard. la prima stagione completa frimley and camberley ho 945 donna nuda e pipi tyan sli cd vasco ncch dl ottomano jeep wrangler 2.5 mitac mio 168 256 palmari gola profonda nera hot porn ccnl vetrerie piedi nudi femminili fetish comune di stella berlinale traduzione if ain t got you un possibile titolo per king of fighters 3d conseguenze di una notte schweiz prestito padova cavo dati per motorola mario catcher kevin lyttle music tastiera musicale usb minch, the- o 190 chrome cromo tv samsung lcd 20 incontro senigallia punto y aparte tego calderon le nozze di zeus giochi di carte scopa win it yaesu vx 150 pattinaggio ghiaccio nomadi quaranta costruisci casa mb corporation giochi su marry potter rumori treno yetisport 1 fscaller code watch christian tv kaspersky anti virus ministero dell ambiente
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
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