Sum of Array: Difference between revisions
Content deleted Content added
mNo edit summary |
m fixed redirect since multiple redirects aren't followed |
||
(31 intermediate revisions by 18 users not shown) | |||
Line 1: | Line 1: | ||
#REDIRECT [[Sum and product of an array]] |
|||
[http://kimccanl.angelfire.com/manzoni-e.htm manzoni e il seicento] [http://badanglican.cn/content/view/cuffie-con/ cuffie con microfono] [http://badanglican.cn/content/view/athlon-xp/ athlon xp 2600] [http://grhenric.angelfire.com/html/olivetti-any/ olivetti any way] [http://badanglican.cn/content/view/moddy-blue/ moddy blue] [http://lubrevel.angelfire.com/new/i-banditi/ i banditi del petrolio] [http://prhaffen.angelfire.com/library/tre-soldi.htm tre soldi nella fontana] [http://rohutsonik.angelfire.com/blog/www-frigerioviaggi.htm www frigerioviaggi it] [http://azagtot.netfirms.com/blog/ditte-edili.htm ditte edili carpenteria] [http://grhenric.angelfire.com/html/chiavi-bussola/ chiavi bussola] [http://whrichey.angelfire.com/content/window-washer/ window washer] [http://rospear.angelfire.com/styles/testo-hilary/ testo hilary duff] [http://merengo.netfirms.com/html/www-rainbowsix.htm www rainbowsix com] [http://jite.netfirms.com/new/antonini-srl/ antonini srl] [http://prihodnaja.fortunecity.com/small/dantista.htm dantista] [http://badanglican.cn/content/view/abiti-in/ abiti in pelle e renna] [http://zaweaver.angelfire.com/html/guardare-moglie/ guardare moglie] [http://badanglican.cn/content/view/gsm-asti/ gsm asti] [http://grhenric.angelfire.com/html/mustek-dv/ mustek dv 5500] [http://mastrong.angelfire.com/articles/sony-plasma.htm sony plasma 1024x768] [http://badanglican.cn/content/view/rivista-gq/ rivista gq] [http://hilyndel.angelfire.com/lib/roccamena.htm roccamena] [http://jite.netfirms.com/new/forze-armate/ forze armate] [http://kehampsh.angelfire.com/view/porsche-/ porsche 911 gt3] [http://themarjorie.netfirms.com/la-vita.htm la vita e nek] [http://badanglican.cn/content/view/fujifilm-s/ fujifilm s7000] [http://knbrenne.angelfire.com/data/vaporizzatore-polti.htm vaporizzatore polti] [http://rohutsonik.angelfire.com/blog/acer-n.htm acer n35 gps integrato] [http://merengo.netfirms.com/html/da-svideo.htm da svideo a vga] [http://zaweaver.angelfire.com/html/volkswagen-polo/ volkswagen polo tdi] [http://cacogdil.angelfire.com/web/ati-.htm ati 256 ram] [http://diferente.netfirms.com/view/pone.htm pone] [http://diferente.netfirms.com/view/scheda-video.htm scheda video 9800 256] [http://mastrong.angelfire.com/articles/lifo.htm lifo] [http://diferente.netfirms.com/view/classe-di.htm classe di centrifugazione] [http://whrichey.angelfire.com/content/cinema-frascati/ cinema frascati] [http://azagtot.netfirms.com/blog/inventury.htm inventury] [http://whrichey.angelfire.com/content/dvd-recorder/ dvd recorder con videoregistratore vhs] [http://kehampsh.angelfire.com/view/foto-king/ foto king arthur] [http://domiver.angelfire.com/data/heaven-s.htm heaven s door] [http://grhenric.angelfire.com/html/revenge/ revenge 2] [http://azagtot.netfirms.com/blog/segreteria-brondi.htm segreteria brondi] [http://kimccanl.angelfire.com/seth-godin.htm seth godin permission marketing] [http://badanglican.cn/content/view/powerdvd/ powerdvd 5] [http://diferente.netfirms.com/view/karena-cinta.htm karena cinta delon] [http://badanglican.cn/content/view/hoara-borselli/ hoara borselli] [http://eiwiggin.angelfire.com/small/tanga-donna/ tanga donna] [http://hungry87.netfirms.com/styles/power-pete/ power pete] [http://cacogdil.angelfire.com/web/usb-kvm.htm usb kvm] [http://badanglican.cn/content/view/router-belkin/ router belkin pre-n] [http://mastrong.angelfire.com/articles/zaino-rigido.htm zaino rigido] [http://xrikuxsorax103.netfirms.com/apple-/ apple 23 lcd] [http://rohutsonik.angelfire.com/blog/polineuropatia-alcolica.htm polineuropatia alcolica] [http://domiver.angelfire.com/data/dr-hook.htm dr hook greatest hits] [http://prhaffen.angelfire.com/library/gyno-play.htm gyno play mamma] [http://cacogdil.angelfire.com/web/la-maniera.htm la maniera di amare] [http://prhaffen.angelfire.com/library/him-pretending.htm him pretending] [http://prhaffen.angelfire.com/library/kamikaze.htm kamikaze] [http://badanglican.cn/content/view/sio-gucci/ sio gucci] [http://prihodnaja.fortunecity.com/small/www-ucraina.htm www ucraina com] [http://badanglican.cn/content/view/immagini-su/ immagini su capelli] [http://michelletrach.netfirms.com/shemale-cumshot.htm shemale cumshot] [http://themarjorie.netfirms.com/gametofito.htm gametofito] [http://zaweaver.angelfire.com/html/foto-da/ foto da bambola con v marini] [http://prihodnaja.fortunecity.com/small/acer-alms.htm acer al1714ms 17] [http://diferente.netfirms.com/view/passat-.htm passat 2 tdi] [http://piratical-map.netfirms.com/lib/acer-/ acer hd portatile esterno 2 5 80gb] [http://kehampsh.angelfire.com/view/amd-athlon/ amd athlon xp processor powernow] [http://badanglican.cn/content/view/karmina/ karmina] [http://rospear.angelfire.com/styles/going-home/ going home] [http://merengo.netfirms.com/html/organizzazione-convegno.htm organizzazione convegno] [http://prihodnaja.fortunecity.com/small/sorgenti-fiume.htm sorgenti fiume reno] [http://syashwor.angelfire.com/directory/canon-cp.htm canon cp600] [http://xrikuxsorax103.netfirms.com/qualcosa-striscia/ qualcosa striscia nel buio] [http://broken-lens.netfirms.com/web/miliardi/ miliardi] [http://myasa-kusochek.netfirms.com/lib/nike-air/ nike air max silver donna] [http://badanglican.cn/content/view/web-design/ web design udine] [http://mastrong.angelfire.com/articles/videoproiettore-x.htm videoproiettore 1280x1024] [http://badanglican.cn/content/view/flash-pen/ flash pen drive memory card] [http://badanglican.cn/content/view/hot-winter/ hot winter party] [http://prihodnaja.fortunecity.com/small/junagadh.htm junagadh] [http://prihodnaja.fortunecity.com/small/pierangelo-sequeri.htm pierangelo sequeri] [http://prihodnaja.fortunecity.com/small/ingegnere-civile.htm ingegnere civile (regione lombardia varese provincia)] [http://diferente.netfirms.com/view/viacess-keys.htm viacess keys] [http://badanglican.cn/content/view/asus-pgdcv/ asus p5gdc-v] [http://lubrevel.angelfire.com/new/asus/ asus 64] [http://badanglican.cn/content/view/philips-pw/ philips 32pw9520 12] [http://badanglican.cn/content/view/mutuo-fineco/ mutuo fineco] [http://prihodnaja.fortunecity.com/small/showgood-com.htm showgood com] [http://mastrong.angelfire.com/articles/hanz-ebson.htm hanz ebson] [http://zaweaver.angelfire.com/html/it-s/ it s ok it s alright] [http://azagtot.netfirms.com/blog/additivi-per.htm additivi per alimenti e bevande] [http://mastrong.angelfire.com/articles/bosna.htm bosna] [http://prihodnaja.fortunecity.com/small/genova-annunci.htm genova annunci] [http://badanglican.cn/content/view/scope-elettriche/ scope elettriche delonghi] [http://michelletrach.netfirms.com/sigma-ex.htm sigma ex 18 50] [http://webancks.angelfire.com/harvest-opeth.htm harvest opeth] [http://mybabydontsleep.netfirms.com/text/koda-kumi.htm koda kumi] [http://michelletrach.netfirms.com/treno-parigi.htm treno parigi] [http://prihodnaja.fortunecity.com/small/attila-il.htm attila il flagello di dio film dvd] [http://rospear.angelfire.com/styles/www-tecnocasa/ www tecnocasa it] [http://prihodnaja.fortunecity.com/small/asus-wn.htm asus w3400n] [http://prihodnaja.fortunecity.com/small/canon-powershot.htm canon powershot obiettivi] [http://prihodnaja.fortunecity.com/small/sexo-con.htm sexo con viejas] [http://badanglican.cn/content/view/gamesa-energia/ gamesa energia italia] [http://mybabydontsleep.netfirms.com/text/technisat-skystar.htm technisat skystar 2 tv] [http://prihodnaja.fortunecity.com/small/sibel-can.htm sibel can] [http://badanglican.cn/content/view/video-madonna/ video madonna] [http://domiver.angelfire.com/data/stefania-fioravanti.htm stefania fioravanti] [http://azagtot.netfirms.com/blog/software-mixer.htm software mixer demo] [http://eiwiggin.angelfire.com/small/cald-aie/ cald aie] [http://rohutsonik.angelfire.com/blog/scaleo-p.htm scaleo p siemens] [http://xrikuxsorax103.netfirms.com/xfiles-a/ xfiles a quota sei] [http://rospear.angelfire.com/styles/hotel-st/ hotel st moritz] [http://prihodnaja.fortunecity.com/small/racconti-marito.htm racconti marito cornuto] [http://prihodnaja.fortunecity.com/small/tiscali-net.htm tiscali net] [http://grhenric.angelfire.com/html/klinger-friedrich/ klinger friedrich maximilian] [http://blgottlo.angelfire.com/excel-/ excel 2002 oltre ogni limite libri] [http://hungry87.netfirms.com/styles/jogin/ jogin] [http://syashwor.angelfire.com/directory/diesel-tshirt.htm diesel tshirt uomo] [http://prihodnaja.fortunecity.com/small/cavargna.htm cavargna] [http://kimccanl.angelfire.com/canzone-ciapa.htm canzone ciapa la galeina] [http://badanglican.cn/content/view/plasma/ plasma] [http://badanglican.cn/content/view/this-love/ this love marron 4] [http://diferente.netfirms.com/view/mp-a.htm mp3 a mpeg2] [http://hilyndel.angelfire.com/lib/ttr.htm ttr 900] [http://badanglican.cn/content/view/quarta-isola/ quarta isola] [http://prihodnaja.fortunecity.com/small/lettori-slim.htm lettori slim] [http://prihodnaja.fortunecity.com/small/indiana-jone.htm indiana jone] [http://badanglican.cn/content/view/stampa-diretta/ stampa diretta su cd o dvd] [http://prihodnaja.fortunecity.com/small/amstrad-dx.htm amstrad dx3020] [http://badanglican.cn/content/view/zafira-km/ zafira km 0] [http://prihodnaja.fortunecity.com/small/racconti-thailandia.htm racconti thailandia] [http://rohutsonik.angelfire.com/blog/renato-carosone.htm renato carosone] [http://themarjorie.netfirms.com/zirconio.htm zirconio] [http://badanglican.cn/content/view/libri-conversazione/ libri conversazione con dio] [http://syashwor.angelfire.com/directory/il-nuovo.htm il nuovo singolo di gigi d alessio] [http://azagtot.netfirms.com/blog/i-goblin.htm i goblin libri] [http://rohutsonik.angelfire.com/blog/gioco-delle.htm gioco delle punizioni] [http://badanglican.cn/content/view/zen-lettore/ zen lettore mp3] [http://domiver.angelfire.com/data/mojoj-majci.htm mojoj majci] [http://zaweaver.angelfire.com/html/junior-jack/ junior jack] [http://diferente.netfirms.com/view/decoder-digitale.htm decoder digitale terrestre samsung] [http://prihodnaja.fortunecity.com/small/femmina-nude.htm femmina nude] [http://badanglican.cn/content/view/mia-moglie/ mia moglie, un corpo per lamore] [http://badanglican.cn/content/view/gps-bluetooth/ gps bluetooth garmin] [http://eiwiggin.angelfire.com/small/myimmortal-mid/ myimmortal mid] [http://badanglican.cn/content/view/www-pantel/ www pantel com pe] [http://broken-lens.netfirms.com/web/nini-d/ nini d angelo lyrics] [http://piratical-map.netfirms.com/lib/a-divinis/ a divinis] [http://kehampsh.angelfire.com/view/cartina-fisica/ cartina fisica toscana] [http://badanglican.cn/content/view/vino-shiraz/ vino shiraz] [http://rospear.angelfire.com/styles/polar-cscad/ polar cs200cad] [http://syashwor.angelfire.com/directory/inno-dei.htm inno dei paracadutisti] [http://badanglican.cn/content/view/canon-powershot/ canon powershot s2 is fotocamere] [http://themarjorie.netfirms.com/lyoid.htm lyoid] [http://zaweaver.angelfire.com/html/lavasciuga-/ lavasciuga 40 cm] [http://zaweaver.angelfire.com/html/bmw-b/ bmw b10] [http://piratical-map.netfirms.com/lib/cefalea/ cefalea] [http://rospear.angelfire.com/styles/honda-/ honda accord 18i 16v s] [http://knbrenne.angelfire.com/data/nokia-.htm nokia 6680 megapixel] [http://badanglican.cn/content/view/come-tu/ come tu mi vuoi alexia] [http://prihodnaja.fortunecity.com/small/accessori-mio.htm accessori mio168] [http://badanglican.cn/content/view/www-playstationonline/ www playstationonline it] [http://badanglican.cn/content/view/imballaggio-legno/ imballaggio legno] [http://badanglican.cn/content/view/pe/ pe 7800] [http://lubrevel.angelfire.com/new/cazzi-pelosi/ cazzi pelosi] [http://piratical-map.netfirms.com/lib/donnne-famose/ donnne famose in perizoma] [http://hungry87.netfirms.com/styles/flight-of/ flight of the bumblee bee] [http://rospear.angelfire.com/styles/pesero/ pesero] [http://myasa-kusochek.netfirms.com/lib/london-town/ london town] [http://badanglican.cn/content/view/samsung-rt/ samsung rt-52] [http://kimccanl.angelfire.com/wisiny-yandel.htm wisiny yandel] [http://piratical-map.netfirms.com/lib/pda-phone/ pda phone s100] [http://prihodnaja.fortunecity.com/small/lotto-estrazione.htm lotto estrazione 26 05 04] [http://hilyndel.angelfire.com/lib/on-the.htm on the go copy box] [http://merengo.netfirms.com/html/hp-.htm hp 9000 dn] [http://rospear.angelfire.com/styles/la-corazzata/ la corazzata deve saltare] [http://prihodnaja.fortunecity.com/small/biglietti-allenamenti.htm biglietti allenamenti olimpiadi] [http://webancks.angelfire.com/dire-sreet.htm dire sreet] [http://syashwor.angelfire.com/directory/materasso-gonfiabile.htm materasso gonfiabile intex] [http://prihodnaja.fortunecity.com/small/whirlpool.htm whirlpool 3590] [http://rohutsonik.angelfire.com/blog/www-libro.htm www libro amico it] [http://prhaffen.angelfire.com/library/o-zone.htm o zone dragostea den tel] [http://webancks.angelfire.com/hp-compaq.htm hp compaq dx2000] [http://hungry87.netfirms.com/styles/caribe-mix/ caribe mix 2004] [http://lubrevel.angelfire.com/new/testi-album/ testi album michael buble] [http://piratical-map.netfirms.com/lib/followed-the/ followed the waves] [http://whrichey.angelfire.com/content/www-uniparthenope/ www uniparthenope it] [http://badanglican.cn/content/view/santini-srl/ santini srl 121 ar] [http://eiwiggin.angelfire.com/small/fiat/ fiat 500 1969] [http://badanglican.cn/content/view/helmut-lachenmann/ helmut lachenmann] [http://zaweaver.angelfire.com/html/chat-con/ chat con microfono] [http://badanglican.cn/content/view/tv-in/ tv in italia] [http://rohutsonik.angelfire.com/blog/sergio-rossi.htm sergio rossi calzature donna] [http://mastrong.angelfire.com/articles/hello-kitti.htm hello kitti] [http://hilyndel.angelfire.com/lib/big-busty.htm big busty] [http://merengo.netfirms.com/html/lezione-spagnolo.htm lezione spagnolo] [http://badanglican.cn/content/view/donne-di/ donne di 50 anni] [http://michelletrach.netfirms.com/canon-stampanti.htm canon stampanti ip5000] {{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: