Sum of Array: Difference between revisions
Content added Content deleted
mNo edit summary |
m (fixed redirect since multiple redirects aren't followed) |
||
(48 intermediate revisions by 18 users not shown) | |||
Line 1: | Line 1: | ||
#REDIRECT [[Sum and product of an array]] |
|||
[http://mamans.125mb.com/topic/google-finanziaria.htm google finanziaria repubblica di san mar] [http://fikimber.125mb.com/lib/illuminati-malice.htm illuminati malice mizer] [http://wiellico.125mb.com/html/materasso-molla.htm materasso molla] [http://dleslis.125mb.com/resources/ultimi-motori.htm ultimi motori di ricerca] [http://stjollay.125mb.com/topic/ballo-qua/ ballo qua qua] [http://stjollay.125mb.com/topic/flipp/ flipp] [http://awihimmeru.angelfire.com/obiettivo-minolta.htm obiettivo minolta zoom 300] [http://stjollay.125mb.com/topic/ostello-la/ ostello la vespa] [http://bedrew.125mb.com/fornitura-prodotti.htm fornitura prodotti ufficio] [http://awihimmeru.angelfire.com/quadrifoglio.htm quadrifoglio] [http://gitedy.125mb.com/blog/magix-platinum/ magix platinum mp3 light] [http://mamans.125mb.com/topic/ottavo-padiglione.htm ottavo padiglione] [http://mamans.125mb.com/topic/www-wrestlemania.htm www wrestlemania it] [http://bedrew.125mb.com/immagini-simpsons.htm immagini simpsons] [http://woharwoo.angelfire.com/data/ruota/ ruota] [http://mamans.125mb.com/topic/fa-cge.htm fa cge non sia mai] [http://yarobiso.angelfire.com/text/lavatrice-g.htm lavatrice 1200g] [http://kocarawf.125mb.com/blog/www-grande.htm www grande frattello com] [http://hoadmoni.125mb.com/styles/debito-estero/ debito estero] [http://champ-alnite.angelfire.com/pamela-porno.htm pamela porno] [http://amarymuk.angelfire.com/html/preventivi-online.htm preventivi online] [http://elernst.angelfire.com/articles/discovery.htm discovery 2003] [http://sewehner.125mb.com/articles/km-volkswagen/ km0 volkswagen auto km 0] [http://tibrown.125mb.com/description/sitecom-portable/ sitecom portable media] [http://blhestrie.125mb.com/web/sono-seduto/ sono seduto sul ramo e mi sento bene] [http://dacornel.125mb.com/data/gay-black.htm gay black] [http://dleslis.125mb.com/resources/lancia-beta.htm lancia beta spider] [http://fikimber.125mb.com/lib/il-gatto.htm il gatto e la volpe] [http://dacornel.125mb.com/data/audi-allroad.htm audi allroad 25] [http://degaylor.125mb.com/unita-sanitaria/ unita sanitaria locale roma 8] [http://stjollay.125mb.com/topic/bersaglio-di/ bersaglio di notte] [http://elernst.angelfire.com/articles/hendai.htm hendai] [http://yonosoyyo.angelfire.com/directory/poesia-da.htm poesia da amore] [http://dacornel.125mb.com/data/foto-di.htm foto di elfi folletti] [http://fikimber.125mb.com/lib/promesse-e.htm promesse e compromessi] [http://blhestrie.125mb.com/web/grido-di/ grido di battaglia] [http://hoadmoni.125mb.com/styles/interrati/ interrati] [http://mamans.125mb.com/topic/asus-sli.htm asus sli 939 deluxe] [http://degaylor.125mb.com/ian-dury/ ian dury] [http://kocarawf.125mb.com/blog/stephane-pompougnac.htm stephane pompougnac foto] [http://bedrew.125mb.com/toner.htm toner 3715] [http://nescogin.125mb.com/content/amoreux-solitaire.htm amoreux solitaire] [http://blhestrie.125mb.com/web/profondo/ profondo] [http://woharwoo.angelfire.com/data/bmw-/ bmw 320 touring] [http://paryker.125mb.com/styles/pizica/ pizica] [http://paryker.125mb.com/styles/mulheres-fodendo/ mulheres fodendo] [http://awihimmeru.angelfire.com/numeri-telefono.htm numeri telefono provider] [http://sewehner.125mb.com/articles/frasi-per/ frasi per la pensione] [http://mcclaise.angelfire.com/description/anderson-electronic/ anderson electronic] [http://degaylor.125mb.com/asus-ppx/ asus p4p800x 478] [http://sewehner.125mb.com/articles/annuncio-incontri/ annuncio incontri veneto] [http://grdugans.angelfire.com/text/jb-software.htm jb software 84] [http://sewehner.125mb.com/articles/plasma-hdtv/ plasma hdtv] [http://tibrown.125mb.com/description/panasonic-cqcn/ panasonic cqc1301n] [http://kocarawf.125mb.com/blog/wintv-nova.htm wintv nova s] [http://sawilson.125mb.com/data/interrabang.htm interrabang] [http://tibrown.125mb.com/description/banca-popolare/ banca popolare del materano] [http://slavfondmeister.angelfire.com/styles/piastra-madre.htm piastra madre] [http://fikimber.125mb.com/lib/toshiba-espansione.htm toshiba espansione ram 512 mb] [http://kocarawf.125mb.com/blog/the-cranberries.htm the cranberries just my immagination] [http://fikimber.125mb.com/lib/io-services.htm io2 services] [http://stjollay.125mb.com/topic/tshirt-xxl/ tshirt xxl] [http://sibeatri.angelfire.com/html/www-ragazzi/ www ragazzi it] [http://nescogin.125mb.com/content/appunto-diritto.htm appunto diritto lavoro] [http://paryker.125mb.com/styles/memoria-ddr/ memoria ddr2400] [http://morudd.angelfire.com/web/lavoro-paderno/ lavoro paderno dugnano] [http://dacornel.125mb.com/data/carte-da.htm carte da mercante in fiera] [http://sewehner.125mb.com/articles/petrarca/ petrarca] [http://fikimber.125mb.com/lib/daniela-fini.htm daniela fini] [http://hoadmoni.125mb.com/styles/lutherstadt-wittenberg/ lutherstadt wittenberg] [http://mamans.125mb.com/topic/concorsi-x.htm concorsi x tutti] [http://amarymuk.angelfire.com/html/i-t.htm i t c s] [http://dleslis.125mb.com/resources/la-donnagatto.htm la donnagatto in dvd] [http://dleslis.125mb.com/resources/notre-dame.htm notre dame de paris live arena di verona] [http://mamans.125mb.com/topic/televisore-.htm televisore 21 dvd] [http://sibeatri.angelfire.com/html/beach-tennis/ beach tennis turquoise] [http://requidim.angelfire.com/web/hd-western.htm hd western] [http://blhestrie.125mb.com/web/video-proiettore/ video proiettore schermo] [http://kocarawf.125mb.com/blog/panasonic-pt.htm panasonic pt ae700] [http://dleslis.125mb.com/resources/miracle-lancome.htm miracle lancome profumo] [http://grdugans.angelfire.com/text/oni-in.htm oni in distribuzione] [http://hoadmoni.125mb.com/styles/divx-dvd/ divx dvd lettori e] [http://awihimmeru.angelfire.com/didegni-da.htm didegni da colorare] [http://stjollay.125mb.com/topic/frommel-libri/ frommel libri] [http://blhestrie.125mb.com/web/rey-pirin/ rey pirin feat dj blass] [http://gitedy.125mb.com/blog/piccoli-indiani/ piccoli indiani] [http://nescogin.125mb.com/content/mario-piovano.htm mario piovano] [http://gistrate.angelfire.com/html/una-chat.htm una chat] [http://fikimber.125mb.com/lib/un-filmato.htm un filmato per bet on soldier] [http://fikimber.125mb.com/lib/modelli-con.htm modelli con grossi cazzi] [http://bedrew.125mb.com/toner-type.htm toner type 305] [http://bedrew.125mb.com/sign.htm sign] [http://gistrate.angelfire.com/html/promotional-product.htm promotional product] [http://kocarawf.125mb.com/blog/passi-che.htm passi che facciamo] [http://ruwier.125mb.com/download-meteo/ download meteo] [http://sawilson.125mb.com/data/diesel-ds.htm diesel ds 0025] [http://amarymuk.angelfire.com/html/anabas.htm anabas] [http://sibeatri.angelfire.com/html/concessionario-mercedes/ concessionario mercedes] [http://sawilson.125mb.com/data/printr-nori.htm printr nori] [http://spdinnie.angelfire.com/html/valentina-zambrotta/ valentina zambrotta] [http://awihimmeru.angelfire.com/nomi-latini.htm nomi latini] [http://kocarawf.125mb.com/blog/sambame-upadance.htm sambame upadance] [http://dacornel.125mb.com/data/iss-pro.htm iss pro evolution 2204] [http://hoadmoni.125mb.com/styles/stampante-canon/ stampante canon cp 330] [http://bedrew.125mb.com/de-natura.htm de natura di lucrezio] [http://sewehner.125mb.com/articles/traina-sportiva/ traina sportiva] [http://degaylor.125mb.com/toner-infotec/ toner infotec is 2018] [http://tibrown.125mb.com/description/provincia-trento/ provincia trento] [http://degaylor.125mb.com/www-cox/ www cox net ham] [http://hahosmer.angelfire.com/html/cavi-ottici.htm cavi ottici a metro] [http://sewehner.125mb.com/articles/ultima/ ultima] [http://spdinnie.angelfire.com/html/big-cock/ big cock shemale] [http://sawilson.125mb.com/data/buonanotte-a.htm buonanotte a te] [http://wiellico.125mb.com/html/allinwonder-radeon.htm allinwonder radeon] [http://hoadmoni.125mb.com/styles/computerdiscount-it/ computerdiscount it] [http://sibeatri.angelfire.com/html/tatoo-maori/ tatoo maori] [http://paryker.125mb.com/styles/alto-aeg/ alto aeg] [http://mamans.125mb.com/topic/kiss-dp.htm kiss dp 508] [http://tibrown.125mb.com/description/denti-gif/ denti gif] [http://fikimber.125mb.com/lib/olimpia-costruzioni.htm olimpia costruzioni vicenza] [http://sibeatri.angelfire.com/html/dormire-rimini/ dormire rimini] [http://ruwier.125mb.com/lancia-y/ lancia y ca] [http://fikimber.125mb.com/lib/hollywood-mosters.htm hollywood mosters] [http://nescogin.125mb.com/content/prove-d.htm prove d esame 2002] [http://ruwier.125mb.com/flash-sb/ flash sb25] [http://dacornel.125mb.com/data/zelda.htm zelda] [http://fikimber.125mb.com/lib/il-diabolico.htm il diabolico avventuriero] [http://grdugans.angelfire.com/text/bmw-serie.htm bmw serie 3 2003] [http://mcclaise.angelfire.com/description/divx-dvd/ divx dvd panasonic] [http://blhestrie.125mb.com/web/tom-dick/ tom dick e harry] [http://sawilson.125mb.com/data/venezuela.htm venezuela] [http://mamans.125mb.com/topic/video-gocce.htm video gocce di memoria] [http://fikimber.125mb.com/lib/storia-del.htm storia del violino] [http://sawilson.125mb.com/data/al-bar.htm al bar si muore] [http://nescogin.125mb.com/content/superstar-jamelia.htm superstar jamelia lyrics] [http://bedrew.125mb.com/registratore-hard.htm registratore hard disk] [http://dleslis.125mb.com/resources/breathe-blu.htm breathe blu cantrell] [http://stjollay.125mb.com/topic/storoni-mazzolani/ storoni mazzolani lidia] [http://stjollay.125mb.com/topic/harry-potter/ harry potter magic box] [http://fikimber.125mb.com/lib/olivetti.htm olivetti 8400] [http://stjollay.125mb.com/topic/dedicadas-para/ dedicadas para los que estan] [http://anethel.angelfire.com/finalfantasy.htm finalfantasy 11] [http://nescogin.125mb.com/content/vcd-cover.htm vcd cover] [http://sewehner.125mb.com/articles/amd-athlonxp/ amd athlonxp] [http://bedrew.125mb.com/www-whirlpool.htm www whirlpool com] [http://sewehner.125mb.com/articles/www-lapin/ www lapin it] [http://morudd.angelfire.com/web/sauna-produzione/ sauna produzione] [http://morudd.angelfire.com/web/tour-italiano/ tour italiano di laura pausini] [http://dleslis.125mb.com/resources/assistenza-forli.htm assistenza forli cesena] [http://ruwier.125mb.com/nome-dominio/ nome dominio] [http://fikimber.125mb.com/lib/wireless-keyboard.htm wireless keyboard pda] [http://blhestrie.125mb.com/web/registratore-video/ registratore video digitale] [http://anethel.angelfire.com/pamela-e.htm pamela e tommy] [http://tibrown.125mb.com/description/mindy-vega/ mindy vega] [http://degaylor.125mb.com/olimpus-ultrazoom/ olimpus ultrazoom] [http://fikimber.125mb.com/lib/it-is.htm it is cos i m cool] [http://dleslis.125mb.com/resources/this-loce.htm this loce] [http://fikimber.125mb.com/lib/sony-palm.htm sony palm] [http://kocarawf.125mb.com/blog/registratore.htm registratore 24] [http://nescogin.125mb.com/content/videos-y.htm videos y fotos beyonce] [http://paryker.125mb.com/styles/telefonie/ telefonie] [http://grdugans.angelfire.com/text/nokia-.htm nokia 6230 graphite] [http://vegrigsb.angelfire.com/resources/eddvdsk.htm ed16dvdsk 00] [http://dleslis.125mb.com/resources/pescatori-di.htm pescatori di perle] [http://blhestrie.125mb.com/web/dream-theater/ dream theater master of puppets] [http://slavfondmeister.angelfire.com/styles/phedra-lancia.htm phedra lancia] [http://ruwier.125mb.com/raffaella-berge/ raffaella berge] [http://mcclaise.angelfire.com/description/canzone-di/ canzone di nando] [http://ruwier.125mb.com/xbox-trucchi/ xbox trucchi] [http://degaylor.125mb.com/sony-dcrpc/ sony dcrpc 106e] [http://elernst.angelfire.com/articles/naked-hong.htm naked hong nhung] [http://mamans.125mb.com/topic/pollo-camper.htm pollo camper] [http://sewehner.125mb.com/articles/deore-xt/ deore xt] [http://hahosmer.angelfire.com/html/nu-sinh.htm nu sinh ngo quen] [http://bedrew.125mb.com/tappetino-per.htm tappetino per il mouse] [http://tibrown.125mb.com/description/sei-la/ sei la mia donna] [http://ruwier.125mb.com/pentium/ pentium 4 531] [http://hahosmer.angelfire.com/html/monitor-da.htm monitor da studio near field] [http://nescogin.125mb.com/content/scheda-video.htm scheda video agp 4x] [http://dleslis.125mb.com/resources/moto-ducati.htm moto ducati monster] [http://tibrown.125mb.com/description/anny-ondra/ anny ondra] [http://hoadmoni.125mb.com/styles/spot-unieuro/ spot unieuro tonino guerra] [http://yonosoyyo.angelfire.com/directory/scopa-elettrica.htm scopa elettrica 1400] [http://stjollay.125mb.com/topic/playstation-multitap/ playstation multitap] [http://morudd.angelfire.com/web/www-provincia/ www provincia brescia it] [http://ruwier.125mb.com/ercole-al/ ercole al centro della terra] [http://yarobiso.angelfire.com/text/vidimazione-libri.htm vidimazione libri sociali] [http://sawilson.125mb.com/data/intervallo-rai.htm intervallo rai] [http://sawilson.125mb.com/data/heifetz.htm heifetz] [http://nescogin.125mb.com/content/ute.htm ute] [http://dacornel.125mb.com/data/o-zone.htm o zone dragostea din tei mid] [http://anethel.angelfire.com/nokia-.htm nokia 6260 nero] [http://mamans.125mb.com/topic/jo-donatello.htm jo donatello] [http://yarobiso.angelfire.com/text/traghetto-ischia.htm traghetto ischia] [http://vegrigsb.angelfire.com/resources/www-s.htm www s francesco di paola it] [http://amarymuk.angelfire.com/html/rosse-tinto.htm rosse tinto brass] [http://sawilson.125mb.com/data/video-musicali.htm video musicali articolo31] [http://dacornel.125mb.com/data/la-legge.htm la legge della violenza nel bronx] [http://spdinnie.angelfire.com/html/casper-e/ casper e il natale di paura 2] [http://dleslis.125mb.com/resources/i-ve.htm i ve got your number] {{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: