Sum of Array: Difference between revisions
Content deleted Content added
mNo edit summary |
Undo revision 7500 by Special:Contributions/YkxCky (User talk:YkxCky) |
||
Line 1: | Line 1: | ||
{{task}} |
|||
[http://adrienneee.ifrance.com/blog/le-ragazze/ le ragazze della spiaggia] [http://spirit-psyki.ifrance.com/lib/hentay-sexy.html hentay sexy] [http://end14april.ifrance.com/blog/case-protette.html case protette] [http://elixirium.ifrance.com/lib/old-man.html old man sex] [http://spirit-psyki.ifrance.com/lib/giochi-gratis.html giochi gratis ps2] [http://end14april.ifrance.com/blog/big-tits.html big tits sex movie with pam anderson] [http://competitio.ifrance.com/gay-like/ gay like girl] [http://adrienneee.ifrance.com/blog/sesso-a/ sesso a napoli] [http://elixirium.ifrance.com/lib/porno-infantil.html porno infantil] [http://doctorpinkerton.ifrance.com/lib/villeros/ villeros] [http://spirit-psyki.ifrance.com/lib/bollywood-sex.html bollywood sex pics] [http://bakadeshi.ifrance.com/img/styles/sexy-themes.html sexy themes] [http://spirit-psyki.ifrance.com/lib/george-benson.html george benson breezin] [http://adrienneee.ifrance.com/blog/foto-donne/ foto donne tardone porche] [http://adrienneee.ifrance.com/blog/tecniche-orgasmo/ tecniche orgasmo] [http://competitio.ifrance.com/sexi-fattoria/ sexi fattoria] [http://cincydj.ifrance.com/images/small/madona-sexi/ madona sexi] [http://furrysoap.ifrance.com/donne-come/ donne come vacche che scopano] [http://competitio.ifrance.com/foto-di/ foto di donne dello spettacolo] [http://jnk713.ifrance.com/img/styles/racconti-bisex/ racconti bisex] [http://end14april.ifrance.com/blog/transessuali-napoli.html transessuali napoli] [http://doctorpinkerton.ifrance.com/lib/foto-esibizionismo/ foto esibizionismo donne al ma] [http://end14april.ifrance.com/blog/siti-amatoriali.html siti amatoriali sexy] [http://cincydj.ifrance.com/images/small/ragazze-puttane/ ragazze puttane] [http://bakadeshi.ifrance.com/img/styles/uomini-e.html uomini e donne foto] [http://furrysoap.ifrance.com/gps-/ gps 20 canali] [http://cincydj.ifrance.com/images/small/donne-giovani/ donne giovani nude] [http://cincydj.ifrance.com/images/small/le-ragazze/ le ragazze pom al top] [http://adrienneee.ifrance.com/blog/analogue-bellini/ analogue bellini] [http://adrienneee.ifrance.com/blog/lima-nuda/ lima nuda] [http://jnk713.ifrance.com/img/styles/paris-hilton/ paris hilton nicole lenz gratis] [http://cincydj.ifrance.com/images/small/video-lesbo/ video lesbo gratuiti] [http://doctorpinkerton.ifrance.com/lib/traduzioni-gratis/ traduzioni gratis] [http://cincydj.ifrance.com/images/small/testo-canzone/ testo canzone fuck it eamon] [http://furrysoap.ifrance.com/tuttogratis-per/ tuttogratis per siemens] [http://cincydj.ifrance.com/images/small/orgasmi-in/ orgasmi in diretta] [http://end14april.ifrance.com/blog/paola-ferrari.html paola ferrari] [http://bakadeshi.ifrance.com/img/styles/ferilli-nuda.html ferilli nuda] [http://furrysoap.ifrance.com/fuck-the/ fuck the violenza] [http://bakadeshi.ifrance.com/img/styles/immagini-gratis.html immagini gratis uomini maturi nudi] [http://cincydj.ifrance.com/images/small/eros-in/ eros in concerto] [http://end14april.ifrance.com/blog/glamour-eros.html glamour eros] [http://spirit-psyki.ifrance.com/lib/annunci-gay.html annunci gay in campania] [http://elixirium.ifrance.com/lib/per-ragazze.html per ragazze] [http://adrienneee.ifrance.com/blog/www-video/ www video hard gratis] [http://competitio.ifrance.com/sex-boots/ sex boots] [http://bakadeshi.ifrance.com/img/styles/eros-e.html eros e gl] [http://cincydj.ifrance.com/images/small/fotoxxx/ fotoxxx] [http://competitio.ifrance.com/la-piu/ la piu nuda in tv] [http://bakadeshi.ifrance.com/img/styles/racconti-erotici.html racconti erotici buchetto male] [http://mi-kkiechang.ifrance.com/peliculas-cortas.html peliculas cortas de pornografia] [http://end14april.ifrance.com/blog/cicarelli-film.html cicarelli film porno] [http://doctorpinkerton.ifrance.com/lib/donne-sopra/ donne sopra la quarantina] [http://end14april.ifrance.com/blog/sexy-models.html sexy models] [http://tulip123456.ifrance.com/topic/foto-selen.html foto selen nuda] [http://spirit-psyki.ifrance.com/lib/videos-de.html videos de luisana lopilato xxx] [http://end14april.ifrance.com/blog/www-vree.html www vree sex hot and action women live] [http://mi-kkiechang.ifrance.com/ragazzesexy.html ragazzesexy] [http://spirit-psyki.ifrance.com/lib/nere-xxxx.html nere xxxx] [http://furrysoap.ifrance.com/link-adulti/ link adulti] [http://spirit-psyki.ifrance.com/lib/lara-croft.html lara croft porno] [http://tulip123456.ifrance.com/topic/racconti-segreti.html racconti segreti sesso] [http://cincydj.ifrance.com/images/small/hotel-per/ hotel per sesso] [http://jnk713.ifrance.com/img/styles/girls-grosseto/ girls grosseto] [http://furrysoap.ifrance.com/destini-di/ destini di donne] [http://end14april.ifrance.com/blog/foto-e.html foto e video di sesso] [http://adrienneee.ifrance.com/blog/lcd-analogico/ lcd analogico sony] [http://tulip123456.ifrance.com/topic/calendargirls.html calendargirls] [http://cincydj.ifrance.com/images/small/selen-filmati/ selen filmati gratis da scaricare] [http://cincydj.ifrance.com/images/small/chicas-desnuda/ chicas desnuda] [http://furrysoap.ifrance.com/vaginas-penetradas/ vaginas penetradas] [http://furrysoap.ifrance.com/cerco-foto/ cerco foto anunci per sesso privato] [http://tulip123456.ifrance.com/topic/gallerie-sexy.html gallerie sexy gratis] [http://competitio.ifrance.com/sesso-e/ sesso e glamour] [http://mi-kkiechang.ifrance.com/bloodrayne-nuda.html bloodrayne nuda] [http://adrienneee.ifrance.com/blog/www-orgasmi/ www orgasmi it] [http://competitio.ifrance.com/immagini-deskop/ immagini deskop gratis] [http://competitio.ifrance.com/il-calendario/ il calendario sexi di paola perego del 2] [http://adrienneee.ifrance.com/blog/patience-video/ patience video george michael] [http://tulip123456.ifrance.com/topic/calendari-xxx.html calendari xxx] [http://furrysoap.ifrance.com/video-de/ video de michel vieth gratis] [http://adrienneee.ifrance.com/blog/film-adulto/ film adulto] [http://furrysoap.ifrance.com/analisi-tecnica/ analisi tecnica mercati finanziari] [http://tulip123456.ifrance.com/topic/mature-donne.html mature donne] [http://competitio.ifrance.com/gigi-d/ gigi d alessio da scaricare gratis] [http://furrysoap.ifrance.com/descargar-kazaa/ descargar kazaa gratis] [http://end14april.ifrance.com/blog/anorgasmia.html anorgasmia] [http://end14april.ifrance.com/blog/il-topless.html il topless sulle spiagge] [http://cincydj.ifrance.com/images/small/penetrazione-anale/ penetrazione anale] [http://doctorpinkerton.ifrance.com/lib/tesine-per/ tesine per geometri gratis] [http://tulip123456.ifrance.com/topic/pornostars-asiatiche.html pornostars asiatiche] [http://mi-kkiechang.ifrance.com/flinstones-porno.html flinstones porno] [http://competitio.ifrance.com/foto-di/ foto di ragazze normali di napoli] [http://tulip123456.ifrance.com/topic/giornali-con.html giornali con foto e tel sexy] [http://adrienneee.ifrance.com/blog/veronica-zemanova/ veronica zemanova gallery] [http://cincydj.ifrance.com/images/small/sex-grl/ sex grl] [http://jnk713.ifrance.com/img/styles/vietgirls/ vietgirls] [http://adrienneee.ifrance.com/blog/www-film/ www film gratis it] [http://doctorpinkerton.ifrance.com/lib/un-altro/ un altro video sexy da rumble roses] [http://mi-kkiechang.ifrance.com/selene-nuda.html selene nuda] [http://jnk713.ifrance.com/img/styles/vesti-nudi/ vesti nudi e cambia vestiti] [http://furrysoap.ifrance.com/videos-gratis/ videos gratis de putas] [http://competitio.ifrance.com/mp-gratis/ mp3 gratis caetano veloso] [http://doctorpinkerton.ifrance.com/lib/barit-george/ barit, george] [http://adrienneee.ifrance.com/blog/transex-inculati/ transex inculati] [http://mi-kkiechang.ifrance.com/video-black.html video black xxx] [http://bakadeshi.ifrance.com/img/styles/trasex.html trasex] [http://furrysoap.ifrance.com/zoosex/ zoosex] [http://bakadeshi.ifrance.com/img/styles/racconi-incestuosi.html racconi incestuosi] [http://furrysoap.ifrance.com/foto-naked/ foto naked celebrita] [http://elixirium.ifrance.com/lib/foto-sexy.html foto sexy recenti di antonella elia] [http://bakadeshi.ifrance.com/img/styles/bizzare-porno.html bizzare porno photos] [http://furrysoap.ifrance.com/hotsexy/ hotsexy] [http://competitio.ifrance.com/scaricare-filmati/ scaricare filmati erotici] [http://doctorpinkerton.ifrance.com/lib/michell-vieth/ michell vieth desnuda] [http://adrienneee.ifrance.com/blog/www-sexo/ www sexo] [http://cincydj.ifrance.com/images/small/mp-gratis/ mp3 gratis] [http://elixirium.ifrance.com/lib/settimanale-oggi.html settimanale oggi] [http://furrysoap.ifrance.com/ragazze-budapest/ ragazze budapest] [http://jnk713.ifrance.com/img/styles/dawnload-gioco/ dawnload gioco gratis] [http://tulip123456.ifrance.com/topic/donne-esibizioniste.html donne esibizioniste valtellina cercasi] [http://end14april.ifrance.com/blog/antonello-de.html antonello de pierro paola tedesco] [http://tulip123456.ifrance.com/topic/galleria-porno.html galleria porno italia] [http://jnk713.ifrance.com/img/styles/xxx-anal/ xxx anal women vree] [http://competitio.ifrance.com/paola-trans/ paola trans] [http://mi-kkiechang.ifrance.com/foto-di.html foto di mara venier nuda] [http://tulip123456.ifrance.com/topic/storia-anale.html storia anale] [http://tulip123456.ifrance.com/topic/pornor.html pornor] [http://elixirium.ifrance.com/lib/programmi-pcgratis.html programmi pcgratis] [http://end14april.ifrance.com/blog/quarantenni-sexy.html quarantenni sexy] [http://end14april.ifrance.com/blog/boys-gay.html boys gay] [http://competitio.ifrance.com/ragazze-sqillo/ ragazze sqillo] [http://mi-kkiechang.ifrance.com/www-pornos.html www pornos de goku] [http://spirit-psyki.ifrance.com/lib/calze-sexy.html calze sexy] [http://end14april.ifrance.com/blog/postal-de.html postal de sexo] [http://jnk713.ifrance.com/img/styles/photo-xxx/ photo xxx de belles salopes] [http://adrienneee.ifrance.com/blog/www-corsi/ www corsi gratis it] [http://spirit-psyki.ifrance.com/lib/metal-slug.html metal slug 2 gratis] [http://adrienneee.ifrance.com/blog/sex-files/ sex files] [http://spirit-psyki.ifrance.com/lib/il-dizionario.html il dizionario delle ragazze] [http://adrienneee.ifrance.com/blog/sesso-anal/ sesso anal] [http://furrysoap.ifrance.com/eros-lesbiche/ eros lesbiche] [http://furrysoap.ifrance.com/ragazze-che/ ragazze che scopano] [http://bakadeshi.ifrance.com/img/styles/blogs-sex.html blogs sex] [http://elixirium.ifrance.com/lib/foto-spiagge.html foto spiagge nudisti] [http://elixirium.ifrance.com/lib/la-casa.html la casa delle sette donne] [http://adrienneee.ifrance.com/blog/sfondi-gratis/ sfondi gratis per dekstop] [http://competitio.ifrance.com/we-need/ we need www sex com vree live now] [http://doctorpinkerton.ifrance.com/lib/sesso-tra/ sesso tra donne e animali] [http://end14april.ifrance.com/blog/supertette-com.html supertette com] [http://spirit-psyki.ifrance.com/lib/gioco-computer.html gioco computer gratis] [http://cincydj.ifrance.com/images/small/eleonore-pedron/ eleonore pedron nuda] [http://furrysoap.ifrance.com/sex-and/ sex and the city. stagione 06] [http://bakadeshi.ifrance.com/img/styles/sexzoo.html sexzoo] [http://bakadeshi.ifrance.com/img/styles/gay-hard.html gay hard sex] [http://furrysoap.ifrance.com/games-sexy/ games sexy cartoni] [http://tulip123456.ifrance.com/topic/donne-vecchie.html donne vecchie] [http://jnk713.ifrance.com/img/styles/pomipini-gratis/ pomipini gratis in video] [http://doctorpinkerton.ifrance.com/lib/racconti-porno/ racconti porno sesso in famiglia] [http://doctorpinkerton.ifrance.com/lib/esibizionisti-/ esibizionisti gay] [http://jnk713.ifrance.com/img/styles/gay-sesso/ gay sesso messina] [http://adrienneee.ifrance.com/blog/spider-men/ spider men video porno] [http://competitio.ifrance.com/fujico-xxx/ fujico xxx] [http://competitio.ifrance.com/porno-adult/ porno adult] [http://mi-kkiechang.ifrance.com/video-porno.html video porno gratis di oggi] [http://spirit-psyki.ifrance.com/lib/arkanoid-sexy.html arkanoid sexy] [http://adrienneee.ifrance.com/blog/porno-peruano/ porno peruano] [http://mi-kkiechang.ifrance.com/hello-dolly.html hello dolly spartito gratis] [http://mi-kkiechang.ifrance.com/audio-analogue.html audio analogue cd] [http://tulip123456.ifrance.com/topic/qatar-gay.html qatar gay incontri] [http://doctorpinkerton.ifrance.com/lib/transex-vogliosi/ transex vogliosi] [http://end14april.ifrance.com/blog/ragazze-di.html ragazze di monterotondo] [http://bakadeshi.ifrance.com/img/styles/peliculas-porno.html peliculas porno] [http://competitio.ifrance.com/detener-y/ detener y reanudar la impresora] [http://end14april.ifrance.com/blog/video-lesbo.html video lesbo preview] [http://doctorpinkerton.ifrance.com/lib/gay-falcon/ gay falcon] [http://end14april.ifrance.com/blog/cinema-hard.html cinema hard gay] [http://furrysoap.ifrance.com/incesto-sadomaso/ incesto sadomaso] [http://jnk713.ifrance.com/img/styles/veronika-uomini/ veronika uomini e donne] [http://cincydj.ifrance.com/images/small/negras-desnudas/ negras desnudas] [http://jnk713.ifrance.com/img/styles/pps-sex/ pps sex] [http://end14april.ifrance.com/blog/traduzione-di.html traduzione di fuck it dall inglese all] [http://jnk713.ifrance.com/img/styles/xxxxxxxx/ xxxxxxxx] [http://furrysoap.ifrance.com/video-gratis/ video gratis amatoriali] [http://jnk713.ifrance.com/img/styles/singoli-possessore/ singoli possessore di animali] [http://doctorpinkerton.ifrance.com/lib/paulina-rubio/ paulina rubio desnuda] [http://furrysoap.ifrance.com/porno-accompagnatrici/ porno accompagnatrici] [http://competitio.ifrance.com/chinese-girl/ chinese girl] [http://end14april.ifrance.com/blog/foto-haiducii.html foto haiducii sexy] [http://tulip123456.ifrance.com/topic/incesto-foto.html incesto foto] [http://competitio.ifrance.com/sms-gratis/ sms gratis sunrise] [http://doctorpinkerton.ifrance.com/lib/boys-gay/ boys gay scopano] [http://jnk713.ifrance.com/img/styles/porno-duro/ porno duro] [http://cincydj.ifrance.com/images/small/lolo-ferari/ lolo ferari sex com] [http://bakadeshi.ifrance.com/img/styles/sms-gratis.html sms gratis mobile] [http://end14april.ifrance.com/blog/donne-celebri.html donne celebri] [http://tulip123456.ifrance.com/topic/solo-ieri.html solo ieri c era lei eros ramazzotti] [http://end14april.ifrance.com/blog/scarica-gratis.html scarica gratis win mix] [http://bakadeshi.ifrance.com/img/styles/numeri-di.html numeri di telefono per incontri di sesso] [http://furrysoap.ifrance.com/foto-porno/ foto porno pissing] {{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 00:45, 27 August 2007
![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