Sum of Array: Difference between revisions
Content deleted Content added
mNo edit summary |
m fixed redirect since multiple redirects aren't followed |
||
(52 intermediate revisions by 19 users not shown) | |||
Line 1: | Line 1: | ||
#REDIRECT [[Sum and product of an array]] |
|||
[http://pegasik.ifrance.com/html/la-pietra/ la pietra filosofale] [http://gmkamm.ifrance.com/view/new/il-giornalaio.htm il giornalaio] [http://uo-ination.ifrance.com/lib/pokeorder-com/ pokeorder com] [http://last-shadow.ifrance.com/text/directory/bestemmie-da/ bestemmie da ascoltare] [http://ryandrablya.ifrance.com/lib/davide-shama.htm davide shama] [http://sherryfraser03.ifrance.com/web/giochi-brutto/ giochi brutto] [http://pegasik.ifrance.com/html/ridotti/ ridotti] [http://orbzine.ifrance.com/images/small/offerta-viaggi.htm offerta viaggi messico] [http://ryandrablya.ifrance.com/lib/kit-vivavoce.htm kit vivavoce universale] [http://lady-mosquito.ifrance.com/web/monsters-inc/ monsters inc] [http://fishing-gro.ifrance.com/html/paul-mauriat/ paul mauriat] [http://gmkamm.ifrance.com/view/new/rossignol-t.htm rossignol t power] [http://gmkamm.ifrance.com/view/new/braun-interface.htm braun interface 3610] [http://orbzine.ifrance.com/images/small/folie-douce.htm folie douce] [http://sherryfraser03.ifrance.com/web/nikon-d/ nikon d70 1870mm] [http://last-shadow.ifrance.com/text/directory/comandante-mar/ comandante mar] [http://bagrec.ifrance.com/lib/hendon.htm hendon] [http://fishing-gro.ifrance.com/html/cucina-francese/ cucina francese] [http://uo-ination.ifrance.com/lib/batteria-/ batteria 6100 toshiba] [http://last-shadow.ifrance.com/text/directory/joytech-av/ joytech av] [http://zonkismusik.ifrance.com/img/styles/tede-jak.htm tede jak zyc] [http://fishing-gro.ifrance.com/html/porto-seguro/ porto seguro] [http://cannotspell.ifrance.com/description/lib/follw-me/ follw me down] [http://heyclassylady.ifrance.com/html/tf.htm tf] [http://me4tateljnitsa.ifrance.com/data/dvd-viaggio/ dvd viaggio al centro della terra] [http://pegasik.ifrance.com/html/mantua-bagni/ mantua bagni snc] [http://bagrec.ifrance.com/lib/codici-ricarica.htm codici ricarica tim 2004 pirata] [http://lady-mosquito.ifrance.com/web/il-papa/ il papa] [http://uo-ination.ifrance.com/lib/gioco-di/ gioco di cani] [http://lauashbu.ifrance.com/lib/foto-mp/ foto mp3] [http://pegasik.ifrance.com/html/nikon-d/ nikon d 55] [http://orbzine.ifrance.com/images/small/peliculas-porno.htm peliculas porno de negras] [http://zonkismusik.ifrance.com/img/styles/immagine-oscar.htm immagine oscar del cinema] [http://me4tateljnitsa.ifrance.com/data/redes-inalambricas/ redes inalambricas] [http://last-shadow.ifrance.com/text/directory/fairweather/ fairweather] [http://lauashbu.ifrance.com/lib/prestito-pinerolo/ prestito pinerolo] [http://ryandrablya.ifrance.com/lib/i-ll.htm i ll be there pearl harbour] [http://uo-ination.ifrance.com/lib/assistente-sociale/ assistente sociale riflessivo] [http://cannotspell.ifrance.com/description/lib/tipi-di/ tipi di abitazioni] [http://gmkamm.ifrance.com/view/new/il-ballo.htm il ballo del tacco e punta] [http://orbzine.ifrance.com/images/small/trotta.htm trotta] [http://rielethuil.ifrance.com/description/lib/zaino-wage.htm zaino wage] [http://stasiia.ifrance.com/img/styles/grand-hotel/ grand hotel fleming roma] [http://stasiia.ifrance.com/img/styles/finanziamenti-golf/ finanziamenti golf plus] [http://last-shadow.ifrance.com/text/directory/micro-hifi/ micro hifi panasonic] [http://lady-mosquito.ifrance.com/web/nvidia-/ nvidia 6600 pci] [http://epireial-h.ifrance.com/topic/leadtek-bluetoot.htm leadtek bluetoot] [http://butanexlove.ifrance.com/lib/ubfx-pro.htm ub1622fx pro] [http://rielethuil.ifrance.com/description/lib/union-jack.htm union jack] [http://stasiia.ifrance.com/img/styles/dubovnik/ dubovnik] [http://lady-mosquito.ifrance.com/web/microcar/ microcar] [http://gmkamm.ifrance.com/view/new/video-zeljko.htm video zeljko vasic] [http://fishing-gro.ifrance.com/html/prof-line/ prof line deluxe] [http://last-shadow.ifrance.com/text/directory/guns-n/ guns n roses gratis] [http://last-shadow.ifrance.com/text/directory/luigi-reverse/ luigi reverse] [http://stasiia.ifrance.com/img/styles/medicina-del/ medicina del lavoro] [http://rielethuil.ifrance.com/description/lib/english-newspapers.htm english newspapers] [http://me4tateljnitsa.ifrance.com/data/letra-de/ letra de canciones de sin bandera] [http://lady-mosquito.ifrance.com/web/vendita-villa/ vendita villa] [http://gmkamm.ifrance.com/view/new/kia-sorento.htm kia sorento ex top] [http://lady-mosquito.ifrance.com/web/gps-globalsat/ gps globalsat] [http://butanexlove.ifrance.com/lib/joane-laurer.htm joane laurer] [http://stasiia.ifrance.com/img/styles/nikon-ds/ nikon d70s kit nital] [http://ryandrablya.ifrance.com/lib/berger-decapitazione.htm berger decapitazione] [http://heyclassylady.ifrance.com/html/telecom-tech.htm telecom tech memo cordless] [http://stasiia.ifrance.com/img/styles/entebbe/ entebbe] [http://last-shadow.ifrance.com/text/directory/locchio-della/ locchio della morte] [http://stasiia.ifrance.com/img/styles/enermax-/ enermax eg701axvh(w)sfma coolergiant] [http://ryandrablya.ifrance.com/lib/midi-musicas.htm midi musicas temas de filmes italiano] [http://lady-mosquito.ifrance.com/web/i-robot/ i robot] [http://me4tateljnitsa.ifrance.com/data/tv-sony/ tv sony 15] [http://zonkismusik.ifrance.com/img/styles/lettore-dvd.htm lettore dvd con hard disk] [http://cannotspell.ifrance.com/description/lib/ragazze-che/ ragazze che sognano] [http://pegasik.ifrance.com/html/input-rti/ input rti 1200] [http://la-seras.ifrance.com/library/html/audio-fantozzi.htm audio fantozzi] [http://zonkismusik.ifrance.com/img/styles/bryndis.htm bryndis] [http://lady-mosquito.ifrance.com/web/gigi-d/ gigi d agostino datura summer] [http://uo-ination.ifrance.com/lib/colonna-sonora/ colonna sonora di oc] [http://fishing-gro.ifrance.com/html/dvd-p/ dvd p244 samsung] [http://epireial-h.ifrance.com/topic/crema-pasticcera.htm crema pasticcera] [http://la-seras.ifrance.com/library/html/edizioni-conde.htm edizioni conde nast spa] [http://gmkamm.ifrance.com/view/new/georgia-gocce.htm georgia gocce di memoria] [http://lauashbu.ifrance.com/lib/peccati-damore/ peccati damore] [http://lady-mosquito.ifrance.com/web/adobe-elements/ adobe elements 2 0 ita] [http://ryandrablya.ifrance.com/lib/zaino-seven.htm zaino seven] [http://lauashbu.ifrance.com/lib/compatibili-epson/ compatibili epson] [http://fishing-gro.ifrance.com/html/sony-cyber/ sony cyber shot f828] [http://stasiia.ifrance.com/img/styles/cartel-de/ cartel de sansa] [http://cannotspell.ifrance.com/description/lib/kershaw/ kershaw] [http://gmkamm.ifrance.com/view/new/amd-athlon.htm amd athlon 64 3700 socket 939 box] [http://orbzine.ifrance.com/images/small/buell-lightning.htm buell lightning] [http://bagrec.ifrance.com/lib/kodak-easyshare.htm kodak easyshare v550 silver] [http://heyclassylady.ifrance.com/html/tesina-geografia.htm tesina geografia] [http://fishing-gro.ifrance.com/html/damas-de/ damas de compania en morelos] [http://uo-ination.ifrance.com/lib/www-wpl/ www wpl pl] [http://me4tateljnitsa.ifrance.com/data/franck-si/ franck si natra] [http://epireial-h.ifrance.com/topic/reggiseno-al.htm reggiseno al silicone] [http://ryandrablya.ifrance.com/lib/canon-.htm canon 300 usm] [http://uo-ination.ifrance.com/lib/e-femmene/ e femmene salemme] [http://orbzine.ifrance.com/images/small/il-sesso.htm il sesso della strega] [http://gmkamm.ifrance.com/view/new/talaria-calcio.htm talaria calcio] [http://cannotspell.ifrance.com/description/lib/taxus/ taxus] [http://stasiia.ifrance.com/img/styles/harley-davidson/ harley davidson road king] [http://epireial-h.ifrance.com/topic/gps-per.htm gps per qtek] [http://stasiia.ifrance.com/img/styles/hp-all/ hp all in one 9110] [http://sherryfraser03.ifrance.com/web/mini-cd/ mini cd] [http://gmkamm.ifrance.com/view/new/motri.htm motri] [http://rielethuil.ifrance.com/description/lib/mouse-cordles.htm mouse cordles logitech] [http://pegasik.ifrance.com/html/sorin-copilu/ sorin copilu de aur cu mihaela minune] [http://fishing-gro.ifrance.com/html/hyper/ hyper] [http://butanexlove.ifrance.com/lib/fotos-gratis.htm fotos gratis pornografia infantil rusas] [http://la-seras.ifrance.com/library/html/compagna-arezzo.htm compagna arezzo] [http://stasiia.ifrance.com/img/styles/ferrovie-orari/ ferrovie orari] [http://gmkamm.ifrance.com/view/new/condizionatori-fissi.htm condizionatori fissi inverter daikin] [http://gmkamm.ifrance.com/view/new/hp-.htm hp photosmart r717] [http://sherryfraser03.ifrance.com/web/video-asia/ video asia argento fantasma dell opera] [http://stasiia.ifrance.com/img/styles/ps-volante/ ps2 volante logitech driving force] [http://pegasik.ifrance.com/html/sincro-studio/ sincro studio] [http://lady-mosquito.ifrance.com/web/praga-shopping/ praga shopping] [http://last-shadow.ifrance.com/text/directory/per-una/ per una manciata doro] [http://butanexlove.ifrance.com/lib/nintendo-revolution.htm nintendo revolution news] [http://orbzine.ifrance.com/images/small/cubiertas.htm cubiertas] [http://epireial-h.ifrance.com/topic/recetas-de.htm recetas de churro] [http://pegasik.ifrance.com/html/selex-t/ selex t 300] [http://pegasik.ifrance.com/html/borsetta-donna/ borsetta donna accessori] [http://sherryfraser03.ifrance.com/web/informatica-prodotti/ informatica prodotti] [http://me4tateljnitsa.ifrance.com/data/tom-jones/ tom jones] [http://epireial-h.ifrance.com/topic/residence-palinuro.htm residence palinuro] [http://stasiia.ifrance.com/img/styles/incontra-singole/ incontra singole] [http://cannotspell.ifrance.com/description/lib/epson-ct/ epson c13t054240] [http://cannotspell.ifrance.com/description/lib/libreria-ufficio/ libreria ufficio arredo e complementi] [http://stasiia.ifrance.com/img/styles/gary-moore/ gary moore live at montreux 1990] [http://gmkamm.ifrance.com/view/new/zoo-puzzle.htm zoo puzzle] [http://orbzine.ifrance.com/images/small/guardia-medica.htm guardia medica turistica 2006] [http://lady-mosquito.ifrance.com/web/pocket-media/ pocket media assistant pma400] [http://me4tateljnitsa.ifrance.com/data/framebuf/ framebuf] [http://butanexlove.ifrance.com/lib/settimo-anniversario.htm settimo anniversario per ultima online] [http://butanexlove.ifrance.com/lib/blu-eifell.htm blu eifell 65] [http://last-shadow.ifrance.com/text/directory/e-scabroso/ e scabroso] [http://me4tateljnitsa.ifrance.com/data/monster-truck/ monster truck madness] [http://la-seras.ifrance.com/library/html/calendario-carolina.htm calendario carolina 2005] [http://pegasik.ifrance.com/html/flexitank/ flexitank] [http://sherryfraser03.ifrance.com/web/i-basilischi/ i basilischi] [http://ryandrablya.ifrance.com/lib/last-minut.htm last minut viaggi] [http://orbzine.ifrance.com/images/small/tv-color.htm tv color 40 4 3] [http://pegasik.ifrance.com/html/ginnastica-glutei/ ginnastica glutei] [http://me4tateljnitsa.ifrance.com/data/sedurre/ sedurre] [http://bagrec.ifrance.com/lib/i-pirati.htm i pirati dello spazio] [http://fishing-gro.ifrance.com/html/fujifilm/ fujifilm 550] [http://lady-mosquito.ifrance.com/web/indaco-dagli/ indaco dagli] [http://ryandrablya.ifrance.com/lib/lampade-abbronzanti.htm lampade abbronzanti fanno male] [http://me4tateljnitsa.ifrance.com/data/telecamere-digitali/ telecamere digitali panasonic] [http://sherryfraser03.ifrance.com/web/summit-camp/ summit camp com] [http://zonkismusik.ifrance.com/img/styles/desiderio-di.htm desiderio di trans] [http://fishing-gro.ifrance.com/html/la-touline/ la touline] [http://orbzine.ifrance.com/images/small/prestito-umbria.htm prestito umbria] [http://cannotspell.ifrance.com/description/lib/demangeon-albert/ demangeon albert] [http://epireial-h.ifrance.com/topic/planetaria.htm planetaria] [http://lady-mosquito.ifrance.com/web/vanessa-blandon/ vanessa blandon] [http://orbzine.ifrance.com/images/small/quarant-anni.htm quarant anni] [http://cannotspell.ifrance.com/description/lib/photos-sexe/ photos sexe gratuit] [http://lady-mosquito.ifrance.com/web/vivavoce-telit/ vivavoce telit] [http://epireial-h.ifrance.com/topic/intimo-slip.htm intimo slip] [http://me4tateljnitsa.ifrance.com/data/galeri-nicola/ galeri nicola] [http://gmkamm.ifrance.com/view/new/finestre-a.htm finestre a tenuta stagna] [http://me4tateljnitsa.ifrance.com/data/kyocera-s/ kyocera s4 batterie per foto e video] [http://rielethuil.ifrance.com/description/lib/ii-gelataio.htm ii gelataio] [http://bagrec.ifrance.com/lib/canini-maurizio.htm canini maurizio] [http://ryandrablya.ifrance.com/lib/il-marzo.htm il marzo di cecchi gori] [http://pegasik.ifrance.com/html/berchet-bubble/ berchet bubble go] [http://gmkamm.ifrance.com/view/new/dac-video.htm dac video] [http://butanexlove.ifrance.com/lib/il-lungo.htm il lungo coltello di londra] [http://lady-mosquito.ifrance.com/web/webcam-donne/ webcam donne] [http://fishing-gro.ifrance.com/html/pixmania-com/ pixmania com] [http://heyclassylady.ifrance.com/html/lexus-bologna.htm lexus bologna] [http://orbzine.ifrance.com/images/small/bt-x.htm bt x] [http://uo-ination.ifrance.com/lib/canon-power/ canon power] [http://pegasik.ifrance.com/html/screensaver-animato/ screensaver animato arte] [http://gmkamm.ifrance.com/view/new/chi-riunire.htm chi riunire] [http://ryandrablya.ifrance.com/lib/southern-literary.htm southern literary messenger ] [http://uo-ination.ifrance.com/lib/cheats-zero/ cheats zero hour] [http://last-shadow.ifrance.com/text/directory/titoli-delle/ titoli delle canzoni] [http://sherryfraser03.ifrance.com/web/lcd-multimedia/ lcd multimedia] [http://lauashbu.ifrance.com/lib/corsi-formazioni/ corsi formazioni roma gratuiti] [http://rielethuil.ifrance.com/description/lib/perreo-musicas.htm perreo musicas] [http://lady-mosquito.ifrance.com/web/sassy/ sassy] [http://cannotspell.ifrance.com/description/lib/mac-powerbook/ mac powerbook g4 17] [http://gmkamm.ifrance.com/view/new/bmw-i.htm bmw 520i] [http://ryandrablya.ifrance.com/lib/piagge.htm piagge] [http://fishing-gro.ifrance.com/html/toto-cutugno/ toto cutugno serenata] [http://uo-ination.ifrance.com/lib/bruel-/ bruel kjaer] [http://epireial-h.ifrance.com/topic/linksys-modem.htm linksys modem adsl wireless] [http://lauashbu.ifrance.com/lib/www-ungheria/ www ungheria net] [http://heyclassylady.ifrance.com/html/hornett.htm hornett 600] [http://epireial-h.ifrance.com/topic/jvc-gzmc.htm jvc gzmc] [http://me4tateljnitsa.ifrance.com/data/disco-inferno/ disco inferno dvd] [http://me4tateljnitsa.ifrance.com/data/vin-santo/ vin santo] [http://epireial-h.ifrance.com/topic/molly-primavera.htm molly primavera del sesso] [http://fishing-gro.ifrance.com/html/immagini-donne/ immagini donne orientali] [http://fishing-gro.ifrance.com/html/a-faccia/ a faccia nuda] {{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: