Sum of Array: Difference between revisions
Content deleted Content added
mNo edit summary |
m fixed redirect since multiple redirects aren't followed |
||
(75 intermediate revisions by 19 users not shown) | |||
Line 1: | Line 1: | ||
#REDIRECT [[Sum and product of an array]] |
|||
[http://tizolaa.ifrance.com/web/basti-distretto/ basti (distretto)] [http://highbulp.ifrance.com/images/small/kalat/ kalat] [http://lovereceier.ifrance.com/lib/vacanza-studio.htm vacanza studio in spagna] [http://voltia.ifrance.com/content/view/mandaryna-drifting.htm mandaryna drifting] [http://akmokanzen.ifrance.com/images/small/juliana-paz.htm juliana paz] [http://demurediablo.ifrance.com/library/html/annuncio/ annuncio 15889] [http://igaros.ifrance.com/lib/merdo/ merdo] [http://toofarfrommaybe.ifrance.com/view/new/no-perditempo/ no perditempo] [http://igaros.ifrance.com/lib/bejewlled/ bejewlled] [http://kalian42.ifrance.com/text/directory/telecom-/ telecom 187 sirio] [http://yeeden.ifrance.com/img/styles/la-comessa/ la comessa] [http://noxuhax.ifrance.com/content/view/sailor-moon.htm sailor moon s da scaricare] [http://lovereceier.ifrance.com/lib/recavo.htm recavo] [http://toofarfrommaybe.ifrance.com/view/new/hd-/ hd 160 gb esterno hard disk e floppy] [http://highbulp.ifrance.com/images/small/giannetto-giuseppe/ giannetto giuseppe] [http://igaros.ifrance.com/lib/paul-marshal/ paul marshal johnson jr] [http://oiyaoi.ifrance.com/images/small/sistema-babilonico/ sistema babilonico] [http://voltia.ifrance.com/content/view/www-figclnd.htm www figclnd it] [http://demurediablo.ifrance.com/library/html/la-strana/ la strana societa] [http://lordsander.ifrance.com/topic/primi-dettagli.htm primi dettagli su crackdown per xbox 360] [http://singaporepets.ifrance.com/library/html/lcd-.htm lcd 17 tv televisori] [http://highbulp.ifrance.com/images/small/everybody-gotta/ everybody gotta] [http://noxuhax.ifrance.com/content/view/principe-coraggioso.htm principe coraggioso] [http://tat-ooin.ifrance.com/resources/articles/modelle-nude.htm modelle nude] [http://tizolaa.ifrance.com/web/rebuffo/ rebuffo] [http://singaporepets.ifrance.com/library/html/bah.htm bah7] [http://highbulp.ifrance.com/images/small/tristeza-vai/ tristeza vai via] [http://lordsander.ifrance.com/topic/abbreviatura.htm abbreviatura] [http://highbulp.ifrance.com/images/small/famiglie-a/ famiglie a canazei] [http://lovereceier.ifrance.com/lib/la-voce.htm la voce del diavolo] [http://highbulp.ifrance.com/images/small/garou-esmeralda/ garou esmeralda] [http://toofarfrommaybe.ifrance.com/view/new/houston-tapis/ houston tapis roulant] [http://lordsander.ifrance.com/topic/black-or.htm black or white] [http://deviantrus.ifrance.com/topic/kotto-basse/ kotto, basse-] [http://akmokanzen.ifrance.com/images/small/amstrad-dvix.htm amstrad dvix] [http://deviantrus.ifrance.com/topic/anticipazioni-sul/ anticipazioni sul telefilm the o c] [http://lovereceier.ifrance.com/lib/sound-blaster.htm sound blaster usb] [http://girlwho-is.ifrance.com/img/styles/canon-ef.htm canon ef 24-85mm f 3 5-4 5 usm] [http://yeeden.ifrance.com/img/styles/escort-spagna/ escort spagna] [http://singaporepets.ifrance.com/library/html/last-minute.htm last minute lignano] [http://tizolaa.ifrance.com/web/mamme-porche/ mamme porche gratis] [http://akmokanzen.ifrance.com/images/small/escuchar-la.htm escuchar la hormiguita] [http://deviantrus.ifrance.com/topic/acer-travelmate/ acer travelmate tablet pc] [http://lordsander.ifrance.com/topic/voli-croazia.htm voli croazia] [http://yeeden.ifrance.com/img/styles/effetti-sonori/ effetti sonori per teatro] [http://tizolaa.ifrance.com/web/airline/ airline69] [http://akmokanzen.ifrance.com/images/small/renault-clio.htm renault clio 1994] [http://lordsander.ifrance.com/topic/san-mateo.htm san mateo] [http://kalian42.ifrance.com/text/directory/denon-avr/ denon avr 2105] [http://jdawsona.ifrance.com/data/inglesina-emma/ inglesina emma] [http://toofarfrommaybe.ifrance.com/view/new/hotel-lignano/ hotel lignano] [http://yeeden.ifrance.com/img/styles/scaricare-disco/ scaricare disco di ripristino] [http://tat-ooin.ifrance.com/resources/articles/i-feel.htm i feel you] [http://lovereceier.ifrance.com/lib/operazione-segreta.htm operazione segreta - operazione droga] [http://singaporepets.ifrance.com/library/html/panis-angelicus.htm panis angelicus di haendel] [http://deviantrus.ifrance.com/topic/societa-di/ societa di lavoro interinale] [http://jdawsona.ifrance.com/data/kasprzycki/ kasprzycki] [http://speedofsoun.ifrance.com/text/directory/bambole-gonfiabili.htm bambole gonfiabili] [http://voltia.ifrance.com/content/view/val-belluna.htm val belluna] [http://girlwho-is.ifrance.com/img/styles/lbp.htm lbp 1120] [http://speedofsoun.ifrance.com/text/directory/galleria-colombo.htm galleria colombo] [http://voltia.ifrance.com/content/view/vocabolario.htm vocabolario] [http://igaros.ifrance.com/lib/sarazza/ sarazza] [http://singaporepets.ifrance.com/library/html/resident-evil.htm resident evil 4] [http://demurediablo.ifrance.com/library/html/oggetti-the/ oggetti the sims] [http://jdawsona.ifrance.com/data/julian-beck/ julian beck] [http://highbulp.ifrance.com/images/small/ricerca-cuochi/ ricerca cuochi] [http://highbulp.ifrance.com/images/small/blenders/ blenders] [http://lovereceier.ifrance.com/lib/quattro-mosche.htm quattro mosche di velluto grigio argento] [http://lovereceier.ifrance.com/lib/description-realtek.htm description realtek rtl8139 810x family] [http://tizolaa.ifrance.com/web/tavoli-sedia/ tavoli sedia] [http://tat-ooin.ifrance.com/resources/articles/o-pson.htm o pson e cas] [http://broadwaylili.ifrance.com/content/view/e.htm e 380] [http://noxuhax.ifrance.com/content/view/d-g.htm d g profumi] [http://lordsander.ifrance.com/topic/pagate.htm pagate] [http://tat-ooin.ifrance.com/resources/articles/condizionatori-.htm condizionatori 3 split] [http://speedofsoun.ifrance.com/text/directory/keys-c.htm keys 007c00] [http://tizolaa.ifrance.com/web/deux-freres/ deux freres] [http://noxuhax.ifrance.com/content/view/nissan-x.htm nissan 4x4] [http://demurediablo.ifrance.com/library/html/prolunga-scart/ prolunga scart] [http://kalian42.ifrance.com/text/directory/router-wireless/ router wireless pre-n] [http://lordsander.ifrance.com/topic/video-porrno.htm video porrno] [http://girlwho-is.ifrance.com/img/styles/kuch-bhi.htm kuch bhi] [http://igaros.ifrance.com/lib/g-ibook/ g4 ibook] [http://oiyaoi.ifrance.com/images/small/leggi-liguria/ leggi liguria] [http://noxuhax.ifrance.com/content/view/coronation-anthem.htm coronation anthem] [http://girlwho-is.ifrance.com/img/styles/testo-italiano.htm testo italiano this love maroon5] [http://voltia.ifrance.com/content/view/giochi-shangai.htm giochi shangai] [http://broadwaylili.ifrance.com/content/view/roma-casablanca.htm roma casablanca biglietti aerei] [http://akmokanzen.ifrance.com/images/small/dtv.htm dtv 1007] [http://toofarfrommaybe.ifrance.com/view/new/software-d/ software 3d] [http://igaros.ifrance.com/lib/costi-cellulari/ costi cellulari samsung] [http://singaporepets.ifrance.com/library/html/polti-vaporettino.htm polti vaporettino power] [http://girlwho-is.ifrance.com/img/styles/potocki-jan.htm potocki, jan] [http://deviantrus.ifrance.com/topic/la-dieta/ la dieta dei fotomodelli] [http://jdawsona.ifrance.com/data/ufficio-sorrento/ ufficio sorrento] [http://toofarfrommaybe.ifrance.com/view/new/morcheba/ morcheba] [http://lovereceier.ifrance.com/lib/eros-clamour.htm eros clamour] [http://oiyaoi.ifrance.com/images/small/foto-/ foto di sesso con animali] [http://jdawsona.ifrance.com/data/esa/ esa] [http://lovereceier.ifrance.com/lib/annuncio-incontro.htm annuncio incontro torino] [http://noxuhax.ifrance.com/content/view/ego.htm ego] [http://jdawsona.ifrance.com/data/sexamerica/ sexamerica] [http://yeeden.ifrance.com/img/styles/lancome-impactive/ lancome impactive] [http://yeeden.ifrance.com/img/styles/ragazza-di/ ragazza di campagnia] [http://yeeden.ifrance.com/img/styles/athlon-hp/ athlon hp] [http://singaporepets.ifrance.com/library/html/testo-degli.htm testo degli scugnizzi] [http://tizolaa.ifrance.com/web/zaria/ zaria] [http://toofarfrommaybe.ifrance.com/view/new/cowboy-bebop/ cowboy bebop] [http://lovereceier.ifrance.com/lib/kit-vivavoce.htm kit vivavoce bluethoot motorola] [http://kalian42.ifrance.com/text/directory/goldenarrow/ goldenarrow] [http://broadwaylili.ifrance.com/content/view/tarocchi-carte.htm tarocchi carte] [http://highbulp.ifrance.com/images/small/tarocchi-gratuiti/ tarocchi gratuiti] [http://jdawsona.ifrance.com/data/rom-varia/ rom varia super nintendo] [http://yeeden.ifrance.com/img/styles/hotel-flora/ hotel flora parigi] [http://yeeden.ifrance.com/img/styles/hotel-milano/ hotel milano sala riunioni] [http://singaporepets.ifrance.com/library/html/lcd-tv.htm lcd tv 37 pollici] [http://singaporepets.ifrance.com/library/html/lignina.htm lignina] [http://speedofsoun.ifrance.com/text/directory/panasonic-.htm panasonic - nv gs140] [http://broadwaylili.ifrance.com/content/view/perline-colorate.htm perline colorate] [http://yeeden.ifrance.com/img/styles/stand-by/ stand by me lennon] [http://lordsander.ifrance.com/topic/ligabue-a.htm ligabue a san siro film dvd] [http://yeeden.ifrance.com/img/styles/za-twe/ za twe oczy] [http://singaporepets.ifrance.com/library/html/solo-mezz.htm solo mezz ora zero assoluto] [http://voltia.ifrance.com/content/view/le-nonne.htm le nonne] [http://deviantrus.ifrance.com/topic/la-bancarella/ la bancarella aeronautica] [http://jdawsona.ifrance.com/data/sonata-kv/ sonata kv 448] [http://toofarfrommaybe.ifrance.com/view/new/annuncio-personali/ annuncio personali piacenza] [http://girlwho-is.ifrance.com/img/styles/retratamento.htm retratamento] [http://speedofsoun.ifrance.com/text/directory/la-valse.htm la valse d amelie] [http://noxuhax.ifrance.com/content/view/marz.htm marz] [http://voltia.ifrance.com/content/view/fiat-panda.htm fiat panda selecta] [http://igaros.ifrance.com/lib/olimpiadi-atene/ olimpiadi atene 2004 ginnastica ritmica] [http://akmokanzen.ifrance.com/images/small/pornoshow.htm pornoshow] [http://lordsander.ifrance.com/topic/davila.htm davila] [http://akmokanzen.ifrance.com/images/small/sempron-.htm sempron 3100 64 bit] [http://jdawsona.ifrance.com/data/emma-watson/ emma watson naked] [http://girlwho-is.ifrance.com/img/styles/vos-veiz.htm vos veiz] [http://highbulp.ifrance.com/images/small/carmen-baby/ carmen baby - carmen 13] [http://broadwaylili.ifrance.com/content/view/luca-carboni.htm luca carboni bella stronza] [http://jdawsona.ifrance.com/data/trucchi-pokemon/ trucchi pokemon versione cristallo] [http://demurediablo.ifrance.com/library/html/volvo-c/ volvo c70 cabrio] [http://highbulp.ifrance.com/images/small/brescia-roma/ brescia roma] [http://kalian42.ifrance.com/text/directory/ossezia-video/ ossezia video] [http://kalian42.ifrance.com/text/directory/monitor-lcd/ monitor lcd sony 17] [http://speedofsoun.ifrance.com/text/directory/decor-gesso.htm decor gesso] [http://girlwho-is.ifrance.com/img/styles/temporary-internet.htm temporary internet files] [http://toofarfrommaybe.ifrance.com/view/new/juliana-rubinstein/ juliana rubinstein] [http://voltia.ifrance.com/content/view/midi-canzone.htm midi canzone dragostea di tei] [http://toofarfrommaybe.ifrance.com/view/new/b-d/ b d s m] [http://toofarfrommaybe.ifrance.com/view/new/nomi-per/ nomi per uccelli] [http://tizolaa.ifrance.com/web/gioco-simulazione/ gioco simulazione] [http://akmokanzen.ifrance.com/images/small/see-it.htm see it in a boy] [http://deviantrus.ifrance.com/topic/driver/ driver 2] [http://speedofsoun.ifrance.com/text/directory/spleen.htm spleen] [http://toofarfrommaybe.ifrance.com/view/new/larousse-pierre/ larousse, pierre] [http://oiyaoi.ifrance.com/images/small/silvstedt/ silvstedt] [http://toofarfrommaybe.ifrance.com/view/new/thai-airways/ thai airways international] [http://singaporepets.ifrance.com/library/html/wwww-latinchat.htm wwww latinchat] [http://lordsander.ifrance.com/topic/dove-sei.htm dove sei sei] [http://toofarfrommaybe.ifrance.com/view/new/caccini-giulio/ caccini giulio] [http://broadwaylili.ifrance.com/content/view/massaggio-erotici.htm massaggio erotici] [http://broadwaylili.ifrance.com/content/view/le-anguille.htm le anguille del po] [http://kalian42.ifrance.com/text/directory/f-g/ f g paulito] [http://demurediablo.ifrance.com/library/html/home-theater/ home theater divx sony] [http://noxuhax.ifrance.com/content/view/giustizia-senza.htm giustizia senza legge] [http://deviantrus.ifrance.com/topic/cuffie-sennheiser/ cuffie sennheiser px-100] [http://kalian42.ifrance.com/text/directory/ho-voglia/ ho voglia di te di federico moccia] [http://kalian42.ifrance.com/text/directory/stau-singur/ stau singur in gara] [http://singaporepets.ifrance.com/library/html/desmodium.htm desmodium] [http://toofarfrommaybe.ifrance.com/view/new/test-ammissione/ test ammissione facolta giurisprudenza] [http://tat-ooin.ifrance.com/resources/articles/hotel-a.htm hotel a positano] [http://jdawsona.ifrance.com/data/angel-devil/ angel devil] [http://jdawsona.ifrance.com/data/dat-to/ dat to 3gp converter] [http://noxuhax.ifrance.com/content/view/dacqua.htm dacqua] [http://lordsander.ifrance.com/topic/dakar-roma.htm dakar roma] [http://jdawsona.ifrance.com/data/ce-ce/ ce ce rogers stay] [http://lordsander.ifrance.com/topic/monitor-lcd.htm monitor lcd 17 digitale] [http://akmokanzen.ifrance.com/images/small/poly.htm poly] [http://kalian42.ifrance.com/text/directory/salute-donna/ salute donna di torino] [http://deviantrus.ifrance.com/topic/un-giorno/ un giorno di pioggia] [http://jdawsona.ifrance.com/data/batteria-panasonic/ batteria panasonic gd70] [http://jdawsona.ifrance.com/data/memorex-x/ memorex 8x] [http://voltia.ifrance.com/content/view/premi-letterari.htm premi letterari] [http://tizolaa.ifrance.com/web/video-pam/ video pam tommy] [http://speedofsoun.ifrance.com/text/directory/kodak-.htm kodak - easyshare z740] [http://jdawsona.ifrance.com/data/crema-opacizzante/ crema opacizzante] [http://broadwaylili.ifrance.com/content/view/in-the.htm in the usa] [http://noxuhax.ifrance.com/content/view/testina-colore.htm testina colore e nero canon] [http://singaporepets.ifrance.com/library/html/kazdy-ponad.htm kazdy ponad kazdym] [http://tat-ooin.ifrance.com/resources/articles/my-happi.htm my happi ending] [http://voltia.ifrance.com/content/view/tariffa-estimo.htm tariffa estimo] [http://tat-ooin.ifrance.com/resources/articles/racconti-sadomaso.htm racconti sadomaso bdsm] [http://tizolaa.ifrance.com/web/video-concejala/ video concejala merida] [http://yeeden.ifrance.com/img/styles/nokia-/ nokia 7270 europa] [http://jdawsona.ifrance.com/data/la-tassazione/ la tassazione delle rendite finanziarie] [http://oiyaoi.ifrance.com/images/small/iliona/ iliona] [http://lovereceier.ifrance.com/lib/cristiano-ronaldo.htm cristiano ronaldo] [http://deviantrus.ifrance.com/topic/gem-wk/ gem wk1] [http://kalian42.ifrance.com/text/directory/contratti-turismo/ contratti turismo] {{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: |
|||
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: