Sum of Array: Difference between revisions

From Rosetta Code
Content deleted Content added
revert spam
mNo edit summary
Line 1: Line 1:
[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}}
{{task}}
Compute the sum of the elements of an Array
Compute the sum of the elements of an Array


Line 11: Line 11:
$var:=0
$var:=0
For ($i;1;Size of array($list))
For ($i;1;Size of array($list))
$var:=$var+$list{$i}
$var:=$var $list{$i}
End for
End for


Line 20: Line 20:
Sum : Integer := 0;
Sum : Integer := 0;
for I in Int_Array'range loop
for I in Int_Array'range loop
Sum := Sum + Int_Array(I);
Sum := Sum Int_Array(I);
end loop;
end loop;


Line 30: Line 30:
repeat with i in array
repeat with i in array
-- very important -- list index starts at 1 not 0
-- very important -- list index starts at 1 not 0
set product to product + i
set product to product i
end repeat
end repeat


Line 42: Line 42:
50 REM Find the sum of elements in the array
50 REM Find the sum of elements in the array
60 SUM = 0
60 SUM = 0
70 FOR I = 1 TO 5: SUM = SUM + ARRAY(I): NEXT I
70 FOR I = 1 TO 5: SUM = SUM ARRAY(I): NEXT I
80 PRINT "The sum is ";SUM
80 PRINT "The sum is ";SUM


==[[C plus plus|C++]]==
==[[C plus plus|C ]]==
[[Category:C plus plus]]
[[Category:C plus plus]]


Line 55: Line 55:
int list[] = { 1, 2, 3 } ;
int list[] = { 1, 2, 3 } ;
int sum = 0 ;
int sum = 0 ;
for( int i = 0 ; i < 3 ; i++ )
for( int i = 0 ; i < 3 ; i )
{
{
sum += list[i];
sum = list[i];
}
}
}
}
Line 69: Line 69:
{
{
int list[] = { 1, 2, 3 } ;
int list[] = { 1, 2, 3 } ;
std::accumulate(list, list + 3, 0);
std::accumulate(list, list 3, 0);
return 0;
return 0;
}
}
Line 78: Line 78:
{
{
T accum = 0;
T accum = 0;
for (unsigned i=0; i<n; i++)
for (unsigned i=0; i<n; i )
accum += array[i];
accum = array[i];
return accum;
return accum;
}
}
Line 103: Line 103:
int arg_length = arg.Length;
int arg_length = arg.Length;


for( int i = 0; i < arg_length; i++ )
for( int i = 0; i < arg_length; i )
value += arg[i];
value = arg[i];




Line 111: Line 111:
int sum = 0;
int sum = 0;
int[] arg = { 1, 2, 3, 4, 5 };
int[] arg = { 1, 2, 3, 4, 5 };
foreach (int value in arg) sum += value;
foreach (int value in arg) sum = value;


==[[Clean]]==
==[[Clean]]==
Line 128: Line 128:


(defparameter *data* #(1 2 3 4 5))
(defparameter *data* #(1 2 3 4 5))
(reduce #'+ *data*)
(reduce #' *data*)


==[[Delphi]]==
==[[Delphi]]==
Line 146: Line 146:


pragma.enable("accumulator")
pragma.enable("accumulator")
accum 0 for x in [1,2,3,4,5] { _ + x }
accum 0 for x in [1,2,3,4,5] { _ x }


==[[Erlang]]==
==[[Erlang]]==
Line 168: Line 168:
0;
0;
sum_rec([Head|Tail]) ->
sum_rec([Head|Tail]) ->
Head + sum_rec(Tail).
Head sum_rec(Tail).


% tail-recursive definition:
% tail-recursive definition:
Line 176: Line 176:
Acc;
Acc;
sum_tail([Head|Tail], Acc) ->
sum_tail([Head|Tail], Acc) ->
sum_tail(Tail, Head + Acc).
sum_tail(Tail, Head Acc).


==[[Forth]]==
==[[Forth]]==
Line 183: Line 183:
: sum ( addr cnt -- n )
: sum ( addr cnt -- n )
0 -rot
0 -rot
cells bounds do i @ + cell +loop ;
cells bounds do i @ cell loop ;




Line 193: Line 193:
dim sum as integer = 0
dim sum as integer = 0
for index as integer = lbound(array) to ubound(array)
for index as integer = lbound(array) to ubound(array)
sum += array(index)
sum = array(index)
next
next


Line 201: Line 201:
let values = [1..10]
let values = [1..10]
sum values -- the easy way
sum values -- the easy way
foldl (+) 0 values -- the hard way
foldl ( ) 0 values -- the hard way


==[[IDL]]==
==[[IDL]]==
Line 214: Line 214:
int[] arg = new int[] { 1,2,3,4,5 };
int[] arg = new int[] { 1,2,3,4,5 };
for (int i: arg)
for (int i: arg)
value += i;
value = i;


==[[JavaScript]]==
==[[JavaScript]]==
Line 222: Line 222:
var sum = 0;
var sum = 0;
for(var i in array)
for(var i in array)
sum += array[i];
sum = array[i];


==[[Perl]]==
==[[Perl]]==
Line 230: Line 230:
my $var;
my $var;
my @list = (1, 2, 3);
my @list = (1, 2, 3);
$var += $_ for (@list);
$var = $_ for (@list);


Alternate
Alternate
Line 245: Line 245:
my $acc = 0;
my $acc = 0;
my @list = qw(1 2 3)
my @list = qw(1 2 3)
map { $acc += $_ } @list
map { $acc = $_ } @list


==[[PHP]]==
==[[PHP]]==
Line 260: Line 260:
lvars i, sum = 0, ar = {1 2 3 4 5 6 7 8 9};
lvars i, sum = 0, ar = {1 2 3 4 5 6 7 8 9};
for i from 1 to length(ar) do
for i from 1 to length(ar) do
ar(i) + sum -> sum;
ar(i) sum -> sum;
endfor;
endfor;


Line 266: Line 266:


lvars sum = 0, ar = {1 2 3 4 5 6 7 8 9};
lvars sum = 0, ar = {1 2 3 4 5 6 7 8 9};
appdata(ar, procedure(x); x + sum -> sum; endprocedure);
appdata(ar, procedure(x); x sum -> sum; endprocedure);


==[[Prolog]]==
==[[Prolog]]==
[[Category:Prolog]]
[[Category:Prolog]]
sum([],0).
sum([],0).
sum([H|T],X) :- sum(T,Y), X is H + Y.
sum([H|T],X) :- sum(T,Y), X is H Y.
test
test
:- sum([1,2,3,4,5,6,7,8,9],X).
:- sum([1,2,3,4,5,6,7,8,9],X).
Line 286: Line 286:


ary = [1,2,3,4,5]
ary = [1,2,3,4,5]
sum = ary.inject{|currentSum,element|currentSum+element}
sum = ary.inject{|currentSum,element|currentSum element}
# => 15
# => 15


Line 292: Line 292:
[[Category:Scala]]
[[Category:Scala]]
val array = Array(1,2,3,4,5)
val array = Array(1,2,3,4,5)
val sum = array.foldLeft(0)(_ + _)
val sum = array.foldLeft(0)(_ _)


This is a shortcut for
This is a shortcut for
val sum = array.foldLeft(0){(currentSum, element) => currentSum + element}
val sum = array.foldLeft(0){(currentSum, element) => currentSum element}


==[[Seed7]]==
==[[Seed7]]==
Line 307: Line 307:
begin
begin
for value range valueArray do
for value range valueArray do
sum +:= value;
sum := value;
end for;
end for;
end func;
end func;
Line 319: Line 319:


val array = [1,2,3,4,5];
val array = [1,2,3,4,5];
foldl op+ 0 array;
foldl op 0 array;


==[[Tcl]]==
==[[Tcl]]==
Line 326: Line 326:
Assuming the values are in a list named <tt>listname</tt>:
Assuming the values are in a list named <tt>listname</tt>:


set result [expr [join $listname +]]
set result [expr [join $listname ]]


==[[Toka]]==
==[[Toka]]==
Line 332: Line 332:


[ ( array size -- sum )
[ ( array size -- sum )
>r 0 r> [ over i swap get-element + ] iterate nip ] is sum-array
>r 0 r> [ over i swap get-element ] iterate nip ] is sum-array


==[[UNIX Shell]]==
==[[UNIX Shell]]==
Line 344: Line 344:
list="1 2 3"
list="1 2 3"
for n in $list
for n in $list
do sum="$(($sum + $n))"
do sum="$(($sum $n))"
done
done
echo $sum
echo $sum
Line 352: Line 352:
sum=0
sum=0
for n
for n
do sum="$(($sum + $n))"
do sum="$(($sum $n))"
done
done
echo $sum
echo $sum
Line 360: Line 360:
sum=0
sum=0
while read n
while read n
do sum="$(($sum + $n))"
do sum="$(($sum $n))"
done
done
echo $sum
echo $sum
Line 371: Line 371:
SUM=0;
SUM=0;
for i in $LIST; do
for i in $LIST; do
SUM=$[$SUM + $i];
SUM=$[$SUM $i];
done;
done;
echo $SUM
echo $SUM

Revision as of 20:05, 15 August 2007

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

Task
Sum of Array
You are encouraged to solve this task according to the task description, using any language you may know.

Compute the sum of the elements of an Array

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

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

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

 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

Compiler: 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#

 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

array = {1, 2, 3, 4, 5}
Start = sum [x \\ x <-: array]

ColdFusion

 <cfset myArray = listToArray("1,2,3,4,5")>
 #arraySum(myArray)#

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

pragma.enable("accumulator")
accum 0 for x in [1,2,3,4,5] { _   x }

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

 : sum ( addr cnt -- n )
   0 -rot
   cells bounds do i @   cell  loop ;


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

 let values = [1..10]
 sum values          -- the easy way
 foldl ( ) 0 values  -- the hard way

IDL

 result = total(array)

Java

 int value = 0;
 int[] arg = new int[] { 1,2,3,4,5 };
 for (int i: arg)
   value  = i;

JavaScript

var array = [1, 2, 3, 4, 5];
var sum = 0;
for(var i in array)
  sum  = array[i];

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

 $list = array(1,2,3,4,5,6,7,8,9);
 echo array_sum($list);

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

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

Interpeter: Python 2.5

 total = sum([1, 2, 3, 4, 5, 6, 7, 8, 9])

Ruby

 ary = [1,2,3,4,5]
 sum = ary.inject{|currentSum,element|currentSum element}
 # => 15

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

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

 val array = [1,2,3,4,5];
 foldl op  0 array;

Tcl

Assuming the values are in a list named listname:

 set result [expr [join $listname  ]]

Toka

[ ( array size -- sum )
  >r 0 r> [ over i swap get-element   ] iterate nip ] is sum-array

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