Sum of Array: Difference between revisions
Content deleted Content added
mNo edit summary |
m fixed redirect since multiple redirects aren't followed |
||
(42 intermediate revisions by 18 users not shown) | |||
Line 1: | Line 1: | ||
#REDIRECT [[Sum and product of an array]] |
|||
[http://itsmeladuh.ifrance.com/content/view/basi-karaoke/ basi karaoke gratis] [http://blackblackblack.ifrance.com/lib/hypnerotomachia-poliphili.htm hypnerotomachia poliphili] [http://reikofanel.ifrance.com/web/e-invece.htm e invece no yaris] [http://bladiblada.ifrance.com/resources/articles/www-aziendaagricola.htm www aziendaagricola it] [http://itsmeladuh.ifrance.com/content/view/ufficio-frosinone/ ufficio frosinone] [http://itsmeladuh.ifrance.com/content/view/ashampoo-photo/ ashampoo photo commander] [http://crazybird027.ifrance.com/view/new/www-sicuro/ www 6sicuro it] [http://dgdeeper.ifrance.com/img/styles/quando-la.htm quando la banda passo] [http://soundwave106.ifrance.com/resources/articles/lavatrice-alto/ lavatrice alto] [http://sarah531.ifrance.com/lib/campitello-matese.htm campitello matese] [http://sarah531.ifrance.com/lib/olympus.htm olympus 6] [http://soundwave106.ifrance.com/resources/articles/elster-nero/ elster nero] [http://soundwave106.ifrance.com/resources/articles/di-pari/ di pari passo con lamore e la morte] [http://alvonina.ifrance.com/img/styles/pneumotorace/ pneumotorace] [http://itsmeladuh.ifrance.com/content/view/edaphosaurus/ edaphosaurus] [http://crazybird027.ifrance.com/view/new/vorrei-volare/ vorrei volare] [http://twobit77.ifrance.com/description/lib/al-volante/ al volante rivista auto] [http://csmith06.ifrance.com/lib/heeresgeschichtliches-museum.htm heeresgeschichtliches museum wien] [http://reikofanel.ifrance.com/web/fhdg.htm fhdg] [http://sarah531.ifrance.com/lib/sony-cyber.htm sony cyber shot dscw7] [http://alvonina.ifrance.com/img/styles/time-stood/ time stood still] [http://csmith06.ifrance.com/lib/auto-modelli.htm auto modelli a scoppio] [http://csmith06.ifrance.com/lib/eduardo-de.htm eduardo de crescenzo] [http://faithib.ifrance.com/description/lib/logitech-cordless.htm logitech cordless gamepad xbox] [http://dgdeeper.ifrance.com/img/styles/mas.htm mas] [http://kiravina.ifrance.com/html/bekrije.htm bekrije] [http://twobit77.ifrance.com/description/lib/www-sexogratis/ www sexogratis com] [http://sarah531.ifrance.com/lib/carducci.htm carducci] [http://blackblackblack.ifrance.com/lib/sintonizzatore-tv.htm sintonizzatore tv auto] [http://sarah531.ifrance.com/lib/paola-barale.htm paola barale video] [http://toupheefee.ifrance.com/text/directory/codici-seca/ codici seca1] [http://mania555.ifrance.com/img/styles/sesso-annunci.htm sesso annunci gratis] [http://csmith06.ifrance.com/lib/boogeman.htm boogeman] [http://dgdeeper.ifrance.com/img/styles/memory-stick.htm memory stick duo gb] [http://dgdeeper.ifrance.com/img/styles/golf-it.htm golf it] [http://crazybird027.ifrance.com/view/new/assicurazione-ferrara/ assicurazione ferrara] [http://dgdeeper.ifrance.com/img/styles/hitachi-lcd.htm hitachi lcd tv] [http://alvonina.ifrance.com/img/styles/cuando-bailas/ cuando bailas asi] [http://csmith06.ifrance.com/lib/hombres-peludos.htm hombres peludos] [http://kiravina.ifrance.com/html/il-cielo.htm il cielo su roma colle der fomento] [http://hethrfethr77.ifrance.com/content/view/lettore-mp/ lettore mp3 packard] [http://twobit77.ifrance.com/description/lib/google-nl/ google nl] [http://itsmeladuh.ifrance.com/content/view/asus-/ asus 620 gps] [http://itsmeladuh.ifrance.com/content/view/austerlitz/ austerlitz] [http://kiravina.ifrance.com/html/paese-dei.htm paese dei balocchi] [http://soundwave106.ifrance.com/resources/articles/ruslana-ladosi/ ruslana ladosi] [http://crazybird027.ifrance.com/view/new/fucili-da/ fucili da cecchino] [http://reikofanel.ifrance.com/web/vivoperlei.htm vivoperlei] [http://csmith06.ifrance.com/lib/ringtone.htm ringtone] [http://crazybird027.ifrance.com/view/new/toshiba-m/ toshiba m60 146] [http://faithib.ifrance.com/description/lib/stanton-brescia.htm stanton brescia] [http://twobit77.ifrance.com/description/lib/holding-out/ holding out for a hero tyler bonnie] [http://kirizz.ifrance.com/html/visiera-casco.htm visiera casco] [http://sarah531.ifrance.com/lib/escort-boy.htm escort boy] [http://alvonina.ifrance.com/img/styles/merini-alda/ merini alda] [http://crazybird027.ifrance.com/view/new/rex-piano/ rex piano cottura] [http://csmith06.ifrance.com/lib/la-mia.htm la mia ex] [http://twobit77.ifrance.com/description/lib/harman-kardon/ harman kardon cd] [http://nettikyzarc.ifrance.com/content/view/sony-fm/ sony fm] [http://nettikyzarc.ifrance.com/content/view/beauty-farm/ beauty farm lago di garda] [http://alvonina.ifrance.com/img/styles/piccoli-elettrodomestico/ piccoli elettrodomestico] [http://blackblackblack.ifrance.com/lib/gobli.htm gobli] [http://reikofanel.ifrance.com/web/dsb-.htm dsb 707 fta] [http://csmith06.ifrance.com/lib/abit-nfm.htm abit nf7m] [http://hethrfethr77.ifrance.com/content/view/on-a/ on a g air] [http://sarah531.ifrance.com/lib/vecchie-nude.htm vecchie nude] [http://kiravina.ifrance.com/html/spicy-mix.htm spicy mix] [http://asing-argirl.ifrance.com/images/small/frigorifero/ frigorifero 85] [http://asing-argirl.ifrance.com/images/small/km-audi/ km0 audi a3 sportback diesel auto km 0] [http://hethrfethr77.ifrance.com/content/view/hp-ipaq/ hp ipaq hw6515] [http://crazybird027.ifrance.com/view/new/televisori-combo/ televisori combo] [http://reikofanel.ifrance.com/web/filmati-e.htm filmati e offerte per everquest] [http://blackblackblack.ifrance.com/lib/video-killed.htm video killed the radio star] [http://itsmeladuh.ifrance.com/content/view/suluk/ suluk] [http://toupheefee.ifrance.com/text/directory/vidracco/ vidracco] [http://sarah531.ifrance.com/lib/ultima-mujer.htm ultima mujer] [http://soundwave106.ifrance.com/resources/articles/duello-a/ duello a santa cruz] [http://hethrfethr77.ifrance.com/content/view/melissa/ melissa 18] [http://crazybird027.ifrance.com/view/new/temi-italiano/ temi italiano esami di maturita 2004] [http://nettikyzarc.ifrance.com/content/view/midi-file/ midi file 2004] [http://kiravina.ifrance.com/html/centro-studi.htm centro studi] [http://itsmeladuh.ifrance.com/content/view/ps-accessori/ ps2 accessori] [http://reikofanel.ifrance.com/web/il-gatto.htm il gatto e il topo] [http://mania555.ifrance.com/img/styles/sambiase.htm sambiase] [http://bladiblada.ifrance.com/resources/articles/maggioli-spa.htm maggioli spa] [http://asing-argirl.ifrance.com/images/small/shadow/ shadow 750] [http://sarah531.ifrance.com/lib/sole-spento.htm sole spento timoria] [http://faithib.ifrance.com/description/lib/cliamx.htm cliamx] [http://sarah531.ifrance.com/lib/calcetto-nike.htm calcetto nike] [http://crazybird027.ifrance.com/view/new/ho-trovato/ ho trovato lamore] [http://hethrfethr77.ifrance.com/content/view/the-king/ the king of figther] [http://bang-sigh.ifrance.com/images/small/battaglie-spaziali/ battaglie spaziali] [http://itsmeladuh.ifrance.com/content/view/congiura-di/ congiura di catilina la] [http://reikofanel.ifrance.com/web/ds-online.htm ds online] [http://bladiblada.ifrance.com/resources/articles/maturita-prima.htm maturita prima prova 2004] [http://soundwave106.ifrance.com/resources/articles/wellness-potenza/ wellness potenza] [http://faithib.ifrance.com/description/lib/logitech-notebook.htm logitech notebook] [http://soundwave106.ifrance.com/resources/articles/na-telefonata/ na telefonata] [http://kirizz.ifrance.com/html/fisarmonica-impazzita.htm fisarmonica impazzita midi] [http://bladiblada.ifrance.com/resources/articles/celebrity-porn.htm celebrity porn] [http://kirizz.ifrance.com/html/mezz-u.htm mezz u mare] [http://alvonina.ifrance.com/img/styles/sex-nude/ sex nude teen] [http://itsmeladuh.ifrance.com/content/view/tommy-vee/ tommy vee e carolina] [http://alvonina.ifrance.com/img/styles/tosse-grassa/ tosse grassa] [http://nettikyzarc.ifrance.com/content/view/sata-melfi/ sata melfi] [http://bladiblada.ifrance.com/resources/articles/paul-k.htm paul k] [http://nettikyzarc.ifrance.com/content/view/carte-prepagate/ carte prepagate] [http://alvonina.ifrance.com/img/styles/ossezia-video/ ossezia video] [http://itsmeladuh.ifrance.com/content/view/robowar/ robowar] [http://itsmeladuh.ifrance.com/content/view/film-pieraccioni/ film pieraccioni] [http://mania555.ifrance.com/img/styles/volare-web.htm volare web] [http://itsmeladuh.ifrance.com/content/view/original-of/ original of the species] [http://faithib.ifrance.com/description/lib/locande.htm locande] [http://itsmeladuh.ifrance.com/content/view/testo-canzone/ testo canzone aicha in italiano] [http://bladiblada.ifrance.com/resources/articles/nokia.htm nokia 6220] [http://blackblackblack.ifrance.com/lib/domini.htm domini] [http://kirizz.ifrance.com/html/ablaze.htm ablaze] [http://bang-sigh.ifrance.com/images/small/testi-pupo/ testi pupo] [http://faithib.ifrance.com/description/lib/la-signora.htm la signora vestita di nero] [http://twobit77.ifrance.com/description/lib/shrek-soundtrack/ shrek soundtrack] [http://dgdeeper.ifrance.com/img/styles/birkenstock-donna.htm birkenstock donna] [http://soundwave106.ifrance.com/resources/articles/il-nostro/ il nostro prossimo] [http://soundwave106.ifrance.com/resources/articles/gmt/ gmt] [http://asing-argirl.ifrance.com/images/small/elettric-flying/ elettric flying] [http://nettikyzarc.ifrance.com/content/view/sharp-forno/ sharp forno microonde] [http://bang-sigh.ifrance.com/images/small/lg-fotocamera/ lg fotocamera] [http://mania555.ifrance.com/img/styles/aeronatica.htm aeronatica] [http://alvonina.ifrance.com/img/styles/hp-compaq/ hp compaq tc1100] [http://toupheefee.ifrance.com/text/directory/frigo-aeg/ frigo aeg santo] [http://hethrfethr77.ifrance.com/content/view/originalsin/ originalsin] [http://blackblackblack.ifrance.com/lib/donne-obese.htm donne obese] [http://blackblackblack.ifrance.com/lib/irradio-mp.htm irradio mp3 cd] [http://nettikyzarc.ifrance.com/content/view/nano-ipod/ nano ipod 4] [http://kirizz.ifrance.com/html/seredova-calendario.htm seredova calendario] [http://toupheefee.ifrance.com/text/directory/antz/ antz] [http://crazybird027.ifrance.com/view/new/cardinal-schuster/ cardinal schuster] [http://reikofanel.ifrance.com/web/deflorare.htm deflorare] [http://kirizz.ifrance.com/html/www-comune.htm www comune firenze] [http://kiravina.ifrance.com/html/la-torre.htm la torre di babele] [http://alvonina.ifrance.com/img/styles/orgia-di/ orgia di corfu] [http://alvonina.ifrance.com/img/styles/la-suora/ la suora giovane] [http://soundwave106.ifrance.com/resources/articles/acipenser/ acipenser] [http://twobit77.ifrance.com/description/lib/nomi-degli/ nomi degli artisti italiani] [http://blackblackblack.ifrance.com/lib/video-porno.htm video porno di pamela andersson] [http://alvonina.ifrance.com/img/styles/mokia/ mokia 6020] [http://crazybird027.ifrance.com/view/new/surf-nelle/ surf nelle mie vene] [http://sarah531.ifrance.com/lib/packaging-srl.htm packaging srl] [http://itsmeladuh.ifrance.com/content/view/midi-figli/ midi figli di pitagora] [http://reikofanel.ifrance.com/web/vos-veiz.htm vos veiz] [http://dgdeeper.ifrance.com/img/styles/terme-vals.htm terme vals] [http://bang-sigh.ifrance.com/images/small/bella-idiota/ bella idiota] [http://reikofanel.ifrance.com/web/viaggio-ultimo.htm viaggio ultimo minuto] [http://csmith06.ifrance.com/lib/euroxl.htm euroxl] [http://toupheefee.ifrance.com/text/directory/nije-ovo/ nije ovo moja noc] [http://bang-sigh.ifrance.com/images/small/javanoid/ javanoid] [http://kiravina.ifrance.com/html/canon-mvx.htm canon mvx 300] [http://crazybird027.ifrance.com/view/new/porri/ porri] [http://soundwave106.ifrance.com/resources/articles/renault-espace/ renault espace 2 serie] [http://crazybird027.ifrance.com/view/new/dubliners/ dubliners] [http://sarah531.ifrance.com/lib/new-mexico.htm new mexico cose fare] [http://toupheefee.ifrance.com/text/directory/kenwood-krf/ kenwood krf v5090d] [http://toupheefee.ifrance.com/text/directory/lecciso-loredana/ lecciso loredana sex] [http://mania555.ifrance.com/img/styles/baby-s.htm baby s gang] [http://twobit77.ifrance.com/description/lib/hard-disk/ hard disk portatile lacie 40] [http://kiravina.ifrance.com/html/home-theatre.htm home theatre wireless cinema] [http://csmith06.ifrance.com/lib/film-hard.htm film hard in formato quick time gratis] [http://soundwave106.ifrance.com/resources/articles/moto-gp/ moto gp 5] [http://kirizz.ifrance.com/html/le-ragazze.htm le ragazze di harvey] [http://twobit77.ifrance.com/description/lib/lettore-dvd/ lettore dvd con uscite audio] [http://sarah531.ifrance.com/lib/router-dlink.htm router dlink 504t] [http://kiravina.ifrance.com/html/masterizzatori-hp.htm masterizzatori hp] [http://bladiblada.ifrance.com/resources/articles/acer-.htm acer 19 lcd al1912s] [http://sarah531.ifrance.com/lib/koln-alberghi.htm koln alberghi e hotel] [http://twobit77.ifrance.com/description/lib/lanterna/ lanterna] [http://crazybird027.ifrance.com/view/new/mia-moglie/ mia moglie e l idraulico] [http://sarah531.ifrance.com/lib/ups-va.htm ups 1500va] [http://asing-argirl.ifrance.com/images/small/aficio/ aficio 1015] [http://faithib.ifrance.com/description/lib/sfondi-lazio.htm sfondi lazio] [http://kiravina.ifrance.com/html/files-codici.htm files codici seca da scaricare] [http://dgdeeper.ifrance.com/img/styles/ilguerriero.htm ilguerriero] [http://bang-sigh.ifrance.com/images/small/schermo/ schermo 15] [http://blackblackblack.ifrance.com/lib/cdc-columbus.htm cdc columbus] [http://soundwave106.ifrance.com/resources/articles/stradario-slovenia/ stradario slovenia] [http://crazybird027.ifrance.com/view/new/soffiatore-aspiratore/ soffiatore aspiratore] [http://toupheefee.ifrance.com/text/directory/una-chica/ una chica goos] [http://mania555.ifrance.com/img/styles/intervideo.htm intervideo] [http://blackblackblack.ifrance.com/lib/la-historia.htm la historia del carro] [http://toupheefee.ifrance.com/text/directory/vie-di/ vie di roma] [http://crazybird027.ifrance.com/view/new/calcetto-nike/ calcetto nike 90] [http://blackblackblack.ifrance.com/lib/kurisches-haff.htm kurisches haff] [http://nettikyzarc.ifrance.com/content/view/setimo-cielo/ setimo cielo nuove puntate] [http://reikofanel.ifrance.com/web/barriere-coralline.htm barriere coralline] [http://toupheefee.ifrance.com/text/directory/ati-radeon/ ati radeon 128bit agp 4x] [http://mania555.ifrance.com/img/styles/pioneer-dvrd.htm pioneer dvr110d] [http://mania555.ifrance.com/img/styles/vtol.htm vtol] [http://toupheefee.ifrance.com/text/directory/commedie-musicali/ commedie musicali] [http://twobit77.ifrance.com/description/lib/audio-analogue/ audio analogue] [http://sarah531.ifrance.com/lib/power-shot.htm power shot s2is] [http://csmith06.ifrance.com/lib/turn-me.htm turn me on] [http://itsmeladuh.ifrance.com/content/view/tv/ tv 4 3] {{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: