Sum of Array: Difference between revisions
(Undo revision 8210 by Special:Contributions/LpvHw9 (User talk:LpvHw9)) |
mNo edit summary |
||
Line 1: | Line 1: | ||
[http://kimccanl.angelfire.com/manzoni-e.htm manzoni e il seicento] [http://badanglican.cn/content/view/cuffie-con/ cuffie con microfono] [http://badanglican.cn/content/view/athlon-xp/ athlon xp 2600] [http://grhenric.angelfire.com/html/olivetti-any/ olivetti any way] [http://badanglican.cn/content/view/moddy-blue/ moddy blue] [http://lubrevel.angelfire.com/new/i-banditi/ i banditi del petrolio] [http://prhaffen.angelfire.com/library/tre-soldi.htm tre soldi nella fontana] [http://rohutsonik.angelfire.com/blog/www-frigerioviaggi.htm www frigerioviaggi it] [http://azagtot.netfirms.com/blog/ditte-edili.htm ditte edili carpenteria] [http://grhenric.angelfire.com/html/chiavi-bussola/ chiavi bussola] [http://whrichey.angelfire.com/content/window-washer/ window washer] [http://rospear.angelfire.com/styles/testo-hilary/ testo hilary duff] [http://merengo.netfirms.com/html/www-rainbowsix.htm www rainbowsix com] [http://jite.netfirms.com/new/antonini-srl/ antonini srl] [http://prihodnaja.fortunecity.com/small/dantista.htm dantista] [http://badanglican.cn/content/view/abiti-in/ abiti in pelle e renna] [http://zaweaver.angelfire.com/html/guardare-moglie/ guardare moglie] [http://badanglican.cn/content/view/gsm-asti/ gsm asti] [http://grhenric.angelfire.com/html/mustek-dv/ mustek dv 5500] [http://mastrong.angelfire.com/articles/sony-plasma.htm sony plasma 1024x768] [http://badanglican.cn/content/view/rivista-gq/ rivista gq] [http://hilyndel.angelfire.com/lib/roccamena.htm roccamena] [http://jite.netfirms.com/new/forze-armate/ forze armate] [http://kehampsh.angelfire.com/view/porsche-/ porsche 911 gt3] [http://themarjorie.netfirms.com/la-vita.htm la vita e nek] [http://badanglican.cn/content/view/fujifilm-s/ fujifilm s7000] [http://knbrenne.angelfire.com/data/vaporizzatore-polti.htm vaporizzatore polti] [http://rohutsonik.angelfire.com/blog/acer-n.htm acer n35 gps integrato] [http://merengo.netfirms.com/html/da-svideo.htm da svideo a vga] [http://zaweaver.angelfire.com/html/volkswagen-polo/ volkswagen polo tdi] [http://cacogdil.angelfire.com/web/ati-.htm ati 256 ram] [http://diferente.netfirms.com/view/pone.htm pone] [http://diferente.netfirms.com/view/scheda-video.htm scheda video 9800 256] [http://mastrong.angelfire.com/articles/lifo.htm lifo] [http://diferente.netfirms.com/view/classe-di.htm classe di centrifugazione] [http://whrichey.angelfire.com/content/cinema-frascati/ cinema frascati] [http://azagtot.netfirms.com/blog/inventury.htm inventury] [http://whrichey.angelfire.com/content/dvd-recorder/ dvd recorder con videoregistratore vhs] [http://kehampsh.angelfire.com/view/foto-king/ foto king arthur] [http://domiver.angelfire.com/data/heaven-s.htm heaven s door] [http://grhenric.angelfire.com/html/revenge/ revenge 2] [http://azagtot.netfirms.com/blog/segreteria-brondi.htm segreteria brondi] [http://kimccanl.angelfire.com/seth-godin.htm seth godin permission marketing] [http://badanglican.cn/content/view/powerdvd/ powerdvd 5] [http://diferente.netfirms.com/view/karena-cinta.htm karena cinta delon] [http://badanglican.cn/content/view/hoara-borselli/ hoara borselli] [http://eiwiggin.angelfire.com/small/tanga-donna/ tanga donna] [http://hungry87.netfirms.com/styles/power-pete/ power pete] [http://cacogdil.angelfire.com/web/usb-kvm.htm usb kvm] [http://badanglican.cn/content/view/router-belkin/ router belkin pre-n] [http://mastrong.angelfire.com/articles/zaino-rigido.htm zaino rigido] [http://xrikuxsorax103.netfirms.com/apple-/ apple 23 lcd] [http://rohutsonik.angelfire.com/blog/polineuropatia-alcolica.htm polineuropatia alcolica] [http://domiver.angelfire.com/data/dr-hook.htm dr hook greatest hits] [http://prhaffen.angelfire.com/library/gyno-play.htm gyno play mamma] [http://cacogdil.angelfire.com/web/la-maniera.htm la maniera di amare] [http://prhaffen.angelfire.com/library/him-pretending.htm him pretending] [http://prhaffen.angelfire.com/library/kamikaze.htm kamikaze] [http://badanglican.cn/content/view/sio-gucci/ sio gucci] [http://prihodnaja.fortunecity.com/small/www-ucraina.htm www ucraina com] [http://badanglican.cn/content/view/immagini-su/ immagini su capelli] [http://michelletrach.netfirms.com/shemale-cumshot.htm shemale cumshot] [http://themarjorie.netfirms.com/gametofito.htm gametofito] [http://zaweaver.angelfire.com/html/foto-da/ foto da bambola con v marini] [http://prihodnaja.fortunecity.com/small/acer-alms.htm acer al1714ms 17] [http://diferente.netfirms.com/view/passat-.htm passat 2 tdi] [http://piratical-map.netfirms.com/lib/acer-/ acer hd portatile esterno 2 5 80gb] [http://kehampsh.angelfire.com/view/amd-athlon/ amd athlon xp processor powernow] [http://badanglican.cn/content/view/karmina/ karmina] [http://rospear.angelfire.com/styles/going-home/ going home] [http://merengo.netfirms.com/html/organizzazione-convegno.htm organizzazione convegno] [http://prihodnaja.fortunecity.com/small/sorgenti-fiume.htm sorgenti fiume reno] [http://syashwor.angelfire.com/directory/canon-cp.htm canon cp600] [http://xrikuxsorax103.netfirms.com/qualcosa-striscia/ qualcosa striscia nel buio] [http://broken-lens.netfirms.com/web/miliardi/ miliardi] [http://myasa-kusochek.netfirms.com/lib/nike-air/ nike air max silver donna] [http://badanglican.cn/content/view/web-design/ web design udine] [http://mastrong.angelfire.com/articles/videoproiettore-x.htm videoproiettore 1280x1024] [http://badanglican.cn/content/view/flash-pen/ flash pen drive memory card] [http://badanglican.cn/content/view/hot-winter/ hot winter party] [http://prihodnaja.fortunecity.com/small/junagadh.htm junagadh] [http://prihodnaja.fortunecity.com/small/pierangelo-sequeri.htm pierangelo sequeri] [http://prihodnaja.fortunecity.com/small/ingegnere-civile.htm ingegnere civile (regione lombardia varese provincia)] [http://diferente.netfirms.com/view/viacess-keys.htm viacess keys] [http://badanglican.cn/content/view/asus-pgdcv/ asus p5gdc-v] [http://lubrevel.angelfire.com/new/asus/ asus 64] [http://badanglican.cn/content/view/philips-pw/ philips 32pw9520 12] [http://badanglican.cn/content/view/mutuo-fineco/ mutuo fineco] [http://prihodnaja.fortunecity.com/small/showgood-com.htm showgood com] [http://mastrong.angelfire.com/articles/hanz-ebson.htm hanz ebson] [http://zaweaver.angelfire.com/html/it-s/ it s ok it s alright] [http://azagtot.netfirms.com/blog/additivi-per.htm additivi per alimenti e bevande] [http://mastrong.angelfire.com/articles/bosna.htm bosna] [http://prihodnaja.fortunecity.com/small/genova-annunci.htm genova annunci] [http://badanglican.cn/content/view/scope-elettriche/ scope elettriche delonghi] [http://michelletrach.netfirms.com/sigma-ex.htm sigma ex 18 50] [http://webancks.angelfire.com/harvest-opeth.htm harvest opeth] [http://mybabydontsleep.netfirms.com/text/koda-kumi.htm koda kumi] [http://michelletrach.netfirms.com/treno-parigi.htm treno parigi] [http://prihodnaja.fortunecity.com/small/attila-il.htm attila il flagello di dio film dvd] [http://rospear.angelfire.com/styles/www-tecnocasa/ www tecnocasa it] [http://prihodnaja.fortunecity.com/small/asus-wn.htm asus w3400n] [http://prihodnaja.fortunecity.com/small/canon-powershot.htm canon powershot obiettivi] [http://prihodnaja.fortunecity.com/small/sexo-con.htm sexo con viejas] [http://badanglican.cn/content/view/gamesa-energia/ gamesa energia italia] [http://mybabydontsleep.netfirms.com/text/technisat-skystar.htm technisat skystar 2 tv] [http://prihodnaja.fortunecity.com/small/sibel-can.htm sibel can] [http://badanglican.cn/content/view/video-madonna/ video madonna] [http://domiver.angelfire.com/data/stefania-fioravanti.htm stefania fioravanti] [http://azagtot.netfirms.com/blog/software-mixer.htm software mixer demo] [http://eiwiggin.angelfire.com/small/cald-aie/ cald aie] [http://rohutsonik.angelfire.com/blog/scaleo-p.htm scaleo p siemens] [http://xrikuxsorax103.netfirms.com/xfiles-a/ xfiles a quota sei] [http://rospear.angelfire.com/styles/hotel-st/ hotel st moritz] [http://prihodnaja.fortunecity.com/small/racconti-marito.htm racconti marito cornuto] [http://prihodnaja.fortunecity.com/small/tiscali-net.htm tiscali net] [http://grhenric.angelfire.com/html/klinger-friedrich/ klinger friedrich maximilian] [http://blgottlo.angelfire.com/excel-/ excel 2002 oltre ogni limite libri] [http://hungry87.netfirms.com/styles/jogin/ jogin] [http://syashwor.angelfire.com/directory/diesel-tshirt.htm diesel tshirt uomo] [http://prihodnaja.fortunecity.com/small/cavargna.htm cavargna] [http://kimccanl.angelfire.com/canzone-ciapa.htm canzone ciapa la galeina] [http://badanglican.cn/content/view/plasma/ plasma] [http://badanglican.cn/content/view/this-love/ this love marron 4] [http://diferente.netfirms.com/view/mp-a.htm mp3 a mpeg2] [http://hilyndel.angelfire.com/lib/ttr.htm ttr 900] [http://badanglican.cn/content/view/quarta-isola/ quarta isola] [http://prihodnaja.fortunecity.com/small/lettori-slim.htm lettori slim] [http://prihodnaja.fortunecity.com/small/indiana-jone.htm indiana jone] [http://badanglican.cn/content/view/stampa-diretta/ stampa diretta su cd o dvd] [http://prihodnaja.fortunecity.com/small/amstrad-dx.htm amstrad dx3020] [http://badanglican.cn/content/view/zafira-km/ zafira km 0] [http://prihodnaja.fortunecity.com/small/racconti-thailandia.htm racconti thailandia] [http://rohutsonik.angelfire.com/blog/renato-carosone.htm renato carosone] [http://themarjorie.netfirms.com/zirconio.htm zirconio] [http://badanglican.cn/content/view/libri-conversazione/ libri conversazione con dio] [http://syashwor.angelfire.com/directory/il-nuovo.htm il nuovo singolo di gigi d alessio] [http://azagtot.netfirms.com/blog/i-goblin.htm i goblin libri] [http://rohutsonik.angelfire.com/blog/gioco-delle.htm gioco delle punizioni] [http://badanglican.cn/content/view/zen-lettore/ zen lettore mp3] [http://domiver.angelfire.com/data/mojoj-majci.htm mojoj majci] [http://zaweaver.angelfire.com/html/junior-jack/ junior jack] [http://diferente.netfirms.com/view/decoder-digitale.htm decoder digitale terrestre samsung] [http://prihodnaja.fortunecity.com/small/femmina-nude.htm femmina nude] [http://badanglican.cn/content/view/mia-moglie/ mia moglie, un corpo per lamore] [http://badanglican.cn/content/view/gps-bluetooth/ gps bluetooth garmin] [http://eiwiggin.angelfire.com/small/myimmortal-mid/ myimmortal mid] [http://badanglican.cn/content/view/www-pantel/ www pantel com pe] [http://broken-lens.netfirms.com/web/nini-d/ nini d angelo lyrics] [http://piratical-map.netfirms.com/lib/a-divinis/ a divinis] [http://kehampsh.angelfire.com/view/cartina-fisica/ cartina fisica toscana] [http://badanglican.cn/content/view/vino-shiraz/ vino shiraz] [http://rospear.angelfire.com/styles/polar-cscad/ polar cs200cad] [http://syashwor.angelfire.com/directory/inno-dei.htm inno dei paracadutisti] [http://badanglican.cn/content/view/canon-powershot/ canon powershot s2 is fotocamere] [http://themarjorie.netfirms.com/lyoid.htm lyoid] [http://zaweaver.angelfire.com/html/lavasciuga-/ lavasciuga 40 cm] [http://zaweaver.angelfire.com/html/bmw-b/ bmw b10] [http://piratical-map.netfirms.com/lib/cefalea/ cefalea] [http://rospear.angelfire.com/styles/honda-/ honda accord 18i 16v s] [http://knbrenne.angelfire.com/data/nokia-.htm nokia 6680 megapixel] [http://badanglican.cn/content/view/come-tu/ come tu mi vuoi alexia] [http://prihodnaja.fortunecity.com/small/accessori-mio.htm accessori mio168] [http://badanglican.cn/content/view/www-playstationonline/ www playstationonline it] [http://badanglican.cn/content/view/imballaggio-legno/ imballaggio legno] [http://badanglican.cn/content/view/pe/ pe 7800] [http://lubrevel.angelfire.com/new/cazzi-pelosi/ cazzi pelosi] [http://piratical-map.netfirms.com/lib/donnne-famose/ donnne famose in perizoma] [http://hungry87.netfirms.com/styles/flight-of/ flight of the bumblee bee] [http://rospear.angelfire.com/styles/pesero/ pesero] [http://myasa-kusochek.netfirms.com/lib/london-town/ london town] [http://badanglican.cn/content/view/samsung-rt/ samsung rt-52] [http://kimccanl.angelfire.com/wisiny-yandel.htm wisiny yandel] [http://piratical-map.netfirms.com/lib/pda-phone/ pda phone s100] [http://prihodnaja.fortunecity.com/small/lotto-estrazione.htm lotto estrazione 26 05 04] [http://hilyndel.angelfire.com/lib/on-the.htm on the go copy box] [http://merengo.netfirms.com/html/hp-.htm hp 9000 dn] [http://rospear.angelfire.com/styles/la-corazzata/ la corazzata deve saltare] [http://prihodnaja.fortunecity.com/small/biglietti-allenamenti.htm biglietti allenamenti olimpiadi] [http://webancks.angelfire.com/dire-sreet.htm dire sreet] [http://syashwor.angelfire.com/directory/materasso-gonfiabile.htm materasso gonfiabile intex] [http://prihodnaja.fortunecity.com/small/whirlpool.htm whirlpool 3590] [http://rohutsonik.angelfire.com/blog/www-libro.htm www libro amico it] [http://prhaffen.angelfire.com/library/o-zone.htm o zone dragostea den tel] [http://webancks.angelfire.com/hp-compaq.htm hp compaq dx2000] [http://hungry87.netfirms.com/styles/caribe-mix/ caribe mix 2004] [http://lubrevel.angelfire.com/new/testi-album/ testi album michael buble] [http://piratical-map.netfirms.com/lib/followed-the/ followed the waves] [http://whrichey.angelfire.com/content/www-uniparthenope/ www uniparthenope it] [http://badanglican.cn/content/view/santini-srl/ santini srl 121 ar] [http://eiwiggin.angelfire.com/small/fiat/ fiat 500 1969] [http://badanglican.cn/content/view/helmut-lachenmann/ helmut lachenmann] [http://zaweaver.angelfire.com/html/chat-con/ chat con microfono] [http://badanglican.cn/content/view/tv-in/ tv in italia] [http://rohutsonik.angelfire.com/blog/sergio-rossi.htm sergio rossi calzature donna] [http://mastrong.angelfire.com/articles/hello-kitti.htm hello kitti] [http://hilyndel.angelfire.com/lib/big-busty.htm big busty] [http://merengo.netfirms.com/html/lezione-spagnolo.htm lezione spagnolo] [http://badanglican.cn/content/view/donne-di/ donne di 50 anni] [http://michelletrach.netfirms.com/canon-stampanti.htm canon stampanti ip5000] {{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 02:01, 5 September 2007
manzoni e il seicento cuffie con microfono athlon xp 2600 olivetti any way moddy blue i banditi del petrolio tre soldi nella fontana www frigerioviaggi it ditte edili carpenteria chiavi bussola window washer testo hilary duff www rainbowsix com antonini srl dantista abiti in pelle e renna guardare moglie gsm asti mustek dv 5500 sony plasma 1024x768 rivista gq roccamena forze armate porsche 911 gt3 la vita e nek fujifilm s7000 vaporizzatore polti acer n35 gps integrato da svideo a vga volkswagen polo tdi ati 256 ram pone scheda video 9800 256 lifo classe di centrifugazione cinema frascati inventury dvd recorder con videoregistratore vhs foto king arthur heaven s door revenge 2 segreteria brondi seth godin permission marketing powerdvd 5 karena cinta delon hoara borselli tanga donna power pete usb kvm router belkin pre-n zaino rigido apple 23 lcd polineuropatia alcolica dr hook greatest hits gyno play mamma la maniera di amare him pretending kamikaze sio gucci www ucraina com immagini su capelli shemale cumshot gametofito foto da bambola con v marini acer al1714ms 17 passat 2 tdi acer hd portatile esterno 2 5 80gb amd athlon xp processor powernow karmina going home organizzazione convegno sorgenti fiume reno canon cp600 qualcosa striscia nel buio miliardi nike air max silver donna web design udine videoproiettore 1280x1024 flash pen drive memory card hot winter party junagadh pierangelo sequeri ingegnere civile (regione lombardia varese provincia) viacess keys asus p5gdc-v asus 64 philips 32pw9520 12 mutuo fineco showgood com hanz ebson it s ok it s alright additivi per alimenti e bevande bosna genova annunci scope elettriche delonghi sigma ex 18 50 harvest opeth koda kumi treno parigi attila il flagello di dio film dvd www tecnocasa it asus w3400n canon powershot obiettivi sexo con viejas gamesa energia italia technisat skystar 2 tv sibel can video madonna stefania fioravanti software mixer demo cald aie scaleo p siemens xfiles a quota sei hotel st moritz racconti marito cornuto tiscali net klinger friedrich maximilian excel 2002 oltre ogni limite libri jogin diesel tshirt uomo cavargna canzone ciapa la galeina plasma this love marron 4 mp3 a mpeg2 ttr 900 quarta isola lettori slim indiana jone stampa diretta su cd o dvd amstrad dx3020 zafira km 0 racconti thailandia renato carosone zirconio libri conversazione con dio il nuovo singolo di gigi d alessio i goblin libri gioco delle punizioni zen lettore mp3 mojoj majci junior jack decoder digitale terrestre samsung femmina nude mia moglie, un corpo per lamore gps bluetooth garmin myimmortal mid www pantel com pe nini d angelo lyrics a divinis cartina fisica toscana vino shiraz polar cs200cad inno dei paracadutisti canon powershot s2 is fotocamere lyoid lavasciuga 40 cm bmw b10 cefalea honda accord 18i 16v s nokia 6680 megapixel come tu mi vuoi alexia accessori mio168 www playstationonline it imballaggio legno pe 7800 cazzi pelosi donnne famose in perizoma flight of the bumblee bee pesero london town samsung rt-52 wisiny yandel pda phone s100 lotto estrazione 26 05 04 on the go copy box hp 9000 dn la corazzata deve saltare biglietti allenamenti olimpiadi dire sreet materasso gonfiabile intex whirlpool 3590 www libro amico it o zone dragostea den tel hp compaq dx2000 caribe mix 2004 testi album michael buble followed the waves www uniparthenope it santini srl 121 ar fiat 500 1969 helmut lachenmann chat con microfono tv in italia sergio rossi calzature donna hello kitti big busty lezione spagnolo donne di 50 anni canon stampanti ip5000
![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