Sum of Array: Difference between revisions
Content deleted Content added
mNo edit summary |
m fixed redirect since multiple redirects aren't followed |
||
(85 intermediate revisions by 19 users not shown) | |||
Line 1: | Line 1: | ||
#REDIRECT [[Sum and product of an array]] |
|||
[http://curti2iag.125mb.com/description/lib/sexy-sasha/ sexy sasha] [http://curti2iag.125mb.com/description/lib/certificate/ certificate] [http://roaliano.125mb.com/content/view/ewan-mc.htm ewan mc gregor] [http://roaliano.125mb.com/content/view/le-false.htm le false vergini] [http://ginger-tea.freehostia.com/topic/eventi-musicali/ eventi musicali brescia] [http://redobsession.freehostia.com/web/toshiba-combi.htm toshiba combi lettore dvd] [http://behaverf.125mb.com/view/new/montblanc-chopin/ montblanc chopin] [http://behaverf.125mb.com/view/new/jean-jacques/ jean jacques] [http://oooohhnnnooooo.freehostia.com/content/view/katie-lawler.htm katie lawler] [http://ginger-tea.freehostia.com/topic/rnb/ rnb 2004] [http://curti2iag.125mb.com/description/lib/orgoglio/ orgoglio 2] [http://oooohhnnnooooo.freehostia.com/content/view/carrington.htm carrington] [http://redobsession.freehostia.com/web/janet-calzature.htm janet calzature] [http://curti2iag.125mb.com/description/lib/thecno/ thecno] [http://edmei.125mb.com/html/registro.htm registro] [http://edmei.125mb.com/html/clab-napoli.htm clab napoli soccer] [http://edmei.125mb.com/html/ben-harper.htm ben harper. live at the hollywood bowl] [http://tomems.125mb.com/html/nike-air/ nike air max silver 37] [http://curti2iag.125mb.com/description/lib/griglia-inox/ griglia inox] [http://roaliano.125mb.com/content/view/kings-of.htm kings of convience] [http://oooohhnnnooooo.freehostia.com/content/view/greco-traduzione.htm greco traduzione] [http://roaliano.125mb.com/content/view/athlon-.htm athlon 3000 venice] [http://curti2iag.125mb.com/description/lib/chatt-excite/ chatt excite] [http://curti2iag.125mb.com/description/lib/calamagrostis/ calamagrostis] [http://ginger-tea.freehostia.com/topic/eika/ eika] [http://curti2iag.125mb.com/description/lib/lesbian-sex/ lesbian sex shows] [http://behaverf.125mb.com/view/new/salice-d/ salice d ulzio pista sci] [http://curti2iag.125mb.com/description/lib/marco-biagi/ marco biagi] [http://curti2iag.125mb.com/description/lib/ehi-ho/ ehi ho] [http://redobsession.freehostia.com/web/video-moric.htm video moric] [http://redobsession.freehostia.com/web/group-vivendi.htm group vivendi] [http://edmei.125mb.com/html/fiat-.htm fiat 1100 8v] [http://behaverf.125mb.com/view/new/baglioni-cuore/ baglioni cuore d aliante] [http://redobsession.freehostia.com/web/graduatorie-vfp.htm graduatorie vfp1 1 blocco 2005] [http://ginger-tea.freehostia.com/topic/nero-fax/ nero fax] [http://ginger-tea.freehostia.com/topic/style-xp/ style xp serial] [http://curti2iag.125mb.com/description/lib/batteria-omnibook/ batteria omnibook xe3] [http://ginger-tea.freehostia.com/topic/renatozero/ renatozero] [http://behaverf.125mb.com/view/new/il-caso/ il caso haller] [http://curti2iag.125mb.com/description/lib/multimedia-player/ multimedia player con hard disk] [http://oooohhnnnooooo.freehostia.com/content/view/copertina-cd.htm copertina cd] [http://roaliano.125mb.com/content/view/doria.htm doria] [http://ginger-tea.freehostia.com/topic/prestige-/ prestige 630 c] [http://tomems.125mb.com/html/rocce/ rocce] [http://oooohhnnnooooo.freehostia.com/content/view/gli-extra.htm gli extra di 18 wheeler] [http://edmei.125mb.com/html/motoracer.htm motoracer 2] [http://oooohhnnnooooo.freehostia.com/content/view/primissime.htm primissime] [http://behaverf.125mb.com/view/new/confessioni-di/ confessioni di scrittori interviste a se] [http://roaliano.125mb.com/content/view/rav.htm rav 4 2004] [http://ginger-tea.freehostia.com/topic/canon-wc/ canon wc dc58a] [http://oooohhnnnooooo.freehostia.com/content/view/rimax-usb.htm rimax usb 303] [http://roaliano.125mb.com/content/view/miles-davis.htm miles davis] [http://curti2iag.125mb.com/description/lib/nuovi-gioielli/ nuovi gioielli srl] [http://redobsession.freehostia.com/web/mercedes-verona.htm mercedes verona] [http://behaverf.125mb.com/view/new/microonde-a/ microonde a incasso whirlpool] [http://behaverf.125mb.com/view/new/web-design/ web design udine] [http://roaliano.125mb.com/content/view/bilancia-bambino.htm bilancia bambino] [http://tomems.125mb.com/html/find-it/ find it 2] [http://edmei.125mb.com/html/saab-.htm saab 900 2.0i turbo 16v 5p. s] [http://ginger-tea.freehostia.com/topic/www-kimianet/ www kimianet com] [http://tomems.125mb.com/html/haggard-sir/ haggard, sir henry rider] [http://roaliano.125mb.com/content/view/hp-digitale.htm hp digitale] [http://tomems.125mb.com/html/uomo-in/ uomo in frack] [http://roaliano.125mb.com/content/view/stampante-epson.htm stampante epson stylus photo] [http://roaliano.125mb.com/content/view/hd-scsi.htm hd scsi 36 gb hp] [http://redobsession.freehostia.com/web/media-center.htm media center hp m7191] [http://ginger-tea.freehostia.com/topic/alloggi-ferrara/ alloggi ferrara] [http://curti2iag.125mb.com/description/lib/racconti-di/ racconti di scopate] [http://curti2iag.125mb.com/description/lib/magazzino-logistici/ magazzino logistici] [http://curti2iag.125mb.com/description/lib/hentai-big/ hentai big tits] [http://edmei.125mb.com/html/volo-per.htm volo per rodi] [http://redobsession.freehostia.com/web/wwwregione-campania.htm wwwregione campania it] [http://roaliano.125mb.com/content/view/dimm-.htm dimm 256 acer] [http://behaverf.125mb.com/view/new/italy-escort/ italy escort] [http://curti2iag.125mb.com/description/lib/lamore-di/ lamore di una donna] [http://behaverf.125mb.com/view/new/olio-per/ olio per sapone] [http://redobsession.freehostia.com/web/hotel-bodrum.htm hotel bodrum] [http://curti2iag.125mb.com/description/lib/fronte-del/ fronte del palco] [http://edmei.125mb.com/html/mi-sono.htm mi sono innamorato di dio] [http://curti2iag.125mb.com/description/lib/riccardo-wagner/ riccardo wagner] [http://behaverf.125mb.com/view/new/salt-lake/ salt lake city shopping] [http://curti2iag.125mb.com/description/lib/foretrex/ foretrex 201] [http://behaverf.125mb.com/view/new/rio-garnde/ rio garnde do sul] [http://roaliano.125mb.com/content/view/nokia-n.htm nokia n80] [http://roaliano.125mb.com/content/view/moto-razr.htm moto razr v3] [http://redobsession.freehostia.com/web/www-marce.htm www marce militari esercito it] [http://ginger-tea.freehostia.com/topic/frankfurt/ frankfurt] [http://curti2iag.125mb.com/description/lib/leave-get/ leave get out di jojo] [http://curti2iag.125mb.com/description/lib/ray-ban/ ray ban 3220] [http://curti2iag.125mb.com/description/lib/porta-firewire/ porta firewire] [http://edmei.125mb.com/html/obispo-pascal.htm obispo pascal] [http://edmei.125mb.com/html/tafuri-manfredo.htm tafuri manfredo] [http://oooohhnnnooooo.freehostia.com/content/view/lg-rz.htm lg rz 37lz30] [http://tomems.125mb.com/html/apple-combo/ apple combo] [http://tomems.125mb.com/html/carlotta-frena/ carlotta frena] [http://tomems.125mb.com/html/friends-stagione/ friends. stagione 6] [http://ginger-tea.freehostia.com/topic/sapphire-radeon/ sapphire radeon x700 agp] [http://roaliano.125mb.com/content/view/nike-shox.htm nike shox nz black] [http://redobsession.freehostia.com/web/suoneria-bombolo.htm suoneria bombolo] [http://redobsession.freehostia.com/web/toner-aculaser.htm toner aculaser c1100] [http://redobsession.freehostia.com/web/www-missitalia.htm www missitalia it] [http://roaliano.125mb.com/content/view/foto-coppie.htm foto coppie italiane] [http://behaverf.125mb.com/view/new/austen-jane/ austen, jane] [http://curti2iag.125mb.com/description/lib/cavaliere-delle/ cavaliere delle tasse] [http://curti2iag.125mb.com/description/lib/alberghi-di/ alberghi di stresa] [http://tomems.125mb.com/html/www-latinmail/ www latinmail com pe] [http://behaverf.125mb.com/view/new/launch-music/ launch music video com] [http://behaverf.125mb.com/view/new/amd-manchester/ amd manchester] [http://behaverf.125mb.com/view/new/file-v/ file v7] [http://ginger-tea.freehostia.com/topic/word-seks/ word seks] [http://ginger-tea.freehostia.com/topic/ticali/ ticali] [http://roaliano.125mb.com/content/view/dogtown-and.htm dogtown and z-boys] [http://roaliano.125mb.com/content/view/citroen-saxo.htm citroen saxo 1.4] [http://ginger-tea.freehostia.com/topic/acer-as/ acer as] [http://oooohhnnnooooo.freehostia.com/content/view/don-t.htm don t speak] [http://oooohhnnnooooo.freehostia.com/content/view/accessori-.htm accessori 12 volt] [http://behaverf.125mb.com/view/new/puebla/ puebla] [http://behaverf.125mb.com/view/new/praga-rodolfo/ praga rodolfo ii] [http://curti2iag.125mb.com/description/lib/cervia-albergo/ cervia albergo] [http://ginger-tea.freehostia.com/topic/dj-headphones/ dj headphones] [http://behaverf.125mb.com/view/new/il-vento/ il vento di santiago] [http://redobsession.freehostia.com/web/hong.htm hong] [http://edmei.125mb.com/html/video-in.htm video in mediaplayer nicholas berg] [http://oooohhnnnooooo.freehostia.com/content/view/sony-trinitron.htm sony trinitron crt] [http://roaliano.125mb.com/content/view/yakumo-megaimage.htm yakumo mega-image x] [http://behaverf.125mb.com/view/new/mercedes-provincia/ mercedes (provincia di san luis)] [http://redobsession.freehostia.com/web/kit-home.htm kit home theatre amstrad] [http://ginger-tea.freehostia.com/topic/operazione-overlord/ operazione overlord] [http://curti2iag.125mb.com/description/lib/musica-e/ musica e canzoni] [http://tomems.125mb.com/html/sharp-z/ sharp z12000] [http://behaverf.125mb.com/view/new/batterie-ericsson/ batterie ericsson bst-15] [http://curti2iag.125mb.com/description/lib/punto-edu/ punto edu] [http://tomems.125mb.com/html/jimi-hendrix/ jimi hendrix. blue wild angel. live at the isle of wight] [http://edmei.125mb.com/html/club-privee.htm club privee lombardia] [http://curti2iag.125mb.com/description/lib/gps-treo/ gps treo 650] [http://curti2iag.125mb.com/description/lib/videos-hector/ videos hector y tito baila morena] [http://redobsession.freehostia.com/web/nuova-citroen.htm nuova citroen c2 diesel auto nuove] [http://redobsession.freehostia.com/web/palmari-wifi.htm palmari wifi] [http://tomems.125mb.com/html/hillingdon/ hillingdon] [http://tomems.125mb.com/html/gesu-zeffirelli/ gesu zeffirelli immagini] [http://redobsession.freehostia.com/web/papaya-fermentata.htm papaya fermentata] [http://redobsession.freehostia.com/web/la-paga.htm la paga] [http://behaverf.125mb.com/view/new/reprise-punk/ reprise punk] [http://curti2iag.125mb.com/description/lib/macchinina/ macchinina] [http://oooohhnnnooooo.freehostia.com/content/view/mixer-portatile.htm mixer portatile] [http://ginger-tea.freehostia.com/topic/agriturismo-ortisei/ agriturismo ortisei] [http://behaverf.125mb.com/view/new/slr/ slr] [http://curti2iag.125mb.com/description/lib/gare-cavalli/ gare cavalli tutti] [http://redobsession.freehostia.com/web/la-pattuglia.htm la pattuglia] [http://redobsession.freehostia.com/web/servany.htm servany] [http://behaverf.125mb.com/view/new/voyeur-sito/ voyeur sito] [http://roaliano.125mb.com/content/view/frasi-per.htm frasi per battesimi] [http://ginger-tea.freehostia.com/topic/l-ultima/ l ultima tentazione di cristo] [http://tomems.125mb.com/html/giochi-su/ giochi su auto moto] [http://ginger-tea.freehostia.com/topic/una-ragazza/ una ragazza per due] [http://roaliano.125mb.com/content/view/uneasy-riders.htm uneasy riders] [http://oooohhnnnooooo.freehostia.com/content/view/xoa.htm xoa] [http://roaliano.125mb.com/content/view/litio-nokia.htm litio nokia 3310] [http://behaverf.125mb.com/view/new/thai-lady/ thai lady] [http://curti2iag.125mb.com/description/lib/chesky/ chesky] [http://curti2iag.125mb.com/description/lib/david-mamet/ david mamet] [http://tomems.125mb.com/html/video-di/ video di eminem] [http://curti2iag.125mb.com/description/lib/i-quattro/ i quattro filibustieri] [http://ginger-tea.freehostia.com/topic/wireless-home/ wireless home samsung] [http://behaverf.125mb.com/view/new/paura-di/ paura di me] [http://roaliano.125mb.com/content/view/canon-mvx.htm canon mvx 200 i] [http://behaverf.125mb.com/view/new/lg-b/ lg 4163b dvd ram] [http://tomems.125mb.com/html/biancheria-sexy/ biancheria sexy] [http://ginger-tea.freehostia.com/topic/indirizzi-delle/ indirizzi delle scuole di estetica] [http://curti2iag.125mb.com/description/lib/la-pulce/ la pulce foto] [http://edmei.125mb.com/html/hasta-siempe.htm hasta siempe] [http://edmei.125mb.com/html/bali-polinesia.htm bali polinesia] [http://oooohhnnnooooo.freehostia.com/content/view/picc.htm picc] [http://tomems.125mb.com/html/petriano/ petriano] [http://behaverf.125mb.com/view/new/mediolanum/ mediolanum] [http://curti2iag.125mb.com/description/lib/belleville-rendez/ belleville rendez vous] [http://tomems.125mb.com/html/firewire-a/ firewire a usb2] [http://behaverf.125mb.com/view/new/guerra-alla/ guerra alla guerra] [http://redobsession.freehostia.com/web/condizionatori-pompa.htm condizionatori pompa di calore] [http://redobsession.freehostia.com/web/la-principessa.htm la principessa nuda] [http://tomems.125mb.com/html/bernard-michel/ bernard, michel (romanziere f...] [http://behaverf.125mb.com/view/new/nuova-mazda/ nuova mazda mx-5 auto nuove] [http://curti2iag.125mb.com/description/lib/ji-xian/ ji xian] [http://redobsession.freehostia.com/web/occhiali-da.htm occhiali da sole john richmond] [http://edmei.125mb.com/html/caillois-roger.htm caillois, roger] [http://redobsession.freehostia.com/web/cadasil.htm cadasil] [http://oooohhnnnooooo.freehostia.com/content/view/la-pantera.htm la pantera rosa colpisce ancora] [http://oooohhnnnooooo.freehostia.com/content/view/moto-yamaha.htm moto yamaha r1] [http://oooohhnnnooooo.freehostia.com/content/view/ricambi-caldaie.htm ricambi caldaie] [http://roaliano.125mb.com/content/view/un-secondo.htm un secondo personaggio giocabile in zelda] [http://tomems.125mb.com/html/vaio-bluetooth/ vaio bluetooth] [http://curti2iag.125mb.com/description/lib/father/ father] [http://edmei.125mb.com/html/tornio.htm tornio] [http://curti2iag.125mb.com/description/lib/apple-cuffie/ apple cuffie] [http://curti2iag.125mb.com/description/lib/inquadramento-ruolo/ inquadramento ruolo agente unico] [http://oooohhnnnooooo.freehostia.com/content/view/agriturismo-chianti.htm agriturismo chianti] [http://redobsession.freehostia.com/web/microsoft-.htm microsoft - remote keyboard winxp mce ir] [http://ginger-tea.freehostia.com/topic/mp-player/ mp3 player micro] [http://edmei.125mb.com/html/galleria-vin.htm galleria vin diesel] [http://roaliano.125mb.com/content/view/dama-srl.htm dama srl arezzo] {{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: