Sum of Array: Difference between revisions
Content added Content deleted
mNo edit summary |
m (fixed redirect since multiple redirects aren't followed) |
||
(22 intermediate revisions by 16 users not shown) | |||
Line 1: | Line 1: | ||
#REDIRECT [[Sum and product of an array]] |
|||
[http://snailtail.cn/www-ggarredamenti/index.html www ggarredamenti it] [http://quoits.cn/africa-segreta.html africa segreta] [http://berlusconipatpat.clung.cn/index.html berlusconi pat pat] [http://beztebe.fast-road.cn/index.html bez tebe] [http://bdsmslaveemaster.kittiss.cn/index.html bdsm slave e master] [http://quoits.cn/a-venezia,.html a venezia, una notte] [http://knock-knock.cn/www-romani/index.html www romani] [http://knock-knock.cn/www-mototuring/index.html www mototuring com] [http://benassibrossalbum.midways.cn/index.html benassi bross album] [http://quoits.cn/allarme-dal.html allarme dal cielo] [http://brigantese.clung.cn/index.html brigante se] [http://quoits.cn/zenica-blues.html zenica blues] [http://balletto.midways.cn/index.html balletto] [http://balenavalentina.romanikki.cn/index.html balena valentina] [http://wonted.cn/www-moto/index.html www moto usate it] [http://kitmun.cn/you-raise.html you raise meup] [http://kitmun.cn/yaoou.html yaoou] [http://snailtail.cn/xirincs-romania/index.html xirincs romania] [http://quoits.cn/al-sole.html al sole] [http://snailtail.cn/yo-no-sopy/index.html yo no sopy tu marido] [http://brigittalajtos.fast-road.cn/index.html brigitta lajtos] [http://snailtail.cn/www-rosa/index.html www rosa dei venti it] [http://bubblintheblue.clung.cn/index.html bubblin the blue] [http://snailtail.cn/xsara-picasso/index.html xsara picasso citroen usata] [http://snailtail.cn/xxl-tv/index.html xxl tv] [http://bennasibrosdhanylyrics.clung.cn/index.html bennasi bros dhany lyrics] [http://knock-knock.cn/wwwelchat/index.html wwwelchat com] [http://knock-knock.cn/www-uninsubria/index.html www uninsubria it] [http://kitmun.cn/zyon-y.html zyon y lennox] [http://knock-knock.cn/www-kala-com/index.html www kala com mx] [http://bestiality.fast-road.cn/index.html bestiality] [http://babybash3rdwishobsesion.fast-road.cn/index.html baby bash 3rd wish obsesion] [http://biginjapa.midways.cn/index.html big in japa] [http://kitmun.cn/y-camino-de.html y camino de la vereda] [http://snailtail.cn/yo-voy-allegar/index.html yo voy allegar] [http://quoits.cn/america-3000.html america 3000 - il pianeta delle amazzoni] [http://bluebreahteasy.midways.cn/index.html blue breaht easy] [http://knock-knock.cn/www-welivetogether/index.html www welivetogether com] [http://knock-knock.cn/www-enssib/index.html www enssib fr] [http://bluejean.romanikki.cn/index.html blue jean] [http://snailtail.cn/www-ssis-unige/index.html www ssis unige it] [http://buoniocattivii.midways.cn/index.html buoni o cattivii] [http://knock-knock.cn/www-yaho-com/index.html www yaho com] [http://quoits.cn/zelig-sesto.html zelig sesto san giovanni] [http://knock-knock.cn/www-kaysnet/index.html www kaysnet co uk] [http://kitmun.cn/yoss.html yoss] [http://snailtail.cn/www-guidarepilotare/index.html www guidarepilotare com] [http://kitmun.cn/yume-no-are.html yume no are] [http://quoits.cn/zeroassoluto.html zeroassoluto almeno mezz ora] [http://quoits.cn/amore,-dolore.html amore, dolore e allegria] [http://quoits.cn/arrangiatevi!.html arrangiatevi!] [http://wonted.cn/www-chiamami/index.html www chiamami st] [http://knock-knock.cn/www-ofuchico/index.html www ofuchico com br] [http://knock-knock.cn/www-voli/index.html www voli it] [http://snailtail.cn/yhoo/index.html yhoo] [http://quoits.cn/zone-allarm.html zone allarm italiano] [http://kitmun.cn/yorh.html yorh] [http://quoits.cn/zorba-il.html zorba il greco] [http://belan.romanikki.cn/index.html belan] [http://beblingotto.romanikki.cn/index.html b e b lingotto] [http://snailtail.cn/xn-view/index.html xn view] [http://kitmun.cn/yamaha-tastiere.html yamaha tastiere] [http://quoits.cn/a-008,-operazione.html a 008, operazione sterminio] [http://quoits.cn/zahira.html zahira] [http://botellitadejerez.midways.cn/index.html botellita de jerez] [http://buttercupwhydoyoubuildmeup.clung.cn/index.html buttercup why do you build me up] [http://barbulesti.fast-road.cn/index.html barbulesti] [http://snailtail.cn/www-policastrum/index.html www policastrum it] [http://knock-knock.cn/www-mensatel/index.html www mensatel com] [http://bertopisano.kittiss.cn/index.html berto pisano] [http://baciolesbico.fast-road.cn/index.html bacio lesbico] [http://kitmun.cn/zucchero-grande.html zucchero grande] [http://breateeasy.clung.cn/index.html breate easy] [http://beejeas.romanikki.cn/index.html bee jeas] [http://biografiadavidbowie.clung.cn/index.html biografia david bowie] [http://wonted.cn/www-wrid-cup/index.html www wrid cup com] [http://kitmun.cn/yunan.html yunan] [http://knock-knock.cn/www-oksesso/index.html www oksesso] [http://ballifolk.clung.cn/index.html balli folk] [http://buleriamidi.midways.cn/index.html buleria midi] [http://wonted.cn/www-enaip/index.html www enaip it] [http://bratzdadisegnare.romanikki.cn/index.html bratz da disegnare] [http://blanquitasaldesnudo.clung.cn/index.html blanquitas al desnudo] [http://busheblair.midways.cn/index.html bush e blair] [http://wonted.cn/www-tvantioquia/index.html www tvantioquia com] [http://knock-knock.cn/www-csalecce/index.html www csalecce] [http://knock-knock.cn/www-bibdarte/index.html www bibdarte it] [http://basumba.kittiss.cn/index.html basumba] [http://kitmun.cn/yesterday-when.html yesterday when i was young] [http://brecovich.kittiss.cn/index.html brecovich] [http://bachair.romanikki.cn/index.html bach air] [http://britneyspearstoxic.kittiss.cn/index.html britney spears toxic] [http://wonted.cn/wamma-wamma/index.html wamma wamma wamma] [http://ballobrasile.romanikki.cn/index.html ballo brasile] [http://wonted.cn/www-ksuave/index.html www ksuave com] [http://knock-knock.cn/www-culladelbimbo/index.html www culladelbimbo it] [http://barracuda.romanikki.cn/index.html barracuda] [http://snailtail.cn/www-intimissimi/index.html www intimissimi] [http://quoits.cn/zelig-mp3.html zelig mp3] [http://blueifyoucomeback.kittiss.cn/index.html blue if you come back] [http://wonted.cn/www-asl8cagliari/index.html www asl8cagliari it] [http://brooklinbounce.clung.cn/index.html brooklin bounce] [http://snailtail.cn/www-policia/index.html www policia gov co 51] [http://bennychao.romanikki.cn/index.html benny chao] [http://bluecdonelove.midways.cn/index.html blue cd one love] [http://knock-knock.cn/www-cronache/index.html www cronache rapimenti it] [http://quoits.cn/alterazioni.html alterazioni della realtà] [http://wonted.cn/www-commercioelettronicoitaliano/index.html www commercioelettronicoitaliano it] [http://kitmun.cn/zuchero.html zuchero everybody] [http://besenzoni.romanikki.cn/index.html besenzoni] [http://quoits.cn/zaion-y-lenox.html zaion y lenox] [http://blaackeyedpeas.midways.cn/index.html blaack eyed peas] [http://kitmun.cn/yvonne-caterfeld.html yvonne caterfeld] [http://kitmun.cn/you-re-my.html you re my heart yo re my soul] [http://wonted.cn/www-svago/index.html www svago com] [http://wonted.cn/www-rete4-com/index.html www rete4 com] [http://kitmun.cn/yaesu-vx7.html yaesu vx7] [http://bennibennassi.kittiss.cn/index.html benni bennassi] [http://quoits.cn/abissi.html abissi] [http://wonted.cn/www-santi-beati/index.html www santi beati it] [http://snailtail.cn/xutos-pontapes/index.html xutos pontapes] [http://bubblintraduzione.kittiss.cn/index.html bubblin traduzione] [http://kitmun.cn/zero-assoluto.html zero assoluto video] [http://knock-knock.cn/www-cesat/index.html www cesat com] [http://b12feat.romanikki.cn/index.html b 12 feat] [http://knock-knock.cn/where-you-lead/index.html where you lead carol king] [http://blondiecallme.midways.cn/index.html blondie call me] [http://bluestaff.romanikki.cn/index.html blue staff] [http://knock-knock.cn/www-stresa-villaggi/index.html www stresa villaggi] [http://biografiasdequimicos.kittiss.cn/index.html biografias de quimicos] [http://blogssex.romanikki.cn/index.html blogs sex] [http://knock-knock.cn/www-betamotor/index.html www betamotor it] [http://knock-knock.cn/www-eritornodate/index.html www eritornodate it] [http://blackfat.romanikki.cn/index.html black fat] [http://snailtail.cn/www-provveditorato/index.html www provveditorato com] [http://quoits.cn/a-parigi-in-vacanza.html a parigi in vacanza] [http://knock-knock.cn/when-i-die/index.html when i die] [http://kitmun.cn/ytunnelpro272.html ytunnelpro272] [http://bassielettricicondor.kittiss.cn/index.html bassi elettrici condor] [http://buonaseradottoreclaudiamori.clung.cn/index.html buonasera dottore claudia mori] [http://budspencerterencehillmp3.midways.cn/index.html budspencer terence hill mp3] [http://basketusa.clung.cn/index.html basket u s a] [http://snailtail.cn/xircom/index.html xircom] [http://snailtail.cn/xbox-media/index.html xbox media play] [http://blameitonthe.clung.cn/index.html blame it on the] [http://wonted.cn/www-acquisto/index.html www acquisto casa it] [http://baviera.kittiss.cn/index.html baviera] [http://snailtail.cn/www-gruppofabbri/index.html www gruppofabbri it] [http://benassibrostipmyheart.kittiss.cn/index.html benassi bros tip my heart] [http://wonted.cn/www-tesi/index.html www tesi online it] [http://kitmun.cn/zika-cvetkovic.html zika cvetkovic] [http://wonted.cn/www-win-it-ricerca/index.html www win it ricerca v videos de hector y] [http://bordeisbordei.clung.cn/index.html bordeis bordei] [http://knock-knock.cn/www-webmail/index.html www webmail uiowa edu] [http://bersuitvergarabatcomar.clung.cn/index.html bersuitvergarabat com ar] [http://bigfun.clung.cn/index.html bigfun] [http://quoits.cn/angi-vera.html angi vera] [http://snailtail.cn/www-piss/index.html www piss] [http://wonted.cn/www-ultra/index.html www ultra blue tv com] [http://bandida.romanikki.cn/index.html bandida] [http://biografiamercedesambrus.kittiss.cn/index.html biografia mercedes ambrus] [http://kitmun.cn/you-re-crazy.html you re crazy] [http://wonted.cn/wilehalm-eschenbach/index.html wilehalm eschenbach] [http://kitmun.cn/you-and.html you and me] [http://kitmun.cn/zita.html zita] [http://kitmun.cn/yutaka-ozaki.html yutaka ozaki] [http://brel.romanikki.cn/index.html brel] [http://knock-knock.cn/wyhylylybymy/index.html wyhylylybymy] [http://bandalimon.clung.cn/index.html banda limon] [http://knock-knock.cn/www-diddle/index.html www diddle] [http://barzelletteufficio.fast-road.cn/index.html barzellette ufficio] [http://snailtail.cn/yulia-mayarchuck/index.html yulia mayarchuck nuda] [http://quoits.cn/zoccolone.html zoccolone] [http://wonted.cn/www-ausoniatools/index.html www ausoniatools com] [http://berlinoesto.fast-road.cn/index.html berlino esto] [http://bumerklingelton.clung.cn/index.html bumer klingelton] [http://quoits.cn/al-piccolo.html al piccolo margherita] [http://wonted.cn/www-basketball/index.html www basketball it] [http://barbieredellasera.clung.cn/index.html barbiere della sera] [http://kitmun.cn/yates.html yates] [http://britneyfoto.fast-road.cn/index.html britney foto] [http://wonted.cn/www-lunapop/index.html www lunapop com] [http://basicinstict.romanikki.cn/index.html basic instict] [http://buscofamilia.midways.cn/index.html busco familia] [http://brigantesimore.clung.cn/index.html brigante si more] [http://knock-knock.cn/weisse/index.html weisse] [http://knock-knock.cn/w-w-w-melamara/index.html w w w melamara rocks it] [http://wonted.cn/www-lisola/index.html www lisola it] [http://bailalanegra.romanikki.cn/index.html baila la negra] [http://wonted.cn/www-sunia-it/index.html www sunia it] [http://blekandpics.midways.cn/index.html blek and pics] [http://snailtail.cn/xing/index.html xing] [http://bandierrossa.clung.cn/index.html bandier rossa] [http://benedettoseitu.romanikki.cn/index.html benedetto sei tu] [http://snailtail.cn/www-infocamere/index.html www infocamere it] [http://quoits.cn/zstar.html zstar] [http://britanyspearsnuda.romanikki.cn/index.html britany spears nuda] [http://knock-knock.cn/www-unt-se/index.html www unt se] [http://quoits.cn/aspetta-primavera.html aspetta primavera bandini] [http://bronomartino.midways.cn/index.html brono martino] [http://snailtail.cn/www-sussidiario/index.html www sussidiario it] [http://snailtail.cn/yo-yo/index.html yo yo] [http://bellajovanotti.clung.cn/index.html bella jovanotti] [http://kitmun.cn/youtouchmytralala.html youtouchmytralala] [http://kitmun.cn/yammat.html yammat] [http://braccialetti.kittiss.cn/index.html braccialetti] [http://quoits.cn/agente-federale.html agente federale x 3] [http://barbaradurso.romanikki.cn/index.html barbara durso] [http://wonted.cn/wverytime/index.html wverytime] [http://wonted.cn/www-zzchat/index.html www zzchat com] [http://snailtail.cn/www-fotos/index.html www fotos de culos orq] [http://knock-knock.cn/www-chistes/index.html www chistes] [http://balaomagico.romanikki.cn/index.html balao magico] [http://britneyspearstutto.midways.cn/index.html britney spears tutto] [http://quoits.cn/amore-gitano.html amore gitano] [http://beetlejuice.fast-road.cn/index.html beetlejuice] [http://briankrausnudo.clung.cn/index.html brian kraus nudo] [http://bobbisueluther.kittiss.cn/index.html bobbi sue luther] [http://knock-knock.cn/www-itullians/index.html www itullians com] [http://bibliaassociazionediculturabiblica.clung.cn/index.html biblia associazione di cultura biblica] [http://quoits.cn/zavidovici.html zavidovici] [http://bonjovi.clung.cn/index.html bon jovi] [http://quoits.cn/zozzone.html zozzone] [http://bigliettisupercoppa.clung.cn/index.html biglietti supercoppa] [http://basicinstinct2.kittiss.cn/index.html basic instinct 2] [http://knock-knock.cn/wolsvagen/index.html wolsvagen] [http://knock-knock.cn/www-taxus/index.html www taxus it] [http://snailtail.cn/xcam-dvb/index.html xcam dvb] [http://snailtail.cn/www-gesu/index.html www gesu liberatore it] [http://wonted.cn/www-soluzioniecostruzioni/index.html www soluzioniecostruzioni com] [http://knock-knock.cn/wwwloredanabontempi/index.html wwwloredanabontempi it] [http://snailtail.cn/ylli-trokites/index.html ylli trokites] [http://quoits.cn/zeze-di-camargo.html zeze di camargo midi] [http://belleinperizoma.clung.cn/index.html belle in perizoma] [http://snailtail.cn/www-ss-ortana/index.html www ss ortana calcio it] [http://brigittakerkova.midways.cn/index.html brigitta kerkova] [http://bacididonna.clung.cn/index.html bacididonna] [http://bouledifrutta.kittiss.cn/index.html boule di frutta] [http://snailtail.cn/xvid/index.html xvid] [http://quoits.cn/zostawcie.html zostawcie] [http://wonted.cn/ww-alitalia/index.html ww alitalia it] [http://burregionecalabria.clung.cn/index.html bur regione calabria] [http://kitmun.cn/zanussiprofessional.html zanussiprofessional it] [http://kitmun.cn/yimou.html yimou] [http://knock-knock.cn/www-scozia-com/index.html www scozia com] [http://benedetticom.romanikki.cn/index.html benedetti com] [http://snailtail.cn/yaga-mackie/index.html yaga mackie ranks clase aparte] [http://quoits.cn/zigano.html zigano] [http://ballaballaballerino.clung.cn/index.html balla balla ballerino] [http://knock-knock.cn/www-ostaggi/index.html www ostaggi usa com] [http://kitmun.cn/zain.html zain] [http://wonted.cn/www-thefamilyitalia/index.html www thefamilyitalia it] [http://bandodiconcorsocorpoforestale.clung.cn/index.html bando di concorso corpo forestale] [http://bluegulity.kittiss.cn/index.html blue gulity] [http://kitmun.cn/zuccherina.html zuccherina] [http://wonted.cn/www-happidea/index.html www happidea it] [http://knock-knock.cn/www-travis-it/index.html www travis it] [http://boysdziewczonka.fast-road.cn/index.html boys dziewczonka] [http://knock-knock.cn/www-tittypalace/index.html www tittypalace it] [http://snailtail.cn/www-sexape-com/index.html www sexape com] [http://barrywhitetestigratis.kittiss.cn/index.html barry white testi gratis] [http://blackeiedpeace.romanikki.cn/index.html black eied peace] [http://snailtail.cn/www-sulpm/index.html www sulpm it] [http://knock-knock.cn/www-radio101/index.html www radio101 it] [http://brikbraknapoli.midways.cn/index.html brik brak napoli] [http://benedettapassione.midways.cn/index.html benedetta passione] [http://snailtail.cn/www-infostrada/index.html www infostrada spazio zero it] [http://kitmun.cn/yeha.html yeha] [http://bacherboy2.kittiss.cn/index.html bacher boy 2] [http://kitmun.cn/yooom-wara.html yooom wara yooom] [http://snailtail.cn/xin-tai-ruan/index.html xin tai ruan] [http://kitmun.cn/yannick.html yannick] [http://snailtail.cn/xpie/index.html xpie] [http://boxautolamiera.clung.cn/index.html box auto lamiera] [http://borsalavoro.midways.cn/index.html borsalavoro] [http://bilderirak.romanikki.cn/index.html bilder irak] [http://snailtail.cn/www-torremolinos/index.html www torremolinos com] [http://brunodelturco.midways.cn/index.html bruno del turco] [http://kitmun.cn/zaza.html zaza] [http://briggettabui.fast-road.cn/index.html briggetta bui] [http://bonnietayler.clung.cn/index.html bonnie tayler] [http://boiardovitaeopere.kittiss.cn/index.html boiardo vita e opere] [http://quoits.cn/allarme-a.html allarme a gibilterra] [http://wonted.cn/www-fantasmas/index.html www fantasmas co] [http://bacisaffici.fast-road.cn/index.html bacisaffici] [http://quoits.cn/a-pugni-nudi.html a pugni nudi] [http://kitmun.cn/yoli-babasonicos.html yoli babasonicos] [http://knock-knock.cn/www-vicenza/index.html www vicenza com] [http://snailtail.cn/www-raitre/index.html www raitre rai it] [http://snailtail.cn/y-ahoo-com/index.html y ahoo com] [http://wonted.cn/www-uff-com-mx/index.html www uff com mx] [http://wonted.cn/www-mans/index.html www mans com] [http://quoits.cn/attenzione-alla.html attenzione alla puttana santa] [http://knock-knock.cn/www-3sudest/index.html www 3sudest ro] [http://knock-knock.cn/www-evatremila/index.html www evatremila] [http://bullismo.midways.cn/index.html bullismo] [http://quoits.cn/angeli-del.html angeli del peccato] [http://bobdylanknockonheavensdoor.fast-road.cn/index.html bob dylan knock on heavens door] [http://bailedelpescao.clung.cn/index.html baile del pescao] [http://quoits.cn/zoo-105.html zoo 105] {{task}} |
|||
Compute the sum of the elements of an Array |
|||
==[[4D]]== |
|||
[[Category: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]]== |
|||
[[Category: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]]== |
|||
[[Category: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]]== |
|||
[[Category: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 plus plus|C ]]== |
|||
[[Category:C plus plus]] |
|||
'''Compiler:''' [[GNU Compiler Collection|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 sharp|C#]]== |
|||
[[Category:C sharp|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]]== |
|||
[[Category:Clean]] |
|||
array = {1, 2, 3, 4, 5} |
|||
Start = sum [x \\ x <-: array] |
|||
==[[ColdFusion]]== |
|||
[[Category:ColdFusion]] |
|||
<cfset myArray = listToArray("1,2,3,4,5")> |
|||
#arraySum(myArray)# |
|||
==[[Common Lisp]]== |
|||
[[Category: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]]== |
|||
[[Category:E]] |
|||
pragma.enable("accumulator") |
|||
accum 0 for x in [1,2,3,4,5] { _ x } |
|||
==[[Erlang]]== |
|||
[[Category:Erlang]] |
|||
Using the standard libraries: |
|||
% create the list: |
|||
L = lists:seq(1, 10). |
|||
% and compute its sum: |
|||
2000 |
|||
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]]== |
|||
[[Category:Forth]] |
|||
: sum ( addr cnt -- n ) |
|||
0 -rot |
|||
cells bounds do i @ cell loop ; |
|||
==[[FreeBASIC]]== |
|||
[[Category: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]]== |
|||
[[Category:Haskell]] |
|||
let values = [1..10] |
|||
sum values -- the easy way |
|||
foldl ( ) 0 values -- the hard way |
|||
==[[IDL]]== |
|||
[[Category:IDL]] |
|||
result = total(array) |
|||
==[[Java]]== |
|||
[[Category:Java]] |
|||
int value = 0; |
|||
int[] arg = new int[] { 1,2,3,4,5 }; |
|||
for (int i: arg) |
|||
value = i; |
|||
==[[JavaScript]]== |
|||
[[Category:JavaScript]] |
|||
var array = [1, 2, 3, 4, 5]; |
|||
var sum = 0; |
|||
for(var i in array) |
|||
sum = array[i]; |
|||
==[[Perl]]== |
|||
[[Category: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]]== |
|||
[[Category:PHP]] |
|||
$list = array(1,2,3,4,5,6,7,8,9); |
|||
echo array_sum($list); |
|||
==[[Pop11]]== |
|||
[[Category: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]]== |
|||
[[Category: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]]== |
|||
[[Category:Python]] |
|||
'''Interpeter:''' [[Python]] 2.5 |
|||
total = sum([1, 2, 3, 4, 5, 6, 7, 8, 9]) |
|||
==[[Ruby]]== |
|||
[[Category:Ruby]] |
|||
ary = [1,2,3,4,5] |
|||
sum = ary.inject{|currentSum,element|currentSum element} |
|||
# => 15 |
|||
== [[Scala]]== |
|||
[[Category: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]]== |
|||
[[Category: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]]== |
|||
[[Category:Standard ML]] |
|||
val array = [1,2,3,4,5]; |
|||
foldl op 0 array; |
|||
==[[Tcl]]== |
|||
[[Category:Tcl]] |
|||
Assuming the values are in a list named <tt>listname</tt>: |
|||
set result [expr [join $listname ]] |
|||
==[[Toka]]== |
|||
[[Category:Toka]] |
|||
[ ( array size -- sum ) |
|||
>r 0 r> [ over i swap get-element ] iterate nip ] is sum-array |
|||
==[[UNIX Shell]]== |
|||
[[Category: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 |
Latest revision as of 08:39, 2 July 2010
Redirect to: