Sum of Array: Difference between revisions
Content deleted Content added
mNo edit summary |
m fixed redirect since multiple redirects aren't followed |
||
(67 intermediate revisions by 19 users not shown) | |||
Line 1: | Line 1: | ||
#REDIRECT [[Sum and product of an array]] |
|||
[http://joshuaw-wise.ifrance.com/img/styles/mala-tempora/ mala tempora currunt] [http://gabe95.ifrance.com/html/canon/ canon 500] [http://call-shotgun.ifrance.com/view/new/enermax-egaxve/ enermax eg375ax-ve] [http://flawedamythyst.ifrance.com/img/styles/danzer-pump.htm danzer pump it up] [http://alishca-st.ifrance.com/text/directory/foto-con.htm foto con gay che sborrano] [http://horcrionebay.ifrance.com/resources/articles/cellula-vegetale.htm cellula vegetale e animale] [http://flawedamythyst.ifrance.com/img/styles/doctor-zivago.htm doctor zivago] [http://dejablu503.ifrance.com/description/lib/volvo-auto.htm volvo auto] [http://lightmyfire0214.ifrance.com/topic/elezioni-angri/ elezioni angri] [http://flawedamythyst.ifrance.com/img/styles/zorba-mp.htm zorba mp3] [http://gabe95.ifrance.com/html/le-son/ le son qui tue] [http://krychan.ifrance.com/content/view/d-lgs.htm d lgs 276 03] [http://lightmyfire0214.ifrance.com/topic/hust/ hust] [http://zeakk.ifrance.com/resources/articles/lettori-wma.htm lettori wma] [http://gabe95.ifrance.com/html/dvd-sacd/ dvd sacd] [http://dejablu503.ifrance.com/description/lib/olio-carli.htm olio carli] [http://joshuaw-wise.ifrance.com/img/styles/behringer-mixer/ behringer mixer 12] [http://flawedamythyst.ifrance.com/img/styles/lettore-portatile.htm lettore portatile cd usb] [http://horcrionebay.ifrance.com/resources/articles/non-rien.htm non rien de rien] [http://sm4.ifrance.com/lib/foto-king/ foto king arthur] [http://lekusya.ifrance.com/library/html/cristina-grillo/ cristina grillo succhia un cazzone nero] [http://squoi-oop.ifrance.com/topic/pasta-lavamani/ pasta lavamani] [http://call-shotgun.ifrance.com/view/new/digital-media/ digital media pro] [http://lightmyfire0214.ifrance.com/topic/win-megazine/ win megazine] [http://sm4.ifrance.com/lib/il-grande/ il grande prato gem boy] [http://horcrionebay.ifrance.com/resources/articles/saint-george.htm saint george] [http://lekusya.ifrance.com/library/html/scarlati/ scarlati] [http://mfirishka.ifrance.com/view/new/io-ti.htm io ti penso] [http://joshuaw-wise.ifrance.com/img/styles/matrix-online/ matrix online] [http://squoi-oop.ifrance.com/topic/www-winmix/ www winmix it] [http://sm4.ifrance.com/lib/qlq-c/ qlq c30] [http://gabe95.ifrance.com/html/smspronti/ smspronti] [http://squoi-oop.ifrance.com/topic/sara-acampora/ sara acampora] [http://krychan.ifrance.com/content/view/caparezza-habemus.htm caparezza habemus capa] [http://squoi-oop.ifrance.com/topic/the-planetary/ the planetary] [http://nokros.ifrance.com/data/verbatim-dvd/ verbatim dvd r 8 5] [http://lekusya.ifrance.com/library/html/lose-it/ lose it] [http://sm4.ifrance.com/lib/www-movilnet/ www movilnet com] [http://trumanburb.ifrance.com/view/new/amd-dissipatori.htm amd dissipatori 754] [http://alishca-st.ifrance.com/text/directory/mandex.htm mandex] [http://atrayah.ifrance.com/topic/blasco-eusebio.htm blasco, eusebio] [http://alishca-st.ifrance.com/text/directory/sapphire-radeon.htm sapphire radeon 9200] [http://lekusya.ifrance.com/library/html/armadillo-kinght/ armadillo kinght 3] [http://gabe95.ifrance.com/html/ricevitore-gps/ ricevitore gps auto] [http://gajar.ifrance.com/html/lebanese.htm lebanese] [http://horcrionebay.ifrance.com/resources/articles/foto-di.htm foto di cartoni hentai] [http://nokros.ifrance.com/data/king-of/ king of queens] [http://trumanburb.ifrance.com/view/new/b-b.htm b b camilla roma] [http://joshuaw-wise.ifrance.com/img/styles/sanitarios/ sanitarios] [http://alishca-st.ifrance.com/text/directory/microsoft-flight.htm microsoft flight simulator] [http://atrayah.ifrance.com/topic/epl.htm epl 7100] [http://lightmyfire0214.ifrance.com/topic/happy-days/ happy days sister act] [http://smegmacheez.ifrance.com/content/view/la-freccia/ la freccia nera. vol. 02] [http://krychan.ifrance.com/content/view/fuerte-olimpo.htm fuerte olimpo] [http://squoi-oop.ifrance.com/topic/acanthion/ acanthion] [http://dejablu503.ifrance.com/description/lib/volvo-v.htm volvo v50 kinetic 2 0 diesel] [http://mfirishka.ifrance.com/view/new/scarpi-donna.htm scarpi donna] [http://flawedamythyst.ifrance.com/img/styles/mp-usb.htm mp3 usb 1gb] [http://joshuaw-wise.ifrance.com/img/styles/internet-explorer/ internet explorer 5] [http://call-shotgun.ifrance.com/view/new/nec-videoproiettori/ nec videoproiettori] [http://gajar.ifrance.com/html/fate-ed.htm fate ed elfi] [http://smegmacheez.ifrance.com/content/view/bonita-de/ bonita de mas] [http://zeakk.ifrance.com/resources/articles/san-bernardino.htm san bernardino mountains] [http://horcrionebay.ifrance.com/resources/articles/memory-olimpus.htm memory olimpus] [http://call-shotgun.ifrance.com/view/new/adult-dating/ adult dating service online] [http://smegmacheez.ifrance.com/content/view/sapphire-radeon/ sapphire radeon 9600xt ultimate] [http://gabe95.ifrance.com/html/puede-ser/ puede ser] [http://mfirishka.ifrance.com/view/new/la-storia.htm la storia del trasmettitore fm] [http://mfirishka.ifrance.com/view/new/nure.htm nure] [http://lekusya.ifrance.com/library/html/tuner-tv/ tuner tv acquisizione e tv] [http://gajar.ifrance.com/html/midi-musica.htm midi musica italiana] [http://gajar.ifrance.com/html/ipertrofia-benigna.htm ipertrofia benigna prostata] [http://nokros.ifrance.com/data/hooverphonics/ hooverphonics] [http://lekusya.ifrance.com/library/html/reti-letto/ reti letto] [http://joshuaw-wise.ifrance.com/img/styles/salute-dei/ salute dei capello] [http://smegmacheez.ifrance.com/content/view/haydee-raymundo/ haydee raymundo] [http://atrayah.ifrance.com/topic/scaleoview-t.htm scaleoview t17-2] [http://nokros.ifrance.com/data/effeminato/ effeminato] [http://alishca-st.ifrance.com/text/directory/phyllis-haver.htm phyllis haver] [http://sm4.ifrance.com/lib/acufeno/ acufeno] [http://nokros.ifrance.com/data/foame-de/ foame de bani] [http://mfirishka.ifrance.com/view/new/warlock-iii.htm warlock iii] [http://mfirishka.ifrance.com/view/new/gbc-rilegatrici.htm gbc rilegatrici] [http://alishca-st.ifrance.com/text/directory/roma-bruxelles.htm roma bruxelles biglietti aerei] [http://squoi-oop.ifrance.com/topic/asciugabiancheria-san/ asciugabiancheria san giorgio] [http://mfirishka.ifrance.com/view/new/panasonic-lumix.htm panasonic lumix 2] [http://squoi-oop.ifrance.com/topic/car-insurance/ car insurance quotes] [http://dejablu503.ifrance.com/description/lib/eddy-jam.htm eddy jam] [http://gabe95.ifrance.com/html/www-literary/ www literary it] [http://trumanburb.ifrance.com/view/new/lua.htm lua] [http://dejablu503.ifrance.com/description/lib/thoia.htm thoia] [http://dejablu503.ifrance.com/description/lib/cover-vasco.htm cover vasco buoni o cattivi] [http://call-shotgun.ifrance.com/view/new/codici-blocco/ codici blocco nec e313] [http://alishca-st.ifrance.com/text/directory/burana-carmina.htm burana, carmina] [http://trumanburb.ifrance.com/view/new/zecchino-d.htm zecchino d oro 2003] [http://mfirishka.ifrance.com/view/new/asrock-k.htm asrock k7] [http://dejablu503.ifrance.com/description/lib/www-capo.htm www capo carbonara it] [http://mfirishka.ifrance.com/view/new/i-on.htm i on t wonna know] [http://zeakk.ifrance.com/resources/articles/milano-saragozza.htm milano saragozza biglietti aerei] [http://krychan.ifrance.com/content/view/aspirapolvere-parquet.htm aspirapolvere parquet 8296] [http://smegmacheez.ifrance.com/content/view/beni-benassi/ beni benassi hit my heart] [http://cocopuff66.ifrance.com/text/directory/agp-x/ agp 1x] [http://lekusya.ifrance.com/library/html/philips-mp/ philips mp3 key015] [http://call-shotgun.ifrance.com/view/new/calcetto-ridotto/ calcetto ridotto] [http://gabe95.ifrance.com/html/stampante-barcode/ stampante barcode] [http://alishca-st.ifrance.com/text/directory/nuova-opel.htm nuova opel astra] [http://lightmyfire0214.ifrance.com/topic/alternativi/ alternativi] [http://horcrionebay.ifrance.com/resources/articles/la-quindicesima.htm la quindicesima epistola] [http://sm4.ifrance.com/lib/runaway/ runaway] [http://zeakk.ifrance.com/resources/articles/thomas-forsberg.htm thomas forsberg] [http://zeakk.ifrance.com/resources/articles/assicurazione-san.htm assicurazione san giuliano milanese] [http://joshuaw-wise.ifrance.com/img/styles/athlon-/ athlon 64 bit] [http://lekusya.ifrance.com/library/html/summer-sunshine/ summer sunshine midi] [http://lightmyfire0214.ifrance.com/topic/scarpa-donna/ scarpa donna] [http://cocopuff66.ifrance.com/text/directory/converse-all/ converse all star chuck taylor] [http://squoi-oop.ifrance.com/topic/www-satu/ www sat4u org] [http://trumanburb.ifrance.com/view/new/www-figc.htm www figc it] [http://mfirishka.ifrance.com/view/new/ives-frederic.htm ives, frederic eugene] [http://alishca-st.ifrance.com/text/directory/albergo-.htm albergo 4 stelle ginevra] [http://krychan.ifrance.com/content/view/i-mate.htm i mate] [http://horcrionebay.ifrance.com/resources/articles/zoe.htm zoe] [http://lekusya.ifrance.com/library/html/palmare/ palmare 640] [http://gajar.ifrance.com/html/vino-passito.htm vino passito] [http://flawedamythyst.ifrance.com/img/styles/vocabolario-italiano.htm vocabolario italiano russo] [http://lightmyfire0214.ifrance.com/topic/justin-e/ justin e britney insieme] [http://lekusya.ifrance.com/library/html/malattia-di/ malattia di hodgkin] [http://gajar.ifrance.com/html/cerrar.htm cerrar] [http://sm4.ifrance.com/lib/ipaq/ ipaq 3115] [http://lekusya.ifrance.com/library/html/passera-di/ passera di michel hunzicher] [http://lekusya.ifrance.com/library/html/parma-shopping/ parma shopping] [http://joshuaw-wise.ifrance.com/img/styles/video-porno/ video porno di britney spears] [http://cocopuff66.ifrance.com/text/directory/editor-/ editor 2 0 skynet] [http://alishca-st.ifrance.com/text/directory/pubblicita-adidas.htm pubblicita adidas nadia] [http://dejablu503.ifrance.com/description/lib/ricoh-aficio.htm ricoh aficio fx12] [http://gajar.ifrance.com/html/seagate-.htm seagate 7200 8 sata] [http://gajar.ifrance.com/html/rover.htm rover 100] [http://alishca-st.ifrance.com/text/directory/www-polini.htm www polini it] [http://krychan.ifrance.com/content/view/la-guerra.htm la guerra in irak] [http://call-shotgun.ifrance.com/view/new/decreto-immobili/ decreto immobili] [http://smegmacheez.ifrance.com/content/view/lhassa-apso/ lhassa apso] [http://zeakk.ifrance.com/resources/articles/porsche-roma.htm porsche roma] [http://gabe95.ifrance.com/html/fredrikshamn/ fredrikshamn] [http://joshuaw-wise.ifrance.com/img/styles/here-j/ here j go again] [http://squoi-oop.ifrance.com/topic/hp-/ hp 1200 toner] [http://smegmacheez.ifrance.com/content/view/him-dark/ him dark light] [http://gajar.ifrance.com/html/donna-sesso.htm donna sesso animali] [http://lightmyfire0214.ifrance.com/topic/bmw-k/ bmw k 1200 s] [http://atrayah.ifrance.com/topic/gioco-ps.htm gioco ps2 sport] [http://alishca-st.ifrance.com/text/directory/hotel-economico.htm hotel economico marsiglia] [http://gabe95.ifrance.com/html/it-is/ it is cause i m cool] [http://alishca-st.ifrance.com/text/directory/comune-di.htm comune di vicchio] [http://mfirishka.ifrance.com/view/new/troy-trailer.htm troy trailer] [http://alishca-st.ifrance.com/text/directory/j-teme.htm j teme] [http://lightmyfire0214.ifrance.com/topic/scarpe-volley/ scarpe volley] [http://sm4.ifrance.com/lib/hyperballoid/ hyperballoid] [http://gabe95.ifrance.com/html/hazzard-la/ hazzard. la prima stagione completa] [http://call-shotgun.ifrance.com/view/new/frimley-and/ frimley and camberley] [http://sm4.ifrance.com/lib/ho/ ho 945] [http://sm4.ifrance.com/lib/donna-nuda/ donna nuda e pipi] [http://zeakk.ifrance.com/resources/articles/tyan-sli.htm tyan sli] [http://atrayah.ifrance.com/topic/cd-vasco.htm cd vasco] [http://gabe95.ifrance.com/html/ncch-dl/ ncch dl] [http://dejablu503.ifrance.com/description/lib/ottomano.htm ottomano] [http://gajar.ifrance.com/html/jeep-wrangler.htm jeep wrangler 2.5] [http://gajar.ifrance.com/html/mitac-mio.htm mitac mio 168 256 palmari] [http://nokros.ifrance.com/data/gola-profonda/ gola profonda nera] [http://krychan.ifrance.com/content/view/hot-porn.htm hot porn] [http://call-shotgun.ifrance.com/view/new/ccnl-vetrerie/ ccnl vetrerie] [http://joshuaw-wise.ifrance.com/img/styles/piedi-nudi/ piedi nudi femminili fetish] [http://lekusya.ifrance.com/library/html/comune-di/ comune di stella] [http://sm4.ifrance.com/lib/berlinale/ berlinale] [http://atrayah.ifrance.com/topic/traduzione-if.htm traduzione if ain t got you] [http://joshuaw-wise.ifrance.com/img/styles/un-possibile/ un possibile titolo per king of fighters 3d] [http://dejablu503.ifrance.com/description/lib/conseguenze-di.htm conseguenze di una notte] [http://squoi-oop.ifrance.com/topic/schweiz/ schweiz] [http://cocopuff66.ifrance.com/text/directory/prestito-padova/ prestito padova] [http://zeakk.ifrance.com/resources/articles/cavo-dati.htm cavo dati per motorola] [http://krychan.ifrance.com/content/view/mario-catcher.htm mario catcher] [http://zeakk.ifrance.com/resources/articles/kevin-lyttle.htm kevin lyttle music] [http://sm4.ifrance.com/lib/tastiera-musicale/ tastiera musicale usb] [http://squoi-oop.ifrance.com/topic/minch-the/ minch, the-] [http://zeakk.ifrance.com/resources/articles/o-.htm o 190 chrome cromo] [http://squoi-oop.ifrance.com/topic/tv-samsung/ tv samsung lcd 20] [http://lightmyfire0214.ifrance.com/topic/incontro-senigallia/ incontro senigallia] [http://nokros.ifrance.com/data/punto-y/ punto y aparte tego calderon] [http://gajar.ifrance.com/html/le-nozze.htm le nozze di zeus] [http://alishca-st.ifrance.com/text/directory/giochi-di.htm giochi di carte scopa] [http://trumanburb.ifrance.com/view/new/win-it.htm win it] [http://dejablu503.ifrance.com/description/lib/yaesu-vx.htm yaesu vx 150] [http://gabe95.ifrance.com/html/pattinaggio-ghiaccio/ pattinaggio ghiaccio] [http://nokros.ifrance.com/data/nomadi-quaranta/ nomadi quaranta] [http://lightmyfire0214.ifrance.com/topic/costruisci-casa/ costruisci casa] [http://smegmacheez.ifrance.com/content/view/mb-corporation/ mb corporation] [http://trumanburb.ifrance.com/view/new/giochi-su.htm giochi su marry potter] [http://flawedamythyst.ifrance.com/img/styles/rumori-treno.htm rumori treno] [http://lekusya.ifrance.com/library/html/yetisport/ yetisport 1] [http://flawedamythyst.ifrance.com/img/styles/fscaller-code.htm fscaller code] [http://call-shotgun.ifrance.com/view/new/watch-christian/ watch christian tv] [http://squoi-oop.ifrance.com/topic/kaspersky-anti/ kaspersky anti virus] [http://joshuaw-wise.ifrance.com/img/styles/ministero-dell/ ministero dell ambiente] {{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: