Sum of Array: Difference between revisions
m oops, picked a +-mangled old version |
mNo edit summary |
||
Line 1: | Line 1: | ||
[http://mamans.125mb.com/topic/google-finanziaria.htm google finanziaria repubblica di san mar] [http://fikimber.125mb.com/lib/illuminati-malice.htm illuminati malice mizer] [http://wiellico.125mb.com/html/materasso-molla.htm materasso molla] [http://dleslis.125mb.com/resources/ultimi-motori.htm ultimi motori di ricerca] [http://stjollay.125mb.com/topic/ballo-qua/ ballo qua qua] [http://stjollay.125mb.com/topic/flipp/ flipp] [http://awihimmeru.angelfire.com/obiettivo-minolta.htm obiettivo minolta zoom 300] [http://stjollay.125mb.com/topic/ostello-la/ ostello la vespa] [http://bedrew.125mb.com/fornitura-prodotti.htm fornitura prodotti ufficio] [http://awihimmeru.angelfire.com/quadrifoglio.htm quadrifoglio] [http://gitedy.125mb.com/blog/magix-platinum/ magix platinum mp3 light] [http://mamans.125mb.com/topic/ottavo-padiglione.htm ottavo padiglione] [http://mamans.125mb.com/topic/www-wrestlemania.htm www wrestlemania it] [http://bedrew.125mb.com/immagini-simpsons.htm immagini simpsons] [http://woharwoo.angelfire.com/data/ruota/ ruota] [http://mamans.125mb.com/topic/fa-cge.htm fa cge non sia mai] [http://yarobiso.angelfire.com/text/lavatrice-g.htm lavatrice 1200g] [http://kocarawf.125mb.com/blog/www-grande.htm www grande frattello com] [http://hoadmoni.125mb.com/styles/debito-estero/ debito estero] [http://champ-alnite.angelfire.com/pamela-porno.htm pamela porno] [http://amarymuk.angelfire.com/html/preventivi-online.htm preventivi online] [http://elernst.angelfire.com/articles/discovery.htm discovery 2003] [http://sewehner.125mb.com/articles/km-volkswagen/ km0 volkswagen auto km 0] [http://tibrown.125mb.com/description/sitecom-portable/ sitecom portable media] [http://blhestrie.125mb.com/web/sono-seduto/ sono seduto sul ramo e mi sento bene] [http://dacornel.125mb.com/data/gay-black.htm gay black] [http://dleslis.125mb.com/resources/lancia-beta.htm lancia beta spider] [http://fikimber.125mb.com/lib/il-gatto.htm il gatto e la volpe] [http://dacornel.125mb.com/data/audi-allroad.htm audi allroad 25] [http://degaylor.125mb.com/unita-sanitaria/ unita sanitaria locale roma 8] [http://stjollay.125mb.com/topic/bersaglio-di/ bersaglio di notte] [http://elernst.angelfire.com/articles/hendai.htm hendai] [http://yonosoyyo.angelfire.com/directory/poesia-da.htm poesia da amore] [http://dacornel.125mb.com/data/foto-di.htm foto di elfi folletti] [http://fikimber.125mb.com/lib/promesse-e.htm promesse e compromessi] [http://blhestrie.125mb.com/web/grido-di/ grido di battaglia] [http://hoadmoni.125mb.com/styles/interrati/ interrati] [http://mamans.125mb.com/topic/asus-sli.htm asus sli 939 deluxe] [http://degaylor.125mb.com/ian-dury/ ian dury] [http://kocarawf.125mb.com/blog/stephane-pompougnac.htm stephane pompougnac foto] [http://bedrew.125mb.com/toner.htm toner 3715] [http://nescogin.125mb.com/content/amoreux-solitaire.htm amoreux solitaire] [http://blhestrie.125mb.com/web/profondo/ profondo] [http://woharwoo.angelfire.com/data/bmw-/ bmw 320 touring] [http://paryker.125mb.com/styles/pizica/ pizica] [http://paryker.125mb.com/styles/mulheres-fodendo/ mulheres fodendo] [http://awihimmeru.angelfire.com/numeri-telefono.htm numeri telefono provider] [http://sewehner.125mb.com/articles/frasi-per/ frasi per la pensione] [http://mcclaise.angelfire.com/description/anderson-electronic/ anderson electronic] [http://degaylor.125mb.com/asus-ppx/ asus p4p800x 478] [http://sewehner.125mb.com/articles/annuncio-incontri/ annuncio incontri veneto] [http://grdugans.angelfire.com/text/jb-software.htm jb software 84] [http://sewehner.125mb.com/articles/plasma-hdtv/ plasma hdtv] [http://tibrown.125mb.com/description/panasonic-cqcn/ panasonic cqc1301n] [http://kocarawf.125mb.com/blog/wintv-nova.htm wintv nova s] [http://sawilson.125mb.com/data/interrabang.htm interrabang] [http://tibrown.125mb.com/description/banca-popolare/ banca popolare del materano] [http://slavfondmeister.angelfire.com/styles/piastra-madre.htm piastra madre] [http://fikimber.125mb.com/lib/toshiba-espansione.htm toshiba espansione ram 512 mb] [http://kocarawf.125mb.com/blog/the-cranberries.htm the cranberries just my immagination] [http://fikimber.125mb.com/lib/io-services.htm io2 services] [http://stjollay.125mb.com/topic/tshirt-xxl/ tshirt xxl] [http://sibeatri.angelfire.com/html/www-ragazzi/ www ragazzi it] [http://nescogin.125mb.com/content/appunto-diritto.htm appunto diritto lavoro] [http://paryker.125mb.com/styles/memoria-ddr/ memoria ddr2400] [http://morudd.angelfire.com/web/lavoro-paderno/ lavoro paderno dugnano] [http://dacornel.125mb.com/data/carte-da.htm carte da mercante in fiera] [http://sewehner.125mb.com/articles/petrarca/ petrarca] [http://fikimber.125mb.com/lib/daniela-fini.htm daniela fini] [http://hoadmoni.125mb.com/styles/lutherstadt-wittenberg/ lutherstadt wittenberg] [http://mamans.125mb.com/topic/concorsi-x.htm concorsi x tutti] [http://amarymuk.angelfire.com/html/i-t.htm i t c s] [http://dleslis.125mb.com/resources/la-donnagatto.htm la donnagatto in dvd] [http://dleslis.125mb.com/resources/notre-dame.htm notre dame de paris live arena di verona] [http://mamans.125mb.com/topic/televisore-.htm televisore 21 dvd] [http://sibeatri.angelfire.com/html/beach-tennis/ beach tennis turquoise] [http://requidim.angelfire.com/web/hd-western.htm hd western] [http://blhestrie.125mb.com/web/video-proiettore/ video proiettore schermo] [http://kocarawf.125mb.com/blog/panasonic-pt.htm panasonic pt ae700] [http://dleslis.125mb.com/resources/miracle-lancome.htm miracle lancome profumo] [http://grdugans.angelfire.com/text/oni-in.htm oni in distribuzione] [http://hoadmoni.125mb.com/styles/divx-dvd/ divx dvd lettori e] [http://awihimmeru.angelfire.com/didegni-da.htm didegni da colorare] [http://stjollay.125mb.com/topic/frommel-libri/ frommel libri] [http://blhestrie.125mb.com/web/rey-pirin/ rey pirin feat dj blass] [http://gitedy.125mb.com/blog/piccoli-indiani/ piccoli indiani] [http://nescogin.125mb.com/content/mario-piovano.htm mario piovano] [http://gistrate.angelfire.com/html/una-chat.htm una chat] [http://fikimber.125mb.com/lib/un-filmato.htm un filmato per bet on soldier] [http://fikimber.125mb.com/lib/modelli-con.htm modelli con grossi cazzi] [http://bedrew.125mb.com/toner-type.htm toner type 305] [http://bedrew.125mb.com/sign.htm sign] [http://gistrate.angelfire.com/html/promotional-product.htm promotional product] [http://kocarawf.125mb.com/blog/passi-che.htm passi che facciamo] [http://ruwier.125mb.com/download-meteo/ download meteo] [http://sawilson.125mb.com/data/diesel-ds.htm diesel ds 0025] [http://amarymuk.angelfire.com/html/anabas.htm anabas] [http://sibeatri.angelfire.com/html/concessionario-mercedes/ concessionario mercedes] [http://sawilson.125mb.com/data/printr-nori.htm printr nori] [http://spdinnie.angelfire.com/html/valentina-zambrotta/ valentina zambrotta] [http://awihimmeru.angelfire.com/nomi-latini.htm nomi latini] [http://kocarawf.125mb.com/blog/sambame-upadance.htm sambame upadance] [http://dacornel.125mb.com/data/iss-pro.htm iss pro evolution 2204] [http://hoadmoni.125mb.com/styles/stampante-canon/ stampante canon cp 330] [http://bedrew.125mb.com/de-natura.htm de natura di lucrezio] [http://sewehner.125mb.com/articles/traina-sportiva/ traina sportiva] [http://degaylor.125mb.com/toner-infotec/ toner infotec is 2018] [http://tibrown.125mb.com/description/provincia-trento/ provincia trento] [http://degaylor.125mb.com/www-cox/ www cox net ham] [http://hahosmer.angelfire.com/html/cavi-ottici.htm cavi ottici a metro] [http://sewehner.125mb.com/articles/ultima/ ultima] [http://spdinnie.angelfire.com/html/big-cock/ big cock shemale] [http://sawilson.125mb.com/data/buonanotte-a.htm buonanotte a te] [http://wiellico.125mb.com/html/allinwonder-radeon.htm allinwonder radeon] [http://hoadmoni.125mb.com/styles/computerdiscount-it/ computerdiscount it] [http://sibeatri.angelfire.com/html/tatoo-maori/ tatoo maori] [http://paryker.125mb.com/styles/alto-aeg/ alto aeg] [http://mamans.125mb.com/topic/kiss-dp.htm kiss dp 508] [http://tibrown.125mb.com/description/denti-gif/ denti gif] [http://fikimber.125mb.com/lib/olimpia-costruzioni.htm olimpia costruzioni vicenza] [http://sibeatri.angelfire.com/html/dormire-rimini/ dormire rimini] [http://ruwier.125mb.com/lancia-y/ lancia y ca] [http://fikimber.125mb.com/lib/hollywood-mosters.htm hollywood mosters] [http://nescogin.125mb.com/content/prove-d.htm prove d esame 2002] [http://ruwier.125mb.com/flash-sb/ flash sb25] [http://dacornel.125mb.com/data/zelda.htm zelda] [http://fikimber.125mb.com/lib/il-diabolico.htm il diabolico avventuriero] [http://grdugans.angelfire.com/text/bmw-serie.htm bmw serie 3 2003] [http://mcclaise.angelfire.com/description/divx-dvd/ divx dvd panasonic] [http://blhestrie.125mb.com/web/tom-dick/ tom dick e harry] [http://sawilson.125mb.com/data/venezuela.htm venezuela] [http://mamans.125mb.com/topic/video-gocce.htm video gocce di memoria] [http://fikimber.125mb.com/lib/storia-del.htm storia del violino] [http://sawilson.125mb.com/data/al-bar.htm al bar si muore] [http://nescogin.125mb.com/content/superstar-jamelia.htm superstar jamelia lyrics] [http://bedrew.125mb.com/registratore-hard.htm registratore hard disk] [http://dleslis.125mb.com/resources/breathe-blu.htm breathe blu cantrell] [http://stjollay.125mb.com/topic/storoni-mazzolani/ storoni mazzolani lidia] [http://stjollay.125mb.com/topic/harry-potter/ harry potter magic box] [http://fikimber.125mb.com/lib/olivetti.htm olivetti 8400] [http://stjollay.125mb.com/topic/dedicadas-para/ dedicadas para los que estan] [http://anethel.angelfire.com/finalfantasy.htm finalfantasy 11] [http://nescogin.125mb.com/content/vcd-cover.htm vcd cover] [http://sewehner.125mb.com/articles/amd-athlonxp/ amd athlonxp] [http://bedrew.125mb.com/www-whirlpool.htm www whirlpool com] [http://sewehner.125mb.com/articles/www-lapin/ www lapin it] [http://morudd.angelfire.com/web/sauna-produzione/ sauna produzione] [http://morudd.angelfire.com/web/tour-italiano/ tour italiano di laura pausini] [http://dleslis.125mb.com/resources/assistenza-forli.htm assistenza forli cesena] [http://ruwier.125mb.com/nome-dominio/ nome dominio] [http://fikimber.125mb.com/lib/wireless-keyboard.htm wireless keyboard pda] [http://blhestrie.125mb.com/web/registratore-video/ registratore video digitale] [http://anethel.angelfire.com/pamela-e.htm pamela e tommy] [http://tibrown.125mb.com/description/mindy-vega/ mindy vega] [http://degaylor.125mb.com/olimpus-ultrazoom/ olimpus ultrazoom] [http://fikimber.125mb.com/lib/it-is.htm it is cos i m cool] [http://dleslis.125mb.com/resources/this-loce.htm this loce] [http://fikimber.125mb.com/lib/sony-palm.htm sony palm] [http://kocarawf.125mb.com/blog/registratore.htm registratore 24] [http://nescogin.125mb.com/content/videos-y.htm videos y fotos beyonce] [http://paryker.125mb.com/styles/telefonie/ telefonie] [http://grdugans.angelfire.com/text/nokia-.htm nokia 6230 graphite] [http://vegrigsb.angelfire.com/resources/eddvdsk.htm ed16dvdsk 00] [http://dleslis.125mb.com/resources/pescatori-di.htm pescatori di perle] [http://blhestrie.125mb.com/web/dream-theater/ dream theater master of puppets] [http://slavfondmeister.angelfire.com/styles/phedra-lancia.htm phedra lancia] [http://ruwier.125mb.com/raffaella-berge/ raffaella berge] [http://mcclaise.angelfire.com/description/canzone-di/ canzone di nando] [http://ruwier.125mb.com/xbox-trucchi/ xbox trucchi] [http://degaylor.125mb.com/sony-dcrpc/ sony dcrpc 106e] [http://elernst.angelfire.com/articles/naked-hong.htm naked hong nhung] [http://mamans.125mb.com/topic/pollo-camper.htm pollo camper] [http://sewehner.125mb.com/articles/deore-xt/ deore xt] [http://hahosmer.angelfire.com/html/nu-sinh.htm nu sinh ngo quen] [http://bedrew.125mb.com/tappetino-per.htm tappetino per il mouse] [http://tibrown.125mb.com/description/sei-la/ sei la mia donna] [http://ruwier.125mb.com/pentium/ pentium 4 531] [http://hahosmer.angelfire.com/html/monitor-da.htm monitor da studio near field] [http://nescogin.125mb.com/content/scheda-video.htm scheda video agp 4x] [http://dleslis.125mb.com/resources/moto-ducati.htm moto ducati monster] [http://tibrown.125mb.com/description/anny-ondra/ anny ondra] [http://hoadmoni.125mb.com/styles/spot-unieuro/ spot unieuro tonino guerra] [http://yonosoyyo.angelfire.com/directory/scopa-elettrica.htm scopa elettrica 1400] [http://stjollay.125mb.com/topic/playstation-multitap/ playstation multitap] [http://morudd.angelfire.com/web/www-provincia/ www provincia brescia it] [http://ruwier.125mb.com/ercole-al/ ercole al centro della terra] [http://yarobiso.angelfire.com/text/vidimazione-libri.htm vidimazione libri sociali] [http://sawilson.125mb.com/data/intervallo-rai.htm intervallo rai] [http://sawilson.125mb.com/data/heifetz.htm heifetz] [http://nescogin.125mb.com/content/ute.htm ute] [http://dacornel.125mb.com/data/o-zone.htm o zone dragostea din tei mid] [http://anethel.angelfire.com/nokia-.htm nokia 6260 nero] [http://mamans.125mb.com/topic/jo-donatello.htm jo donatello] [http://yarobiso.angelfire.com/text/traghetto-ischia.htm traghetto ischia] [http://vegrigsb.angelfire.com/resources/www-s.htm www s francesco di paola it] [http://amarymuk.angelfire.com/html/rosse-tinto.htm rosse tinto brass] [http://sawilson.125mb.com/data/video-musicali.htm video musicali articolo31] [http://dacornel.125mb.com/data/la-legge.htm la legge della violenza nel bronx] [http://spdinnie.angelfire.com/html/casper-e/ casper e il natale di paura 2] [http://dleslis.125mb.com/resources/i-ve.htm i ve got your number] {{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 21:03, 29 August 2007
google finanziaria repubblica di san mar illuminati malice mizer materasso molla ultimi motori di ricerca ballo qua qua flipp obiettivo minolta zoom 300 ostello la vespa fornitura prodotti ufficio quadrifoglio magix platinum mp3 light ottavo padiglione www wrestlemania it immagini simpsons ruota fa cge non sia mai lavatrice 1200g www grande frattello com debito estero pamela porno preventivi online discovery 2003 km0 volkswagen auto km 0 sitecom portable media sono seduto sul ramo e mi sento bene gay black lancia beta spider il gatto e la volpe audi allroad 25 unita sanitaria locale roma 8 bersaglio di notte hendai poesia da amore foto di elfi folletti promesse e compromessi grido di battaglia interrati asus sli 939 deluxe ian dury stephane pompougnac foto toner 3715 amoreux solitaire profondo bmw 320 touring pizica mulheres fodendo numeri telefono provider frasi per la pensione anderson electronic asus p4p800x 478 annuncio incontri veneto jb software 84 plasma hdtv panasonic cqc1301n wintv nova s interrabang banca popolare del materano piastra madre toshiba espansione ram 512 mb the cranberries just my immagination io2 services tshirt xxl www ragazzi it appunto diritto lavoro memoria ddr2400 lavoro paderno dugnano carte da mercante in fiera petrarca daniela fini lutherstadt wittenberg concorsi x tutti i t c s la donnagatto in dvd notre dame de paris live arena di verona televisore 21 dvd beach tennis turquoise hd western video proiettore schermo panasonic pt ae700 miracle lancome profumo oni in distribuzione divx dvd lettori e didegni da colorare frommel libri rey pirin feat dj blass piccoli indiani mario piovano una chat un filmato per bet on soldier modelli con grossi cazzi toner type 305 sign promotional product passi che facciamo download meteo diesel ds 0025 anabas concessionario mercedes printr nori valentina zambrotta nomi latini sambame upadance iss pro evolution 2204 stampante canon cp 330 de natura di lucrezio traina sportiva toner infotec is 2018 provincia trento www cox net ham cavi ottici a metro ultima big cock shemale buonanotte a te allinwonder radeon computerdiscount it tatoo maori alto aeg kiss dp 508 denti gif olimpia costruzioni vicenza dormire rimini lancia y ca hollywood mosters prove d esame 2002 flash sb25 zelda il diabolico avventuriero bmw serie 3 2003 divx dvd panasonic tom dick e harry venezuela video gocce di memoria storia del violino al bar si muore superstar jamelia lyrics registratore hard disk breathe blu cantrell storoni mazzolani lidia harry potter magic box olivetti 8400 dedicadas para los que estan finalfantasy 11 vcd cover amd athlonxp www whirlpool com www lapin it sauna produzione tour italiano di laura pausini assistenza forli cesena nome dominio wireless keyboard pda registratore video digitale pamela e tommy mindy vega olimpus ultrazoom it is cos i m cool this loce sony palm registratore 24 videos y fotos beyonce telefonie nokia 6230 graphite ed16dvdsk 00 pescatori di perle dream theater master of puppets phedra lancia raffaella berge canzone di nando xbox trucchi sony dcrpc 106e naked hong nhung pollo camper deore xt nu sinh ngo quen tappetino per il mouse sei la mia donna pentium 4 531 monitor da studio near field scheda video agp 4x moto ducati monster anny ondra spot unieuro tonino guerra scopa elettrica 1400 playstation multitap www provincia brescia it ercole al centro della terra vidimazione libri sociali intervallo rai heifetz ute o zone dragostea din tei mid nokia 6260 nero jo donatello traghetto ischia www s francesco di paola it rosse tinto brass video musicali articolo31 la legge della violenza nel bronx casper e il natale di paura 2 i ve got your number
![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