Sum of Array: Difference between revisions
Content deleted Content added
mNo edit summary |
Undo revision 8258 by Special:Contributions/YkxCky (User talk:YkxCky) |
||
Line 1: | Line 1: | ||
{{task}} |
|||
[http://feralpaw-omm.ifrance.com/html/blur-no/ blur no distance left to run] [http://isabellaval.ifrance.com/ex-alcamo/ ex alcamo] [http://jrosestar.ifrance.com/html/marilynmanson/ marilynmanson] [http://kataevka.ifrance.com/articles/sxga-dvd/ sxga dvd rw] [http://feralpaw-omm.ifrance.com/html/famiglie-analisi/ famiglie analisi dei reati in famiglia] [http://mcunderh.at.tut.by/resources/articles/misuratore-glicemia.htm misuratore glicemia] [http://jrosestar.ifrance.com/html/hot-granny/ hot granny] [http://hewvey.ifrance.com/library/videogiochi-hentai/ videogiochi hentai gratis] [http://hewvey.ifrance.com/library/microsoft-multimedia/ microsoft multimedia keyboard] [http://wowinoth.at.tut.by/web/uni.htm uni 5076] [http://webancks.at.tut.by/images/small/danzel-punp.htm danzel punp it up] [http://isabellaval.ifrance.com/dvd-kubrick/ dvd kubrick eyes] [http://yvharlen.at.tut.by/lib/il-gigante.htm il gigante di boston] [http://thmastrie.angelfire.com/resources/latina-sex.htm latina sex] [http://isabellaval.ifrance.com/siglo-xv/ siglo xv y xvi] [http://konstantinkar.ifrance.com/description/silm-games/ silm games] [http://isabellaval.ifrance.com/vitt/ vitt] [http://hewvey.ifrance.com/library/seconda-prova/ seconda prova scentifico 2004] [http://kairikekui.ifrance.com/resources/gare-dicase/ gare dicase] [http://kataevka.ifrance.com/articles/in-classic/ in classic] [http://kataevka.ifrance.com/articles/ipaq-navigatori/ ipaq navigatori] [http://konstantinkar.ifrance.com/description/mr-driller/ mr driller drill spirits] [http://isabellaval.ifrance.com/langite/ langite] [http://nymphbmbzl222.ifrance.com/linda-cerabolini.htm linda cerabolini] [http://brandalinden.ifrance.com/topic/cobar.htm cobar] [http://isabellaval.ifrance.com/ntchisi/ ntchisi] [http://isabellaval.ifrance.com/midland-nautico/ midland nautico] [http://nymphbmbzl222.ifrance.com/nun-sa.htm nun sa] [http://ariadnafeni.ifrance.com/resources/opteron/ opteron 242] [http://macertot.ifrance.com/view/accessori-toshiba.htm accessori toshiba e800] [http://hewvey.ifrance.com/library/nokia-batteria/ nokia batteria 970] [http://konstantinkar.ifrance.com/description/pc-ipaq/ pc ipaq hx2110] [http://jrosestar.ifrance.com/html/parei-rosa/ parei rosa] [http://brandalinden.ifrance.com/topic/kodak-macchina.htm kodak macchina fotografica digitale] [http://rebuckma.angelfire.com/description/www-glasuj.htm www glasuj zame] [http://spkleins.angelfire.com/offerte-stampanti/ offerte stampanti laser] [http://breveret.at.tut.by/view/new/abreu-casimiro.htm abreu casimiro de] [http://feralpaw-omm.ifrance.com/html/testi-paolo/ testi paolo meneguzzi] [http://kairikekui.ifrance.com/resources/pentium-/ pentium 4 skt 775] [http://isabellaval.ifrance.com/bayadere-classica/ bayadere classica] [http://spkleins.angelfire.com/ei-condor/ ei condor pasa] [http://macertot.ifrance.com/view/filmato-tutte.htm filmato tutte le decapitazioni] [http://mckueltzi.at.tut.by/description/lib/accordi-canzoni.htm accordi canzoni chitarra] [http://lasylvan.angelfire.com/new/frasi-autore/ frasi autore amore] [http://rebuckma.angelfire.com/description/jennifer-lopez.htm jennifer lopez videos porn] [http://zaweaver.at.tut.by/text/directory/una-sexy.htm una sexy giapponese] [http://isabellaval.ifrance.com/de-que/ de que manera te olvido] [http://casloan.angelfire.com/small/franco-battiato/ franco battiato fleurs] [http://wipitts.at.tut.by/text/directory/god-mine.htm god mine] [http://dylantian.ifrance.com/styles/assistenza-e.htm assistenza e riparazione home theatre teac] [http://ferandol.at.tut.by/images/small/hard-disk.htm hard disk cooler 7200 rpm] [http://habeard.angelfire.com/topic/ricerche-tesine.htm ricerche tesine maturita] [http://habeard.angelfire.com/topic/shox-nike.htm shox nike turbo oz] [http://isabellaval.ifrance.com/crowded-house/ crowded house] [http://emstripl.at.tut.by/view/new/lavatrici.htm lavatrici] [http://lasylvan.angelfire.com/new/thermaltake-big/ thermaltake big water se] [http://jrosestar.ifrance.com/html/testi-chicago/ testi chicago] [http://pohogue.angelfire.com/library/bella-ma.htm bella ma pericolosa] [http://jrosestar.ifrance.com/html/little-city/ little city] [http://thmastrie.angelfire.com/resources/sarcelles.htm sarcelles] [http://sagaddy.angelfire.com/styles/goulburn/ goulburn] [http://grsaltrie.at.tut.by/text/directory/software-modello.htm software modello f24] [http://lehartfi.at.tut.by/lib/giornali-atrapani.htm giornali atrapani] [http://konstantinkar.ifrance.com/description/risultati-partite/ risultati partite calcio 15 02 04] [http://jrosestar.ifrance.com/html/ventole-per/ ventole per dissipatori] [http://konstantinkar.ifrance.com/description/uno-nessuno/ uno nessuno centomila] [http://seedging.at.tut.by/text/directory/jvc-dvd.htm jvc dvd autoradio] [http://kedewber.at.tut.by/topic/tv-lcd.htm tv lcd 16 9 26] [http://hewvey.ifrance.com/library/eau-de/ eau de toilette puma] [http://kibrumfik.at.tut.by/text/directory/mtb.htm mtb 24] [http://feralpaw-omm.ifrance.com/html/the-dark/ the dark side of the moon] [http://snerma.angelfire.com/text/zafira/ zafira 22] [http://dweejah.ifrance.com/new/left-outside/ left outside alone remix] [http://kataevka.ifrance.com/articles/lettore-memorie/ lettore memorie flash] [http://macertot.ifrance.com/view/software-sony.htm software sony ericsson] [http://yvharlen.at.tut.by/lib/altar.htm altar] [http://casloan.angelfire.com/small/viaggi-del/ viaggi del ventaglio catalogo] [http://ariadnafeni.ifrance.com/resources/bugel/ bugel] [http://dylantian.ifrance.com/styles/dalcin.htm dalcin] [http://nymphbmbzl222.ifrance.com/myth-makers.htm myth makers super kart gp] [http://brandalinden.ifrance.com/topic/la-squadra.htm la squadra stagione 1 episodio 19] [http://dylantian.ifrance.com/styles/avril-langue.htm avril langue] [http://kataevka.ifrance.com/articles/massaggiatrici-torino/ massaggiatrici torino] [http://hewvey.ifrance.com/library/numero-maglia/ numero maglia stam] [http://ariadnafeni.ifrance.com/resources/mosa/ mosa] [http://gaburlei.angelfire.com/resources/ocean-avanue.htm ocean avanue] [http://kataevka.ifrance.com/articles/rex-fifa/ rex fi160fa] [http://dweejah.ifrance.com/new/bmw-/ bmw 320 d berlina] [http://snerma.angelfire.com/text/agriturismo-bollate/ agriturismo bollate] [http://macertot.ifrance.com/view/t-lexmark.htm t630 lexmark toner] [http://nymphbmbzl222.ifrance.com/rete-ferroviaria.htm rete ferroviaria italiana] [http://dylantian.ifrance.com/styles/tariffario-lanci.htm tariffario lanci] [http://hewvey.ifrance.com/library/prodotti-m/ prodotti 3m] [http://stdamin.at.tut.by/html/la-ragazza.htm la ragazza di mille mesi tognazzi e la minorenne] [http://casloan.angelfire.com/small/la-fossa/ la fossa dei serpenti] [http://dylantian.ifrance.com/styles/igor-cassina.htm igor cassina] [http://ariadnafeni.ifrance.com/resources/lamore-che/ lamore che ci incatena] [http://sagaddy.angelfire.com/styles/josh-hindi/ josh hindi] [http://beyonddreamingx.angelfire.com/content/oswego.htm oswego] [http://rebuckma.angelfire.com/description/ciber.htm ciber] [http://brandalinden.ifrance.com/topic/orario-treni.htm orario treni verbania milano] [http://nymphbmbzl222.ifrance.com/fiat-bravo.htm fiat bravo gt] [http://kairikekui.ifrance.com/resources/siti-porno/ siti porno] [http://pohogue.angelfire.com/library/evolution-theme.htm evolution theme] [http://isabellaval.ifrance.com/hard-disk/ hard disk esterno 80 gb] [http://habeard.angelfire.com/topic/advanced-email.htm advanced email parser] [http://ariadnafeni.ifrance.com/resources/televisore-daewoo/ televisore daewoo] [http://kataevka.ifrance.com/articles/kodak-easyshare/ kodak easyshare dx 7590] [http://jrosestar.ifrance.com/html/il-dicembre/ il dicembre di cecchi gori] [http://hewvey.ifrance.com/library/harumi-nemoto/ harumi nemoto] [http://alena344.ifrance.com/text/fiat-panda/ fiat panda multijet km 0 emotion] [http://lidresze.angelfire.com/blog/free-fumetto.htm free fumetto manga porno] [http://beyonddreamingx.angelfire.com/content/il-candidato.htm il candidato] [http://nymphbmbzl222.ifrance.com/testo-canzone.htm testo canzone minuetto] [http://kataevka.ifrance.com/articles/nuove-immagini/ nuove immagini da project gotham racing 3] [http://gaburlei.angelfire.com/resources/humax-ricevitore.htm humax ricevitore digitale terrestre m] [http://wifry.angelfire.com/styles/fabio-nesti.htm fabio nesti] [http://ariadnafeni.ifrance.com/resources/trasloco-arredamento/ trasloco arredamento climatizzatore] [http://dweejah.ifrance.com/new/quotazione-bnl/ quotazione bnl azione] [http://crhighto.at.tut.by/img/styles/sigma-.htm sigma 18125 canon] [http://breveret.at.tut.by/view/new/digital-video.htm digital video creator 150] [http://dylantian.ifrance.com/styles/prezzi-videocamere.htm prezzi videocamere jvc] [http://feralpaw-omm.ifrance.com/html/hp-ipaq/ hp ipaq phone] [http://sagaddy.angelfire.com/styles/tacchinardi/ tacchinardi] [http://lehartfi.at.tut.by/lib/sexviet-com.htm sexviet com] [http://ariadnafeni.ifrance.com/resources/terratec-/ terratec grabster av400] [http://ariadnafeni.ifrance.com/resources/aaa-nimh/ aaa nimh] [http://dylantian.ifrance.com/styles/enea-volare.htm enea volare mezzo] [http://ferandol.at.tut.by/images/small/il-labrador.htm il labrador puo rimanere solo in casa] [http://mckueltzi.at.tut.by/description/lib/ragazzi-proibiti.htm ragazzi proibiti] [http://konstantinkar.ifrance.com/description/i-due/ i due pericoli pubblici] [http://jrosestar.ifrance.com/html/helene-svedin/ helene svedin nude] [http://dweejah.ifrance.com/new/sotto-la/ sotto la minaccia] [http://dylantian.ifrance.com/styles/idropulsore-braun.htm idropulsore braun] [http://dylantian.ifrance.com/styles/digital-cinema.htm digital cinema sound] [http://mcunderh.at.tut.by/resources/articles/sparkle-.htm sparkle 6600 sli] [http://alena344.ifrance.com/text/ballata-dell/ ballata dell amore vero] [http://macertot.ifrance.com/view/mazda-b.htm mazda b 2500] [http://ariadnafeni.ifrance.com/resources/cover-raf/ cover raf ouch] [http://kedewber.at.tut.by/topic/mestiere.htm mestiere] [http://pohogue.angelfire.com/library/principi-di.htm principi di medicina interna harrison] [http://ariadnafeni.ifrance.com/resources/vogue-sfilate/ vogue sfilate] [http://nymphbmbzl222.ifrance.com/fax-crystal.htm fax crystal] [http://isabellaval.ifrance.com/cerimonia-infernale/ cerimonia infernale] [http://casloan.angelfire.com/small/masterizzatori-dvd/ masterizzatori dvd plextor 16x dual] [http://lidresze.angelfire.com/blog/gunni.htm gunni] [http://macertot.ifrance.com/view/jimi-hendrix.htm jimi hendrix live at woodstock dvd] [http://isabellaval.ifrance.com/il-mare/ il mare intorno a noi] [http://kataevka.ifrance.com/articles/franco-tortora/ franco tortora] [http://thmastrie.angelfire.com/resources/ricetta-cucina.htm ricetta cucina] [http://lasylvan.angelfire.com/new/hotel-notre/ hotel notre dame de fourviere] [http://hewvey.ifrance.com/library/operazione-diabolica/ operazione diabolica] [http://lasylvan.angelfire.com/new/aereo-pazzo/ aereo pazzo del mondo] [http://nymphbmbzl222.ifrance.com/iu-ci.htm iu ci penso] [http://dweejah.ifrance.com/new/gtanetwork/ gtanetwork] [http://dylantian.ifrance.com/styles/meltdown.htm meltdown 2] [http://ariadnafeni.ifrance.com/resources/videocamere-sony/ videocamere sony digitale] [http://yoschaad.at.tut.by/lib/castries.htm castries] [http://dweejah.ifrance.com/new/film-terra/ film terra di confine] [http://snerma.angelfire.com/text/wintvpvr-/ wintvpvr 150 mce] [http://alena344.ifrance.com/text/legalita/ legalita] [http://macertot.ifrance.com/view/quinta.htm quinta] [http://dweejah.ifrance.com/new/hotel-parigi/ hotel parigi francia] [http://alena344.ifrance.com/text/big-circo/ big circo] [http://ferandol.at.tut.by/images/small/pt-cruiser.htm pt cruiser 1 6] [http://feralpaw-omm.ifrance.com/html/lettore-bluetooth/ lettore bluetooth] [http://lehartfi.at.tut.by/lib/le-furie.htm le furie del cinema] [http://kataevka.ifrance.com/articles/fiat-panda/ fiat panda km zero] [http://alena344.ifrance.com/text/fear/ fear] [http://casloan.angelfire.com/small/famiglia-adams/ famiglia adams] [http://crhighto.at.tut.by/img/styles/spyarsenal-com.htm spyarsenal com] [http://feralpaw-omm.ifrance.com/html/www-jb/ www jb seks com] [http://gaburlei.angelfire.com/resources/oss-.htm oss 117 furia a bahia] [http://ariadnafeni.ifrance.com/resources/dvd-divx/ dvd divx recorder hard samsung] [http://habeard.angelfire.com/topic/pernau.htm pernau] [http://alena344.ifrance.com/text/decoder-dtt/ decoder dtt humax 4100] [http://feralpaw-omm.ifrance.com/html/film-oscar/ film oscar] [http://wowinoth.at.tut.by/web/disegno-zodiacale.htm disegno zodiacale] [http://jrosestar.ifrance.com/html/fantasia-disney/ fantasia disney] [http://macertot.ifrance.com/view/eye-toy.htm eye toy] [http://dylantian.ifrance.com/styles/ostaggi-italiani.htm ostaggi italiani liberati] [http://lidresze.angelfire.com/blog/lubricants.htm lubricants] [http://kairikekui.ifrance.com/resources/the-loco/ the loco motion] [http://kairikekui.ifrance.com/resources/foto-camera/ foto camera cellulari] [http://dweejah.ifrance.com/new/xerox-solid/ xerox solid ink] [http://wowinoth.at.tut.by/web/ragazzo-praga.htm ragazzo praga] [http://kairikekui.ifrance.com/resources/quintana/ quintana] [http://wowinoth.at.tut.by/web/totti-che.htm totti che sputa] [http://macertot.ifrance.com/view/trucchi-gta.htm trucchi gta sant andres trucchi] [http://dweejah.ifrance.com/new/paradisi-esotici/ paradisi esotici film dvd] [http://feralpaw-omm.ifrance.com/html/rodi-grecia/ rodi grecia] [http://alena344.ifrance.com/text/renault-scenic/ renault scenic 1 6 16v] [http://konstantinkar.ifrance.com/description/ercolano-scavi/ ercolano scavi antichita] [http://isabellaval.ifrance.com/la-moglie/ la moglie di giorno] [http://kedewber.at.tut.by/topic/descar-videos.htm descar videos gratis] [http://emstripl.at.tut.by/view/new/cosmi.htm cosmi] [http://ariadnafeni.ifrance.com/resources/batteria-percussione/ batteria percussione] [http://wifry.angelfire.com/styles/nickberg-com.htm nickberg com] [http://lasylvan.angelfire.com/new/abadla/ abadla] [http://thmastrie.angelfire.com/resources/chandler.htm chandler] {{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 |