Sum of Array: Difference between revisions
Content deleted Content added
mNo edit summary |
m fixed redirect since multiple redirects aren't followed |
||
(29 intermediate revisions by 18 users not shown) | |||
Line 1: | Line 1: | ||
#REDIRECT [[Sum and product of an array]] |
|||
[http://feralpaw-omm.ifrance.com/html/blur-no/ blur no distance left to run] [http://isabellaval.ifrance.com/ex-alcamo/ ex alcamo] [http://jrosestar.ifrance.com/html/marilynmanson/ marilynmanson] [http://kataevka.ifrance.com/articles/sxga-dvd/ sxga dvd rw] [http://feralpaw-omm.ifrance.com/html/famiglie-analisi/ famiglie analisi dei reati in famiglia] [http://mcunderh.at.tut.by/resources/articles/misuratore-glicemia.htm misuratore glicemia] [http://jrosestar.ifrance.com/html/hot-granny/ hot granny] [http://hewvey.ifrance.com/library/videogiochi-hentai/ videogiochi hentai gratis] [http://hewvey.ifrance.com/library/microsoft-multimedia/ microsoft multimedia keyboard] [http://wowinoth.at.tut.by/web/uni.htm uni 5076] [http://webancks.at.tut.by/images/small/danzel-punp.htm danzel punp it up] [http://isabellaval.ifrance.com/dvd-kubrick/ dvd kubrick eyes] [http://yvharlen.at.tut.by/lib/il-gigante.htm il gigante di boston] [http://thmastrie.angelfire.com/resources/latina-sex.htm latina sex] [http://isabellaval.ifrance.com/siglo-xv/ siglo xv y xvi] [http://konstantinkar.ifrance.com/description/silm-games/ silm games] [http://isabellaval.ifrance.com/vitt/ vitt] [http://hewvey.ifrance.com/library/seconda-prova/ seconda prova scentifico 2004] [http://kairikekui.ifrance.com/resources/gare-dicase/ gare dicase] [http://kataevka.ifrance.com/articles/in-classic/ in classic] [http://kataevka.ifrance.com/articles/ipaq-navigatori/ ipaq navigatori] [http://konstantinkar.ifrance.com/description/mr-driller/ mr driller drill spirits] [http://isabellaval.ifrance.com/langite/ langite] [http://nymphbmbzl222.ifrance.com/linda-cerabolini.htm linda cerabolini] [http://brandalinden.ifrance.com/topic/cobar.htm cobar] [http://isabellaval.ifrance.com/ntchisi/ ntchisi] [http://isabellaval.ifrance.com/midland-nautico/ midland nautico] [http://nymphbmbzl222.ifrance.com/nun-sa.htm nun sa] [http://ariadnafeni.ifrance.com/resources/opteron/ opteron 242] [http://macertot.ifrance.com/view/accessori-toshiba.htm accessori toshiba e800] [http://hewvey.ifrance.com/library/nokia-batteria/ nokia batteria 970] [http://konstantinkar.ifrance.com/description/pc-ipaq/ pc ipaq hx2110] [http://jrosestar.ifrance.com/html/parei-rosa/ parei rosa] [http://brandalinden.ifrance.com/topic/kodak-macchina.htm kodak macchina fotografica digitale] [http://rebuckma.angelfire.com/description/www-glasuj.htm www glasuj zame] [http://spkleins.angelfire.com/offerte-stampanti/ offerte stampanti laser] [http://breveret.at.tut.by/view/new/abreu-casimiro.htm abreu casimiro de] [http://feralpaw-omm.ifrance.com/html/testi-paolo/ testi paolo meneguzzi] [http://kairikekui.ifrance.com/resources/pentium-/ pentium 4 skt 775] [http://isabellaval.ifrance.com/bayadere-classica/ bayadere classica] [http://spkleins.angelfire.com/ei-condor/ ei condor pasa] [http://macertot.ifrance.com/view/filmato-tutte.htm filmato tutte le decapitazioni] [http://mckueltzi.at.tut.by/description/lib/accordi-canzoni.htm accordi canzoni chitarra] [http://lasylvan.angelfire.com/new/frasi-autore/ frasi autore amore] [http://rebuckma.angelfire.com/description/jennifer-lopez.htm jennifer lopez videos porn] [http://zaweaver.at.tut.by/text/directory/una-sexy.htm una sexy giapponese] [http://isabellaval.ifrance.com/de-que/ de que manera te olvido] [http://casloan.angelfire.com/small/franco-battiato/ franco battiato fleurs] [http://wipitts.at.tut.by/text/directory/god-mine.htm god mine] [http://dylantian.ifrance.com/styles/assistenza-e.htm assistenza e riparazione home theatre teac] [http://ferandol.at.tut.by/images/small/hard-disk.htm hard disk cooler 7200 rpm] [http://habeard.angelfire.com/topic/ricerche-tesine.htm ricerche tesine maturita] [http://habeard.angelfire.com/topic/shox-nike.htm shox nike turbo oz] [http://isabellaval.ifrance.com/crowded-house/ crowded house] [http://emstripl.at.tut.by/view/new/lavatrici.htm lavatrici] [http://lasylvan.angelfire.com/new/thermaltake-big/ thermaltake big water se] [http://jrosestar.ifrance.com/html/testi-chicago/ testi chicago] [http://pohogue.angelfire.com/library/bella-ma.htm bella ma pericolosa] [http://jrosestar.ifrance.com/html/little-city/ little city] [http://thmastrie.angelfire.com/resources/sarcelles.htm sarcelles] [http://sagaddy.angelfire.com/styles/goulburn/ goulburn] [http://grsaltrie.at.tut.by/text/directory/software-modello.htm software modello f24] [http://lehartfi.at.tut.by/lib/giornali-atrapani.htm giornali atrapani] [http://konstantinkar.ifrance.com/description/risultati-partite/ risultati partite calcio 15 02 04] [http://jrosestar.ifrance.com/html/ventole-per/ ventole per dissipatori] [http://konstantinkar.ifrance.com/description/uno-nessuno/ uno nessuno centomila] [http://seedging.at.tut.by/text/directory/jvc-dvd.htm jvc dvd autoradio] [http://kedewber.at.tut.by/topic/tv-lcd.htm tv lcd 16 9 26] [http://hewvey.ifrance.com/library/eau-de/ eau de toilette puma] [http://kibrumfik.at.tut.by/text/directory/mtb.htm mtb 24] [http://feralpaw-omm.ifrance.com/html/the-dark/ the dark side of the moon] [http://snerma.angelfire.com/text/zafira/ zafira 22] [http://dweejah.ifrance.com/new/left-outside/ left outside alone remix] [http://kataevka.ifrance.com/articles/lettore-memorie/ lettore memorie flash] [http://macertot.ifrance.com/view/software-sony.htm software sony ericsson] [http://yvharlen.at.tut.by/lib/altar.htm altar] [http://casloan.angelfire.com/small/viaggi-del/ viaggi del ventaglio catalogo] [http://ariadnafeni.ifrance.com/resources/bugel/ bugel] [http://dylantian.ifrance.com/styles/dalcin.htm dalcin] [http://nymphbmbzl222.ifrance.com/myth-makers.htm myth makers super kart gp] [http://brandalinden.ifrance.com/topic/la-squadra.htm la squadra stagione 1 episodio 19] [http://dylantian.ifrance.com/styles/avril-langue.htm avril langue] [http://kataevka.ifrance.com/articles/massaggiatrici-torino/ massaggiatrici torino] [http://hewvey.ifrance.com/library/numero-maglia/ numero maglia stam] [http://ariadnafeni.ifrance.com/resources/mosa/ mosa] [http://gaburlei.angelfire.com/resources/ocean-avanue.htm ocean avanue] [http://kataevka.ifrance.com/articles/rex-fifa/ rex fi160fa] [http://dweejah.ifrance.com/new/bmw-/ bmw 320 d berlina] [http://snerma.angelfire.com/text/agriturismo-bollate/ agriturismo bollate] [http://macertot.ifrance.com/view/t-lexmark.htm t630 lexmark toner] [http://nymphbmbzl222.ifrance.com/rete-ferroviaria.htm rete ferroviaria italiana] [http://dylantian.ifrance.com/styles/tariffario-lanci.htm tariffario lanci] [http://hewvey.ifrance.com/library/prodotti-m/ prodotti 3m] [http://stdamin.at.tut.by/html/la-ragazza.htm la ragazza di mille mesi tognazzi e la minorenne] [http://casloan.angelfire.com/small/la-fossa/ la fossa dei serpenti] [http://dylantian.ifrance.com/styles/igor-cassina.htm igor cassina] [http://ariadnafeni.ifrance.com/resources/lamore-che/ lamore che ci incatena] [http://sagaddy.angelfire.com/styles/josh-hindi/ josh hindi] [http://beyonddreamingx.angelfire.com/content/oswego.htm oswego] [http://rebuckma.angelfire.com/description/ciber.htm ciber] [http://brandalinden.ifrance.com/topic/orario-treni.htm orario treni verbania milano] [http://nymphbmbzl222.ifrance.com/fiat-bravo.htm fiat bravo gt] [http://kairikekui.ifrance.com/resources/siti-porno/ siti porno] [http://pohogue.angelfire.com/library/evolution-theme.htm evolution theme] [http://isabellaval.ifrance.com/hard-disk/ hard disk esterno 80 gb] [http://habeard.angelfire.com/topic/advanced-email.htm advanced email parser] [http://ariadnafeni.ifrance.com/resources/televisore-daewoo/ televisore daewoo] [http://kataevka.ifrance.com/articles/kodak-easyshare/ kodak easyshare dx 7590] [http://jrosestar.ifrance.com/html/il-dicembre/ il dicembre di cecchi gori] [http://hewvey.ifrance.com/library/harumi-nemoto/ harumi nemoto] [http://alena344.ifrance.com/text/fiat-panda/ fiat panda multijet km 0 emotion] [http://lidresze.angelfire.com/blog/free-fumetto.htm free fumetto manga porno] [http://beyonddreamingx.angelfire.com/content/il-candidato.htm il candidato] [http://nymphbmbzl222.ifrance.com/testo-canzone.htm testo canzone minuetto] [http://kataevka.ifrance.com/articles/nuove-immagini/ nuove immagini da project gotham racing 3] [http://gaburlei.angelfire.com/resources/humax-ricevitore.htm humax ricevitore digitale terrestre m] [http://wifry.angelfire.com/styles/fabio-nesti.htm fabio nesti] [http://ariadnafeni.ifrance.com/resources/trasloco-arredamento/ trasloco arredamento climatizzatore] [http://dweejah.ifrance.com/new/quotazione-bnl/ quotazione bnl azione] [http://crhighto.at.tut.by/img/styles/sigma-.htm sigma 18125 canon] [http://breveret.at.tut.by/view/new/digital-video.htm digital video creator 150] [http://dylantian.ifrance.com/styles/prezzi-videocamere.htm prezzi videocamere jvc] [http://feralpaw-omm.ifrance.com/html/hp-ipaq/ hp ipaq phone] [http://sagaddy.angelfire.com/styles/tacchinardi/ tacchinardi] [http://lehartfi.at.tut.by/lib/sexviet-com.htm sexviet com] [http://ariadnafeni.ifrance.com/resources/terratec-/ terratec grabster av400] [http://ariadnafeni.ifrance.com/resources/aaa-nimh/ aaa nimh] [http://dylantian.ifrance.com/styles/enea-volare.htm enea volare mezzo] [http://ferandol.at.tut.by/images/small/il-labrador.htm il labrador puo rimanere solo in casa] [http://mckueltzi.at.tut.by/description/lib/ragazzi-proibiti.htm ragazzi proibiti] [http://konstantinkar.ifrance.com/description/i-due/ i due pericoli pubblici] [http://jrosestar.ifrance.com/html/helene-svedin/ helene svedin nude] [http://dweejah.ifrance.com/new/sotto-la/ sotto la minaccia] [http://dylantian.ifrance.com/styles/idropulsore-braun.htm idropulsore braun] [http://dylantian.ifrance.com/styles/digital-cinema.htm digital cinema sound] [http://mcunderh.at.tut.by/resources/articles/sparkle-.htm sparkle 6600 sli] [http://alena344.ifrance.com/text/ballata-dell/ ballata dell amore vero] [http://macertot.ifrance.com/view/mazda-b.htm mazda b 2500] [http://ariadnafeni.ifrance.com/resources/cover-raf/ cover raf ouch] [http://kedewber.at.tut.by/topic/mestiere.htm mestiere] [http://pohogue.angelfire.com/library/principi-di.htm principi di medicina interna harrison] [http://ariadnafeni.ifrance.com/resources/vogue-sfilate/ vogue sfilate] [http://nymphbmbzl222.ifrance.com/fax-crystal.htm fax crystal] [http://isabellaval.ifrance.com/cerimonia-infernale/ cerimonia infernale] [http://casloan.angelfire.com/small/masterizzatori-dvd/ masterizzatori dvd plextor 16x dual] [http://lidresze.angelfire.com/blog/gunni.htm gunni] [http://macertot.ifrance.com/view/jimi-hendrix.htm jimi hendrix live at woodstock dvd] [http://isabellaval.ifrance.com/il-mare/ il mare intorno a noi] [http://kataevka.ifrance.com/articles/franco-tortora/ franco tortora] [http://thmastrie.angelfire.com/resources/ricetta-cucina.htm ricetta cucina] [http://lasylvan.angelfire.com/new/hotel-notre/ hotel notre dame de fourviere] [http://hewvey.ifrance.com/library/operazione-diabolica/ operazione diabolica] [http://lasylvan.angelfire.com/new/aereo-pazzo/ aereo pazzo del mondo] [http://nymphbmbzl222.ifrance.com/iu-ci.htm iu ci penso] [http://dweejah.ifrance.com/new/gtanetwork/ gtanetwork] [http://dylantian.ifrance.com/styles/meltdown.htm meltdown 2] [http://ariadnafeni.ifrance.com/resources/videocamere-sony/ videocamere sony digitale] [http://yoschaad.at.tut.by/lib/castries.htm castries] [http://dweejah.ifrance.com/new/film-terra/ film terra di confine] [http://snerma.angelfire.com/text/wintvpvr-/ wintvpvr 150 mce] [http://alena344.ifrance.com/text/legalita/ legalita] [http://macertot.ifrance.com/view/quinta.htm quinta] [http://dweejah.ifrance.com/new/hotel-parigi/ hotel parigi francia] [http://alena344.ifrance.com/text/big-circo/ big circo] [http://ferandol.at.tut.by/images/small/pt-cruiser.htm pt cruiser 1 6] [http://feralpaw-omm.ifrance.com/html/lettore-bluetooth/ lettore bluetooth] [http://lehartfi.at.tut.by/lib/le-furie.htm le furie del cinema] [http://kataevka.ifrance.com/articles/fiat-panda/ fiat panda km zero] [http://alena344.ifrance.com/text/fear/ fear] [http://casloan.angelfire.com/small/famiglia-adams/ famiglia adams] [http://crhighto.at.tut.by/img/styles/spyarsenal-com.htm spyarsenal com] [http://feralpaw-omm.ifrance.com/html/www-jb/ www jb seks com] [http://gaburlei.angelfire.com/resources/oss-.htm oss 117 furia a bahia] [http://ariadnafeni.ifrance.com/resources/dvd-divx/ dvd divx recorder hard samsung] [http://habeard.angelfire.com/topic/pernau.htm pernau] [http://alena344.ifrance.com/text/decoder-dtt/ decoder dtt humax 4100] [http://feralpaw-omm.ifrance.com/html/film-oscar/ film oscar] [http://wowinoth.at.tut.by/web/disegno-zodiacale.htm disegno zodiacale] [http://jrosestar.ifrance.com/html/fantasia-disney/ fantasia disney] [http://macertot.ifrance.com/view/eye-toy.htm eye toy] [http://dylantian.ifrance.com/styles/ostaggi-italiani.htm ostaggi italiani liberati] [http://lidresze.angelfire.com/blog/lubricants.htm lubricants] [http://kairikekui.ifrance.com/resources/the-loco/ the loco motion] [http://kairikekui.ifrance.com/resources/foto-camera/ foto camera cellulari] [http://dweejah.ifrance.com/new/xerox-solid/ xerox solid ink] [http://wowinoth.at.tut.by/web/ragazzo-praga.htm ragazzo praga] [http://kairikekui.ifrance.com/resources/quintana/ quintana] [http://wowinoth.at.tut.by/web/totti-che.htm totti che sputa] [http://macertot.ifrance.com/view/trucchi-gta.htm trucchi gta sant andres trucchi] [http://dweejah.ifrance.com/new/paradisi-esotici/ paradisi esotici film dvd] [http://feralpaw-omm.ifrance.com/html/rodi-grecia/ rodi grecia] [http://alena344.ifrance.com/text/renault-scenic/ renault scenic 1 6 16v] [http://konstantinkar.ifrance.com/description/ercolano-scavi/ ercolano scavi antichita] [http://isabellaval.ifrance.com/la-moglie/ la moglie di giorno] [http://kedewber.at.tut.by/topic/descar-videos.htm descar videos gratis] [http://emstripl.at.tut.by/view/new/cosmi.htm cosmi] [http://ariadnafeni.ifrance.com/resources/batteria-percussione/ batteria percussione] [http://wifry.angelfire.com/styles/nickberg-com.htm nickberg com] [http://lasylvan.angelfire.com/new/abadla/ abadla] [http://thmastrie.angelfire.com/resources/chandler.htm chandler] {{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: