Sum of Array: Difference between revisions
Undo revision 7064 by Special:Contributions/YkxCky (User talk:YkxCky) |
mNo edit summary |
||
Line 1: | Line 1: | ||
[http://quotes-girl.somee.com/content/view/mappa-grecia/ mappa grecia] [http://am-i-bird.somee.com/img/styles/rolling-thunder.htm rolling thunder] [http://am-i-bird.somee.com/img/styles/salween.htm salween] [http://trocterli.t35.com/img/styles/saitek-x/ saitek x52 hotas] [http://peter-land.somee.com/images/small/gassman.htm gassman] [http://flheddina.t35.com/web/tony-hawks/ tony hawks underground 2] [http://davelovesyou.somee.com/library/html/anacrusi/ anacrusi] [http://jedillar.125mb.com/resources/articles/condizionatori-dual/ condizionatori dual] [http://peter-land.somee.com/images/small/immigrazione.htm immigrazione] [http://luallard.t35.com/html/asus-anx/ asus a7n8x e deluxe] [http://lamcgie.125mb.com/html/nikon-coolpix.htm nikon coolpix 4600] [http://trocterli.t35.com/img/styles/griffin-airbase/ griffin airbase] [http://nairoz.somee.com/img/styles/katia-palmieri/ katia palmieri] [http://kiboukni.125mb.com/web/lampada-mt/ lampada mt 830] [http://luallard.t35.com/html/tonnie/ tonnie] [http://davelovesyou.somee.com/library/html/james-duncan/ james duncan] [http://madavid.t35.com/html/i-don/ i don t wanna to miss a thing] [http://de-der.somee.com/library/html/citroen-xantia.htm citroen xantia 1.8] [http://luallard.t35.com/html/notebook-battery/ notebook battery] [http://fergus-mac.somee.com/description/lib/biglietto-da/ biglietto da visita] [http://itgurney.t35.com/view/new/passeri/ passeri] [http://davelovesyou.somee.com/library/html/shure-cuffie/ shure cuffie auricolari] [http://kiboukni.125mb.com/web/gianciotto/ gianciotto] [http://jedillar.125mb.com/resources/articles/bonnie-tyner/ bonnie tyner] [http://viemalin.freeweb7.com/resources/articles/files-exe.htm files exe] [http://quotes-girl.somee.com/content/view/audigy-/ audigy 2 esterna] [http://lamcgie.t35.com/web/osman-aga.htm osman aga] [http://lakitsmi.freeweb7.com/web/google-co/ google co] [http://coarmago.freeweb7.com/library/html/pulecenella.htm pulecenella] [http://ouquaite.freeweb7.com/img/styles/babcock-harold/ babcock, harold delos] [http://lamcgie.t35.com/web/teofarma-srl.htm teofarma srl] [http://dabeutle.t35.com/content/view/irradio-lettore.htm irradio lettore divx dvd] [http://yourorus.somee.com/data/intel-p.htm intel p4 540] [http://fawarfo.freeweb7.com/topic/mamae-eu.htm mamae eu] [http://am-i-bird.somee.com/img/styles/mail-sorrisi.htm mail sorrisi mondadori it] [http://dabeutle.t35.com/content/view/notizie-.htm notizie 18 maggio] [http://thegoogly.somee.com/view/new/thomson-home.htm thomson home theatre] [http://lakitsmi.freeweb7.com/web/isuzu-d/ isuzu d max] [http://dj-deus.somee.com/lib/foto-di.htm foto di ragazze bellissime] [http://itgurney.t35.com/view/new/roma-albergo/ roma albergo centro] [http://lakitsmi.freeweb7.com/web/case-vacanza/ case vacanza montagna] [http://davelovesyou.somee.com/library/html/kill-bill/ kill bill cd musicali] [http://trocterli.t35.com/img/styles/register/ register] [http://dj-deus.somee.com/lib/marins-uccisi.htm marins uccisi] [http://itgurney.t35.com/view/new/uccidere-i/ uccidere i nemici con neve] [http://brrezab.freeweb7.com/img/styles/materiale-ufficio.htm materiale ufficio] [http://dj-deus.somee.com/lib/www-southwest.htm www southwest com] [http://madavid.t35.com/html/chevrolet-tahoe/ chevrolet tahoe] [http://coarmago.freeweb7.com/library/html/sertap-erener.htm sertap erener] [http://lamcgie.t35.com/web/rosa-vi.htm rosa vi] [http://peter-land.somee.com/images/small/rom-star.htm rom star trek] [http://viemalin.freeweb7.com/resources/articles/d-ettorre.htm d ettorre] [http://betheresa.t35.com/text/directory/jvc-d/ jvc d290] [http://flheddina.t35.com/web/venezia-tessile/ venezia tessile] [http://luallard.t35.com/html/singole-friuli/ singole friuli] [http://viemalin.freeweb7.com/resources/articles/dimage-g.htm dimage g500] [http://brrezab.freeweb7.com/img/styles/ati-radeon.htm ati radeon 9200 tv out] [http://rimarsh.t35.com/images/small/box-firewire.htm box firewire back-up e accessori] [http://dj-deus.somee.com/lib/lettore-dvd.htm lettore dvd nec] [http://coarmago.freeweb7.com/library/html/bang.htm bang] [http://ouquaite.freeweb7.com/img/styles/daredevil-colonna/ daredevil colonna sonora] [http://de-der.somee.com/library/html/carabinieri-prima.htm carabinieri. prima serie. vol. 1] [http://jedillar.125mb.com/resources/articles/cerimonie-ristorante/ cerimonie ristorante a napoli] [http://jututor.125mb.com/lib/radeon-xpro/ radeon x600pro vivo] [http://quotes-girl.somee.com/content/view/una-donna/ una donna sposata] [http://betheresa.t35.com/text/directory/gioco-mac/ gioco mac arcade] [http://coarmago.freeweb7.com/library/html/suzuki-alto.htm suzuki alto] [http://nairoz.somee.com/img/styles/dv-editing/ dv editing] [http://fergus-mac.somee.com/description/lib/langenthal/ langenthal] [http://betheresa.t35.com/text/directory/meninpain/ meninpain] [http://trocterli.t35.com/img/styles/integratori-labrada/ integratori labrada] [http://jedillar.125mb.com/resources/articles/samsung/ samsung 30] [http://davelovesyou.somee.com/library/html/prezzo-di/ prezzo di computer suunto] [http://rihelm.125mb.com/img/styles/da-gennaro/ da gennaro forno] [http://kiboukni.125mb.com/web/saggio-breve/ saggio breve sul verismo] [http://brrezab.freeweb7.com/img/styles/hauppauge-pvr.htm hauppauge pvr150] [http://de-der.somee.com/library/html/nokia-con.htm nokia con fotocamera] [http://de-der.somee.com/library/html/euro-soccer.htm euro soccer 2004] [http://nairoz.somee.com/img/styles/fame-in/ fame in africa] [http://jedillar.125mb.com/resources/articles/cowboy-bebop/ cowboy bebop. vol. 01] [http://lamcgie.125mb.com/html/m.htm 3m] [http://yourorus.somee.com/data/tesine-esami.htm tesine esami] [http://fawarfo.freeweb7.com/topic/ibm-mouse.htm ibm mouse e tastiere] [http://fawarfo.freeweb7.com/topic/over-the.htm over the edge] [http://luallard.t35.com/html/edel-waiss/ edel waiss] [http://madavid.t35.com/html/fudoki/ fudoki] [http://davelovesyou.somee.com/library/html/signore-mature/ signore mature] [http://luallard.t35.com/html/suggerisci-link/ suggerisci link] [http://jututor.125mb.com/lib/webcam-spazio/ webcam spazio] [http://itgurney.t35.com/view/new/las-vegas/ las vegas tours] [http://yourorus.somee.com/data/tesi-gratis.htm tesi gratis] [http://am-i-bird.somee.com/img/styles/edo-memoria.htm edo memoria ram] [http://itgurney.t35.com/view/new/dvi-to/ dvi to vga] [http://flheddina.t35.com/web/guscio-siemens/ guscio siemens s55] [http://nairoz.somee.com/img/styles/montalbano-commissario/ montalbano commissario] [http://davelovesyou.somee.com/library/html/tipo-canzoni/ tipo canzoni] [http://dabeutle.t35.com/content/view/hp-business.htm hp business 2600] [http://brrezab.freeweb7.com/img/styles/walter-trout.htm walter trout. relentless. the concert] [http://lensman.somee.com/html/bascet/ bascet] [http://coarmago.freeweb7.com/library/html/staffa-video.htm staffa video proiettore] [http://davelovesyou.somee.com/library/html/biliardo-sega/ biliardo sega] [http://ouquaite.freeweb7.com/img/styles/asus-notebook/ asus notebook v6v] [http://betheresa.t35.com/text/directory/selective/ selective] [http://de-der.somee.com/library/html/giochi-dell.htm giochi dell orrore] [http://nairoz.somee.com/img/styles/toyota-corolla/ toyota corolla verso 2001] [http://flheddina.t35.com/web/inni-di/ inni di guerra tedeschi] [http://coarmago.freeweb7.com/library/html/global-palyer.htm global palyer] [http://viemalin.freeweb7.com/resources/articles/maglie-donna.htm maglie donna diesel] [http://trocterli.t35.com/img/styles/llocali-gay/ llocali gay a genova] [http://itgurney.t35.com/view/new/the-black/ the black hole - il buco nero] [http://coarmago.freeweb7.com/library/html/differenze-erotiche.htm differenze erotiche] [http://luallard.t35.com/html/impreza-sti/ impreza sti 2002] [http://coarmago.freeweb7.com/library/html/christian-dior.htm christian dior black tie] [http://brrezab.freeweb7.com/img/styles/processori-intel.htm processori intel 775] [http://brrezab.freeweb7.com/img/styles/gagliardo-guido.htm gagliardo guido] [http://lamcgie.t35.com/web/www-anna.htm www anna secchi] [http://jututor.125mb.com/lib/mp-fm/ mp3 fm 1 gb] [http://kiboukni.125mb.com/web/bears/ bears] [http://kiboukni.125mb.com/web/litigio-donne/ litigio donne] [http://quotes-girl.somee.com/content/view/www-fotohighway/ www fotohighway com] [http://madavid.t35.com/html/maroon-/ maroon 5 testi] [http://fawarfo.freeweb7.com/topic/i-think.htm i think about you bryan adams] [http://davelovesyou.somee.com/library/html/you-are/ you are my] [http://coarmago.freeweb7.com/library/html/www-iacp.htm www iacp napoli it] [http://viemalin.freeweb7.com/resources/articles/hamtaro-paper.htm hamtaro paper dolls] [http://ouquaite.freeweb7.com/img/styles/simbolo-eletrico/ simbolo eletrico] [http://madavid.t35.com/html/sfondo-honda/ sfondo honda] [http://rimarsh.t35.com/images/small/blue-testo.htm blue testo di a chi mi dice] [http://trocterli.t35.com/img/styles/weekend-mare/ weekend mare] [http://luallard.t35.com/html/super-lotto/ super lotto] [http://lakitsmi.freeweb7.com/web/siemens-sl/ siemens sl 740] [http://jedillar.125mb.com/resources/articles/sesso-con/ sesso con animali] [http://yourorus.somee.com/data/motocicleta-rd.htm motocicleta rd yamah] [http://brrezab.freeweb7.com/img/styles/sarah-connor.htm sarah connor living to love you] [http://dj-deus.somee.com/lib/lancia-flavia.htm lancia flavia coupe 2000] [http://ouquaite.freeweb7.com/img/styles/fotoconduttore-aculaser/ fotoconduttore aculaser c9100] [http://jututor.125mb.com/lib/please-com/ please com home for christmas] [http://nairoz.somee.com/img/styles/stracult/ stracult] [http://nationavce.somee.com/library/html/giochi-sporchi/ giochi sporchi] [http://yourorus.somee.com/data/flash-card.htm flash card apacer] [http://thegoogly.somee.com/view/new/il-mercato.htm il mercato del lavoro] [http://kiboukni.125mb.com/web/itinerario-sicilia/ itinerario sicilia] [http://flheddina.t35.com/web/indri/ indri] [http://lakitsmi.freeweb7.com/web/vestiti-williams/ vestiti williams] [http://luallard.t35.com/html/marcia-elgar/ marcia elgar] [http://flheddina.t35.com/web/testi-jovanotti/ testi jovanotti] [http://lamcgie.125mb.com/html/pietro-germi.htm pietro germi] [http://itgurney.t35.com/view/new/skoda-auto/ skoda auto] [http://dj-deus.somee.com/lib/philips-brilliance.htm philips brilliance 21] [http://dj-deus.somee.com/lib/soluzione-gioco.htm soluzione gioco pc gratis] [http://dj-deus.somee.com/lib/cocco-bill.htm cocco bill. ritorna al futuro] [http://itgurney.t35.com/view/new/nautica-vendo/ nautica vendo] [http://lakitsmi.freeweb7.com/web/firmware-v/ firmware v300] [http://quotes-girl.somee.com/content/view/old-woman/ old woman] [http://betheresa.t35.com/text/directory/piatto-quadrato/ piatto quadrato] [http://am-i-bird.somee.com/img/styles/lavatrici-hoover.htm lavatrici hoover 650] [http://luallard.t35.com/html/the-sims/ the sims hot date] [http://peter-land.somee.com/images/small/combinata-legno.htm combinata legno] [http://itgurney.t35.com/view/new/cfnm/ cfnm] [http://kiboukni.125mb.com/web/www-citta/ www citta di padova] [http://am-i-bird.somee.com/img/styles/piano-ammortamento.htm piano ammortamento montante] [http://thegoogly.somee.com/view/new/lap-dance.htm lap dance locali] [http://coarmago.freeweb7.com/library/html/tshirt-marvel.htm t-shirt marvel uomo] [http://dabeutle.t35.com/content/view/cadiz-midi.htm cadiz midi] [http://peter-land.somee.com/images/small/aspirapolvere-per.htm aspirapolvere per parquet senza sacco] [http://jedillar.125mb.com/resources/articles/bacio-tra/ bacio tra madonna e britney] [http://lensman.somee.com/html/ixsus-/ ixsus 500 canon] [http://dj-deus.somee.com/lib/dress-my.htm dress my babe] [http://betheresa.t35.com/text/directory/foto-di/ foto di micetti] [http://jedillar.125mb.com/resources/articles/buongiorno-principessa/ buongiorno principessa] [http://yourorus.somee.com/data/asus-an.htm asus a8n -sli premium] [http://davelovesyou.somee.com/library/html/www-selec/ www selec cl] [http://dj-deus.somee.com/lib/blw.htm blw 165] [http://nairoz.somee.com/img/styles/navigazione-gps/ navigazione gps] [http://lakitsmi.freeweb7.com/web/profumo-valentino/ profumo valentino] [http://thegoogly.somee.com/view/new/ragazze-scollate.htm ragazze scollate] [http://ouquaite.freeweb7.com/img/styles/leos-great/ leos great day] [http://am-i-bird.somee.com/img/styles/gay-blowjob.htm gay blowjob] [http://de-der.somee.com/library/html/lucia.htm lucia 1] [http://madavid.t35.com/html/james-stewart/ james stewart] [http://madavid.t35.com/html/giradischi-thorens/ giradischi thorens] [http://fawarfo.freeweb7.com/topic/n.htm n92] [http://thegoogly.somee.com/view/new/disentis.htm disentis] [http://thegoogly.somee.com/view/new/enigmisti.htm enigmisti] [http://thegoogly.somee.com/view/new/bomba-carta.htm bomba carta] [http://madavid.t35.com/html/pro-cosme/ pro cosme] [http://quotes-girl.somee.com/content/view/gas-mask/ gas mask] [http://madavid.t35.com/html/hitachi-travelstar/ hitachi travelstar 7k60] [http://de-der.somee.com/library/html/how-many.htm how many roads bob dylan] [http://de-der.somee.com/library/html/nero-free.htm nero free download] [http://lensman.somee.com/html/lettore-pcmcia/ lettore pcmcia] [http://rihelm.125mb.com/img/styles/perdere-l/ perdere l amore ranieri] [http://rihelm.125mb.com/img/styles/www-lavatrici/ www lavatrici bosh com] [http://rimarsh.t35.com/images/small/ufficio-montebelluna.htm ufficio montebelluna] [http://kiboukni.125mb.com/web/lalla-e/ lalla e rossana] [http://jututor.125mb.com/lib/l-ultimo/ l ultimo valzer] [http://betheresa.t35.com/text/directory/catwoman-forever/ catwoman forever] [http://rimarsh.t35.com/images/small/laica-elettrostimolatore.htm laica elettrostimolatore] [http://itgurney.t35.com/view/new/www-ciunga/ www ciunga com] [http://lakitsmi.freeweb7.com/web/paint-shop/ paint shop 8] {{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 18:37, 22 August 2007
mappa grecia rolling thunder salween saitek x52 hotas gassman tony hawks underground 2 anacrusi condizionatori dual immigrazione asus a7n8x e deluxe nikon coolpix 4600 griffin airbase katia palmieri lampada mt 830 tonnie james duncan i don t wanna to miss a thing citroen xantia 1.8 notebook battery biglietto da visita passeri shure cuffie auricolari gianciotto bonnie tyner files exe audigy 2 esterna osman aga google co pulecenella babcock, harold delos teofarma srl irradio lettore divx dvd intel p4 540 mamae eu mail sorrisi mondadori it notizie 18 maggio thomson home theatre isuzu d max foto di ragazze bellissime roma albergo centro case vacanza montagna kill bill cd musicali register marins uccisi uccidere i nemici con neve materiale ufficio www southwest com chevrolet tahoe sertap erener rosa vi rom star trek d ettorre jvc d290 venezia tessile singole friuli dimage g500 ati radeon 9200 tv out box firewire back-up e accessori lettore dvd nec bang daredevil colonna sonora carabinieri. prima serie. vol. 1 cerimonie ristorante a napoli radeon x600pro vivo una donna sposata gioco mac arcade suzuki alto dv editing langenthal meninpain integratori labrada samsung 30 prezzo di computer suunto da gennaro forno saggio breve sul verismo hauppauge pvr150 nokia con fotocamera euro soccer 2004 fame in africa cowboy bebop. vol. 01 3m tesine esami ibm mouse e tastiere over the edge edel waiss fudoki signore mature suggerisci link webcam spazio las vegas tours tesi gratis edo memoria ram dvi to vga guscio siemens s55 montalbano commissario tipo canzoni hp business 2600 walter trout. relentless. the concert bascet staffa video proiettore biliardo sega asus notebook v6v selective giochi dell orrore toyota corolla verso 2001 inni di guerra tedeschi global palyer maglie donna diesel llocali gay a genova the black hole - il buco nero differenze erotiche impreza sti 2002 christian dior black tie processori intel 775 gagliardo guido www anna secchi mp3 fm 1 gb bears litigio donne www fotohighway com maroon 5 testi i think about you bryan adams you are my www iacp napoli it hamtaro paper dolls simbolo eletrico sfondo honda blue testo di a chi mi dice weekend mare super lotto siemens sl 740 sesso con animali motocicleta rd yamah sarah connor living to love you lancia flavia coupe 2000 fotoconduttore aculaser c9100 please com home for christmas stracult giochi sporchi flash card apacer il mercato del lavoro itinerario sicilia indri vestiti williams marcia elgar testi jovanotti pietro germi skoda auto philips brilliance 21 soluzione gioco pc gratis cocco bill. ritorna al futuro nautica vendo firmware v300 old woman piatto quadrato lavatrici hoover 650 the sims hot date combinata legno cfnm www citta di padova piano ammortamento montante lap dance locali t-shirt marvel uomo cadiz midi aspirapolvere per parquet senza sacco bacio tra madonna e britney ixsus 500 canon dress my babe foto di micetti buongiorno principessa asus a8n -sli premium www selec cl blw 165 navigazione gps profumo valentino ragazze scollate leos great day gay blowjob lucia 1 james stewart giradischi thorens n92 disentis enigmisti bomba carta pro cosme gas mask hitachi travelstar 7k60 how many roads bob dylan nero free download lettore pcmcia perdere l amore ranieri www lavatrici bosh com ufficio montebelluna lalla e rossana l ultimo valzer catwoman forever laica elettrostimolatore www ciunga com paint shop 8
![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