Sum of Array: Difference between revisions
Content deleted Content added
mNo edit summary |
revert spam |
||
Line 1: | Line 1: | ||
{{task}} |
|||
[http://highbulp.ifrance.com/images/small/max-scopate/ max scopate] [http://lordsander.ifrance.com/topic/hp-compaq.htm hp compaq dx6100] [http://lovereceier.ifrance.com/lib/usted.htm usted] [http://broadwaylili.ifrance.com/content/view/mar-chiquita.htm mar chiquita] [http://highbulp.ifrance.com/images/small/interfaccia-bluetooth/ interfaccia bluetooth usb] [http://akmokanzen.ifrance.com/images/small/solletico.htm solletico] [http://oiyaoi.ifrance.com/images/small/love-hona/ love hona] [http://highbulp.ifrance.com/images/small/videocamera-mustek/ videocamera mustek 5000] [http://lordsander.ifrance.com/topic/finale-a.htm finale a valvole] [http://toofarfrommaybe.ifrance.com/view/new/hp-laserjet/ hp laserjet 3020 toner] [http://igaros.ifrance.com/lib/corpo-foresta/ corpo foresta] [http://highbulp.ifrance.com/images/small/lcd-/ lcd 10 pollici] [http://tat-ooin.ifrance.com/resources/articles/volantini.htm volantini] [http://deviantrus.ifrance.com/topic/circuiti-moto/ circuiti moto gp] [http://noxuhax.ifrance.com/content/view/la-guerra.htm la guerra dei nibelunghi] [http://singaporepets.ifrance.com/library/html/mi-manca.htm mi manca] [http://igaros.ifrance.com/lib/i-pod/ i pod mp3 apple] [http://demurediablo.ifrance.com/library/html/dvd-combo/ dvd combo vhs divx] [http://tat-ooin.ifrance.com/resources/articles/biglietto-visita.htm biglietto visita gratis] [http://singaporepets.ifrance.com/library/html/ibm-thinkpad.htm ibm thinkpad computers] [http://lovereceier.ifrance.com/lib/maddnes.htm maddnes] [http://igaros.ifrance.com/lib/gallerie-porno/ gallerie porno gratuite] [http://kalian42.ifrance.com/text/directory/signora-di/ signora di giorno amante di notte] [http://singaporepets.ifrance.com/library/html/nuovi-cellulari.htm nuovi cellulari nero] [http://noxuhax.ifrance.com/content/view/acryllium.htm acryllium] [http://broadwaylili.ifrance.com/content/view/nokia-.htm nokia 7610 memoria] [http://highbulp.ifrance.com/images/small/vacanza-in/ vacanza in turchia] [http://toofarfrommaybe.ifrance.com/view/new/regione-trentino/ regione trentino alto adige] [http://broadwaylili.ifrance.com/content/view/la-strada.htm la strada dei peccatori] [http://igaros.ifrance.com/lib/donna-mathews/ donna mathews] [http://noxuhax.ifrance.com/content/view/la-scarpetta.htm la scarpetta e la rosa] [http://noxuhax.ifrance.com/content/view/segnalazione-.htm segnalazione - cartelli e insegne] [http://tizolaa.ifrance.com/web/disco-freno/ disco freno accessori auto e moto] [http://jdawsona.ifrance.com/data/gps-per/ gps per palmari qtek] [http://jdawsona.ifrance.com/data/programma-dvd/ programma dvd] [http://yeeden.ifrance.com/img/styles/pelle-ducati/ pelle ducati] [http://yeeden.ifrance.com/img/styles/hard-disk/ hard disk portatile lacie 40] [http://lovereceier.ifrance.com/lib/pigro-midi.htm pigro midi pino daniele] [http://broadwaylili.ifrance.com/content/view/i-palazzi.htm i palazzi di venezia la storia della] [http://highbulp.ifrance.com/images/small/granata-seconda/ granata seconda pelle] [http://kalian42.ifrance.com/text/directory/notebook-p/ notebook p4 3 2] [http://oiyaoi.ifrance.com/images/small/albergo-cortina/ albergo cortina] [http://highbulp.ifrance.com/images/small/motorola-accessori/ motorola accessori] [http://oiyaoi.ifrance.com/images/small/great-teacher/ great teacher onizuka] [http://oiyaoi.ifrance.com/images/small/tesi-automazione/ tesi automazione] [http://speedofsoun.ifrance.com/text/directory/th.htm th 42] [http://akmokanzen.ifrance.com/images/small/honda-accord.htm honda accord 7 serie] [http://singaporepets.ifrance.com/library/html/bosley.htm bosley] [http://igaros.ifrance.com/lib/piatti-termici/ piatti termici] [http://tat-ooin.ifrance.com/resources/articles/etichette-cd.htm etichette cd dvd] [http://singaporepets.ifrance.com/library/html/regole-yo.htm regole yo gi ho] [http://broadwaylili.ifrance.com/content/view/living-in.htm living in a fantasy] [http://noxuhax.ifrance.com/content/view/foto-caccia.htm foto caccia] [http://oiyaoi.ifrance.com/images/small/meyal/ meyal] [http://toofarfrommaybe.ifrance.com/view/new/arigliano/ arigliano] [http://girlwho-is.ifrance.com/img/styles/koganei.htm koganei] [http://yeeden.ifrance.com/img/styles/usb-memory/ usb memory stick] [http://demurediablo.ifrance.com/library/html/suonerie-da/ suonerie da scaricare] [http://yeeden.ifrance.com/img/styles/inno-nationale/ inno nationale] [http://tat-ooin.ifrance.com/resources/articles/chitarra-blues.htm chitarra blues] [http://igaros.ifrance.com/lib/garut/ garut] [http://kalian42.ifrance.com/text/directory/frigoriferi-/ frigoriferi 12 volt compressore] [http://voltia.ifrance.com/content/view/fotocamera-digitale.htm fotocamera digitale canon 350 d] [http://igaros.ifrance.com/lib/taglia-tubi/ taglia tubi red] [http://toofarfrommaybe.ifrance.com/view/new/gratta-e/ gratta e vinci video] [http://singaporepets.ifrance.com/library/html/apple.htm apple 512] [http://demurediablo.ifrance.com/library/html/testo-di/ testo di if aint got you] [http://tizolaa.ifrance.com/web/foto-di/ foto di eva] [http://oiyaoi.ifrance.com/images/small/frigorifero-side/ frigorifero side by] [http://toofarfrommaybe.ifrance.com/view/new/french-kisses/ french kisses] [http://girlwho-is.ifrance.com/img/styles/hifi-lg.htm hi-fi lg] [http://igaros.ifrance.com/lib/www-mitchel/ www mitchel vieth] [http://toofarfrommaybe.ifrance.com/view/new/semisonic/ semisonic] [http://tizolaa.ifrance.com/web/rs-mmc/ rs mmc 1 8] [http://speedofsoun.ifrance.com/text/directory/vanni-rossi.htm vanni rossi] [http://deviantrus.ifrance.com/topic/bcs/ bcs 311] [http://tat-ooin.ifrance.com/resources/articles/albergo-lido.htm albergo lido di camaiore] [http://deviantrus.ifrance.com/topic/z-cellulari/ z200 cellulari sony ericsson] [http://voltia.ifrance.com/content/view/calcio-parma.htm calcio parma] [http://lovereceier.ifrance.com/lib/scheda-gprs.htm scheda gprs compact flash] [http://tat-ooin.ifrance.com/resources/articles/scribner-charles.htm scribner, charles] [http://akmokanzen.ifrance.com/images/small/ford-cayman.htm ford cayman blue] [http://yeeden.ifrance.com/img/styles/c-photo/ c86 photo] [http://toofarfrommaybe.ifrance.com/view/new/loghi-cellulare/ loghi cellulare sony] [http://singaporepets.ifrance.com/library/html/globalsat-pda.htm globalsat pda holder] [http://deviantrus.ifrance.com/topic/tunnig/ tunnig] [http://tat-ooin.ifrance.com/resources/articles/tadap.htm tadap] [http://toofarfrommaybe.ifrance.com/view/new/epson-emp/ epson emp 730] [http://kalian42.ifrance.com/text/directory/domina-coral/ domina coral bay oasis] [http://noxuhax.ifrance.com/content/view/billy-ydol.htm billy ydol] [http://highbulp.ifrance.com/images/small/foto-di/ foto di cazzi piccoli] [http://girlwho-is.ifrance.com/img/styles/chat-ragazze.htm chat ragazze porche] [http://speedofsoun.ifrance.com/text/directory/perla-nera.htm perla nera com] [http://lordsander.ifrance.com/topic/annunci-di.htm annunci di donne troie in cerca di sesso] [http://noxuhax.ifrance.com/content/view/abit-nfm.htm abit nf7-m] [http://girlwho-is.ifrance.com/img/styles/stamping-ground.htm stamping ground] [http://lovereceier.ifrance.com/lib/radio-anni.htm radio anni 30] [http://demurediablo.ifrance.com/library/html/stasera-io/ stasera io vorrei sentir la ninna nanna] [http://lovereceier.ifrance.com/lib/video-amatoriale.htm video amatoriale paris hilton] [http://highbulp.ifrance.com/images/small/terminatore-scsi/ terminatore scsi] [http://toofarfrommaybe.ifrance.com/view/new/tecnici-di/ tecnici di macchine da gioco (regione emilia romagna)] [http://lovereceier.ifrance.com/lib/mesecina.htm mesecina] [http://demurediablo.ifrance.com/library/html/visti-asilo/ visti asilo e immigrazione] [http://lordsander.ifrance.com/topic/gomma-modding.htm gomma modding] [http://jdawsona.ifrance.com/data/frankee-nuda/ frankee nuda] [http://igaros.ifrance.com/lib/bmw-x/ bmw x5 emilia romagna] [http://deviantrus.ifrance.com/topic/www-killer/ www killer pollo] [http://highbulp.ifrance.com/images/small/tomtom-europe/ tomtom europe] [http://voltia.ifrance.com/content/view/backstage-manuela.htm backstage manuela arcuri gratis] [http://speedofsoun.ifrance.com/text/directory/nesta.htm nesta] [http://tat-ooin.ifrance.com/resources/articles/usb.htm usb 250] [http://lordsander.ifrance.com/topic/whittier-john.htm whittier, john greenleaf] [http://tat-ooin.ifrance.com/resources/articles/eurothink-cdtheque.htm eurothink cdtheque] [http://broadwaylili.ifrance.com/content/view/coppie-cercano.htm coppie cercano sigoli] [http://girlwho-is.ifrance.com/img/styles/programma-musica.htm programma musica] [http://jdawsona.ifrance.com/data/tv-/ tv 12 pollici] [http://toofarfrommaybe.ifrance.com/view/new/beat-the/ beat the meter] [http://demurediablo.ifrance.com/library/html/dream-dance/ dream dance vol 30] [http://tizolaa.ifrance.com/web/i-simboli/ i simboli cristiani primitivi] [http://singaporepets.ifrance.com/library/html/forni-micronde.htm forni micronde] [http://jdawsona.ifrance.com/data/gigolo-california/ gigolo california dream] [http://broadwaylili.ifrance.com/content/view/fuji-s.htm fuji s5500] [http://speedofsoun.ifrance.com/text/directory/rubias.htm rubias19] [http://girlwho-is.ifrance.com/img/styles/xg-flash.htm xg flash] [http://kalian42.ifrance.com/text/directory/unito/ unito] [http://yeeden.ifrance.com/img/styles/scuola-canto/ scuola canto] [http://singaporepets.ifrance.com/library/html/wifi-pci.htm wi-fi pci card] [http://igaros.ifrance.com/lib/bikkemberg-uomo/ bikkemberg uomo abbigliamento] [http://tat-ooin.ifrance.com/resources/articles/il-bambino.htm il bambino dinverno] [http://demurediablo.ifrance.com/library/html/mari-gruppo/ mari (gruppo etnico della rus...] [http://noxuhax.ifrance.com/content/view/celo.htm celo-] [http://yeeden.ifrance.com/img/styles/do-doi/ do doi pachi] [http://voltia.ifrance.com/content/view/biografia-su.htm biografia su meneguzzi] [http://highbulp.ifrance.com/images/small/soggiorno-albergo/ soggiorno albergo lisbona] [http://deviantrus.ifrance.com/topic/prada-uomo/ prada uomo cinture] [http://oiyaoi.ifrance.com/images/small/finanziamento-vimercate/ finanziamento vimercate] [http://lordsander.ifrance.com/topic/evangelici.htm evangelici] [http://highbulp.ifrance.com/images/small/renou-louis/ renou, louis] [http://igaros.ifrance.com/lib/y-nos/ y nos dieron las diez] [http://demurediablo.ifrance.com/library/html/supercafone/ supercafone] [http://lovereceier.ifrance.com/lib/gerarchi-si.htm gerarchi si muore] [http://kalian42.ifrance.com/text/directory/sandali-schiava/ sandali schiava] [http://jdawsona.ifrance.com/data/faces-of/ faces of death] [http://noxuhax.ifrance.com/content/view/naughty-girl.htm naughty girl di beyonce] [http://akmokanzen.ifrance.com/images/small/vercana.htm vercana] [http://lovereceier.ifrance.com/lib/barbera-asti.htm barbera asti] [http://singaporepets.ifrance.com/library/html/hotel-best.htm hotel best western aida opera parigi] [http://tat-ooin.ifrance.com/resources/articles/figa-nere.htm figa nere] [http://lovereceier.ifrance.com/lib/karel-y.htm karel y voltio] [http://igaros.ifrance.com/lib/tatanka-dj/ tatanka dj] [http://girlwho-is.ifrance.com/img/styles/samsung-fotocamera.htm samsung fotocamera digimax a6] [http://lordsander.ifrance.com/topic/giugno.htm giugno] [http://lovereceier.ifrance.com/lib/game-boy.htm game boy mario kart] [http://kalian42.ifrance.com/text/directory/mariah-carey/ mariah carey video nuda] [http://broadwaylili.ifrance.com/content/view/musica-moderna.htm musica moderna] [http://toofarfrommaybe.ifrance.com/view/new/steve-wonder/ steve wonder] [http://girlwho-is.ifrance.com/img/styles/sound-blaster.htm sound blaster audigy2 zs notebook] [http://kalian42.ifrance.com/text/directory/confederazione-italiana/ confederazione italiana agricoltori] [http://singaporepets.ifrance.com/library/html/haciendo-miguel.htm haciendo miguel] [http://singaporepets.ifrance.com/library/html/rkelly.htm rkelly] [http://voltia.ifrance.com/content/view/gusi.htm gusi] [http://lovereceier.ifrance.com/lib/betoi.htm betoi] [http://voltia.ifrance.com/content/view/radeon-agp.htm radeon agp 8x] [http://deviantrus.ifrance.com/topic/rampilonga/ rampilonga] [http://girlwho-is.ifrance.com/img/styles/despretine-ozone.htm despretine ozone] [http://lordsander.ifrance.com/topic/la-torre.htm la torre di babele] [http://voltia.ifrance.com/content/view/rs-mmc.htm rs mmc dv 1] [http://singaporepets.ifrance.com/library/html/mille-pazzie.htm mille pazzie] [http://lovereceier.ifrance.com/lib/congelatore-rex.htm congelatore rex incasso] [http://broadwaylili.ifrance.com/content/view/hotel-varazze.htm hotel varazze] [http://highbulp.ifrance.com/images/small/showgirl-nude/ showgirl nude gratis] [http://broadwaylili.ifrance.com/content/view/parafrasi-cinque.htm parafrasi cinque maggio] [http://broadwaylili.ifrance.com/content/view/mappa.htm mappa] [http://demurediablo.ifrance.com/library/html/vasco-un/ vasco un senso] [http://tizolaa.ifrance.com/web/concerto-madonna/ concerto madonna] [http://broadwaylili.ifrance.com/content/view/lecco-tessile.htm lecco tessile] [http://lordsander.ifrance.com/topic/u-make.htm u make me wanna dei blue] [http://deviantrus.ifrance.com/topic/e-sulo/ e sulo niente] [http://tizolaa.ifrance.com/web/video-tv/ video tv flat] [http://jdawsona.ifrance.com/data/giochi-di/ giochi di magie] [http://igaros.ifrance.com/lib/lettori-mp/ lettori mp3 cowon iaudio 4 1 gb] [http://kalian42.ifrance.com/text/directory/maltrato-infantil/ maltrato infantil] [http://lovereceier.ifrance.com/lib/la-epoca.htm la epoca medieval] [http://voltia.ifrance.com/content/view/gaetano.htm gaetano] [http://tizolaa.ifrance.com/web/video-porno/ video porno gratis da scaricare formato] [http://akmokanzen.ifrance.com/images/small/mares-cyrano.htm mares cyrano] [http://kalian42.ifrance.com/text/directory/www-linkami/ www linkami net] [http://yeeden.ifrance.com/img/styles/tesi-di/ tesi di storia moderna e sistema stai eu] [http://lovereceier.ifrance.com/lib/driver-motorola.htm driver motorola c450 xp] [http://toofarfrommaybe.ifrance.com/view/new/una-dopo/ una dopo laltra] [http://tat-ooin.ifrance.com/resources/articles/enfisema-polmonare.htm enfisema polmonare] [http://singaporepets.ifrance.com/library/html/musica-pop.htm musica pop] [http://akmokanzen.ifrance.com/images/small/hard-disk.htm hard disk iomega esterno autoalimentato] [http://kalian42.ifrance.com/text/directory/toscana-casa/ toscana casa vacanza] [http://akmokanzen.ifrance.com/images/small/tabletas.htm tabletas] [http://voltia.ifrance.com/content/view/attivita-vincolante.htm attivita vincolante e discrezionale] [http://jdawsona.ifrance.com/data/florat/ florat] [http://lovereceier.ifrance.com/lib/dino-il.htm dino il lavandino] [http://girlwho-is.ifrance.com/img/styles/maniglia-design.htm maniglia design] [http://lovereceier.ifrance.com/lib/olympus-dm.htm olympus dm 20] {{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 22:04, 19 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