Sum of Array: Difference between revisions

From Rosetta Code
Content deleted Content added
revert spam
mNo edit summary
Line 1: Line 1:
[http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=138 free verizon ringtones] [http://wc1.worldcrossing.com/WebX/.1de609fe qwest ringtones] [http://wc1.worldcrossing.com/WebX/.1de609e4 clonazepam online] [http://wc1.worldcrossing.com/WebX/.1de60a31 buy zanaflex] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=106 free mtv ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a1a mtv ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a15 kyocera ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=218 free sony ericsson ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=132 cheap tenuate] [http://students.hsc.unt.edu/housing/item.cfm?type=2904 real ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=137 cheap valium] [http://news.engin.brown.edu/forums/thread-view.asp?tid=168 paxil online] [http://library.cshl.edu/wp/vb/member.php?u=1367 didrex online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30295 cheap sildenafil] [http://wc1.worldcrossing.com/WebX/.1de609f7 nextel ringtones] [http://wc1.worldcrossing.com/WebX/.1de609f3 midi ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1348 cheap ativan] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=337 online xanax] [http://wc1.worldcrossing.com/WebX/.1de609e6 buy cyclobenzaprine] [http://library.cshl.edu/wp/vb/member.php?u=1360 order lorazepam] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=162 free alltel ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=176 didrex online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=204 free mp3 ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2911 free cingular ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a07 tracfone ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30241 buy xanax] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=356 tracfone ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=111 norco online] [http://wc1.worldcrossing.com/WebX/.1de60a13 cheap fioricet] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=191 cheap lorazepam] [http://library.cshl.edu/wp/vb/member.php?u=1369 free ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2858 cheap viagra] [http://news.engin.brown.edu/forums/thread-view.asp?tid=224 free wwe ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2852 xanax online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30307 zoloft online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30331 free ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a27 but sildenafil] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=145 cheap xenical] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30339 free samsung ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30294 cheap prozac] [http://news.engin.brown.edu/forums/thread-view.asp?tid=176 buy ultracet] [http://library.cshl.edu/wp/vb/member.php?u=1352 norco online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=189 lipitor] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=362 free motorola ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=224 sagem ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=101 buy meridia] [http://wc1.worldcrossing.com/WebX/.1de609ee hydrocodone] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30244 diazepam online] [http://wc1.worldcrossing.com/WebX/.1de609e0 ativan online] [http://students.hsc.unt.edu/housing/item.cfm?type=2861 cheap norco] [http://students.hsc.unt.edu/housing/item.cfm?type=2877 cheap prozac] [http://news.engin.brown.edu/forums/thread-view.asp?tid=216 sonyericsson ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=360 free qwest ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=190 lisinopril online] [http://wc1.worldcrossing.com/WebX/.1de60a10 didrex online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=228 cheap ultram] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=76 cialis] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30259 cheap alprazolam] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=359 free nextel ringtones] [http://wc1.worldcrossing.com/WebX/.1de609e5 free cool ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2888 cheap lortab] [http://library.cshl.edu/wp/vb/member.php?u=1373 free nextel ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=216 zyban online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=206 cheap pharmacy online] [http://library.cshl.edu/wp/vb/member.php?u=1344 cheap xanax] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=344 ativan] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30345 free sprint ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30248 cheap ativan] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=131 sprint ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2871 cheap wellbutrin] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=146 zanaflex online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=154 carisoprodol online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30280 cheap propecia] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=118 buy prozac] [http://library.cshl.edu/wp/vb/member.php?u=1341 valium online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=144 xanax online] [http://students.hsc.unt.edu/housing/item.cfm?type=2905 free motorola ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=112 ortho] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=223 wellbutrin online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=169 levitra online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=156 fioricet online] [http://library.cshl.edu/wp/vb/member.php?u=1377 samsung ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30312 ortho online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30356 free cool ringtones] [http://wc1.worldcrossing.com/WebX/.1de609f0 lorazepam online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=187 celexa online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30230 cheap tramadol] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=211 polyphonic ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=214 cheap zanaflex] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=110 nokia ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=189 ortho online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=237 samsung ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=151 soma online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=99 lorazepam online] [http://students.hsc.unt.edu/housing/item.cfm?type=2912 sagem ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2907 sprint ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a17 lipitor online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30334 free mp3 ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=234 free sprint ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=141 vigrx online] [http://wc1.worldcrossing.com/WebX/.1de60a23 free polyphonic ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a1f lisinopril online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=197 mp3 ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1349 order viagra] [http://library.cshl.edu/wp/vb/member.php?u=1382 cingular ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30310 cheap tenuate] [http://students.hsc.unt.edu/housing/item.cfm?type=2922 free kyocera ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=206 free qwest ringtones] [http://wc1.worldcrossing.com/WebX/.1de609f9 order norco] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=351 clonazepam online] [http://students.hsc.unt.edu/housing/item.cfm?type=2878 sildenafil online] [http://students.hsc.unt.edu/housing/item.cfm?type=2902 nextel ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=357 free funny ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1347 cheap cialis] [http://library.cshl.edu/wp/vb/member.php?u=1365 cheap ultracet] [http://news.engin.brown.edu/forums/thread-view.asp?tid=210 free sprint ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=158 diazepam online] [http://wc1.worldcrossing.com/WebX/.1de609ea free ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a25 samsung ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=88 free ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1358 hydrocodone online] [http://library.cshl.edu/wp/vb/member.php?u=1339 tramadol online] [http://students.hsc.unt.edu/housing/item.cfm?type=2868 cheap ambien] [http://news.engin.brown.edu/forums/thread-view.asp?tid=228 mtv ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=104 free motorola ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=89 funny ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=211 music ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=80 cool ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1388 cheap fioricet] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=361 free real ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=172 online lorazepam] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=333 soma online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=182 clomid online] [http://library.cshl.edu/wp/vb/member.php?u=1355 cheap clonazepam] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=205 paxil online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=184 cheap lisinopril] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=124 free samsung ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a04 sonyericsson ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=115 cheap phentermine] [http://library.cshl.edu/wp/vb/member.php?u=1387 alltel ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a05 sprint ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=167 cheap clonazepam] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30283 buy didrex] [http://news.engin.brown.edu/forums/thread-view.asp?tid=157 ultram online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=230 verizon ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30309 celexa online] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=348 but norco] [http://wc1.worldcrossing.com/WebX/.1de60a11 diethylpropion online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=188 tenuate online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=212 verizon ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30308 cheap lipitor] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=82 buy diazepam] [http://library.cshl.edu/wp/vb/member.php?u=1380 verizon ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=163 adipex online] [http://students.hsc.unt.edu/housing/item.cfm?type=2859 meridia online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=209 free samsung ringtones] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=170 cingular ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=78 clomid] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30298 lisinopril online] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=74 cheap carisoprodol] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=352 cheap paxil] [http://news.engin.brown.edu/forums/thread-view.asp?tid=227 free ericsson ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1386 mono ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=69 albuterol online] [http://wc1.worldcrossing.com/WebX/.1de60a12 ericsson ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=171 ambien online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=188 cheap levitra] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=83 didrex online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30358 free midi ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2879 cheap clomid] [http://news.engin.brown.edu/forums/thread-view.asp?tid=186 lipitor online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=215 free sagem ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=346 cheap meridia] [http://students.hsc.unt.edu/housing/item.cfm?type=2923 free sony ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30237 cheap valium] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=171 order clomid] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=184 online hoodia] [http://wc1.worldcrossing.com/WebX/.1de60a1b music ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30264 but hydrocodone] [http://wc1.worldcrossing.com/WebX/.1de609db cheap adipex] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30321 cheap hgh] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=90 hgh online] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30263 order levitra] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=133 free tracfone ringtones] [http://news.engin.brown.edu/forums/thread-view.asp?tid=226 free sony ringtones] [http://www.ees.ufl.edu/alumni/forums.asp?ForumId=5&TopicId=117 propecia online] [http://news.engin.brown.edu/forums/thread-view.asp?tid=165 vicodin online] [http://library.cshl.edu/wp/vb/member.php?u=1340 soma online] [http://students.hsc.unt.edu/housing/item.cfm?type=2909 free verizon ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30252 buy meridia] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=221 cheap vicodin] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30347 free verizon ringtones] [http://library.cshl.edu/wp/vb/member.php?u=1354 cheap alprazolam] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30330 free nokia ringtones] [http://students.hsc.unt.edu/housing/item.cfm?type=2903 free qwest ringtones] [http://wc1.worldcrossing.com/WebX/.1de60a26 sharp ringtones] [http://www.e.kth.se/cgi-bin/esekt/discussion?command=read&discussionid=4&id=30348 free polyphonic ringtones] [http://www.psfc.mit.edu/~jinseok/bbse/view.php?id=presentations&no=345 viagra online] [http://itcweb.ecsu.edu/portal/forums.asp?ForumId=13&TopicId=239 cheap sildenafil] [http://students.hsc.unt.edu/housing/item.cfm?type=2876 buy didrex] {{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 19:59, 14 July 2007

free verizon ringtones qwest ringtones clonazepam online buy zanaflex free mtv ringtones mtv ringtones kyocera ringtones free sony ericsson ringtones cheap tenuate real ringtones cheap valium paxil online didrex online cheap sildenafil nextel ringtones midi ringtones cheap ativan online xanax buy cyclobenzaprine order lorazepam free alltel ringtones didrex online free mp3 ringtones free cingular ringtones tracfone ringtones buy xanax tracfone ringtones norco online cheap fioricet cheap lorazepam free ringtones cheap viagra free wwe ringtones xanax online zoloft online free ringtones but sildenafil cheap xenical free samsung ringtones cheap prozac buy ultracet norco online lipitor free motorola ringtones sagem ringtones buy meridia hydrocodone diazepam online ativan online cheap norco cheap prozac sonyericsson ringtones free qwest ringtones lisinopril online didrex online cheap ultram cialis cheap alprazolam free nextel ringtones free cool ringtones cheap lortab free nextel ringtones zyban online cheap pharmacy online cheap xanax ativan free sprint ringtones cheap ativan sprint ringtones cheap wellbutrin zanaflex online carisoprodol online cheap propecia buy prozac valium online xanax online free motorola ringtones ortho wellbutrin online levitra online fioricet online samsung ringtones ortho online free cool ringtones lorazepam online celexa online cheap tramadol polyphonic ringtones cheap zanaflex nokia ringtones ortho online samsung ringtones soma online lorazepam online sagem ringtones sprint ringtones lipitor online free mp3 ringtones free sprint ringtones vigrx online free polyphonic ringtones lisinopril online mp3 ringtones order viagra cingular ringtones cheap tenuate free kyocera ringtones free qwest ringtones order norco clonazepam online sildenafil online nextel ringtones free funny ringtones cheap cialis cheap ultracet free sprint ringtones diazepam online free ringtones samsung ringtones free ringtones hydrocodone online tramadol online cheap ambien mtv ringtones free motorola ringtones funny ringtones music ringtones cool ringtones cheap fioricet free real ringtones online lorazepam soma online clomid online cheap clonazepam paxil online cheap lisinopril free samsung ringtones sonyericsson ringtones cheap phentermine alltel ringtones sprint ringtones cheap clonazepam buy didrex ultram online verizon ringtones celexa online but norco diethylpropion online tenuate online verizon ringtones cheap lipitor buy diazepam verizon ringtones adipex online meridia online free samsung ringtones cingular ringtones clomid lisinopril online cheap carisoprodol cheap paxil free ericsson ringtones mono ringtones albuterol online ericsson ringtones ambien online cheap levitra didrex online free midi ringtones cheap clomid lipitor online free sagem ringtones cheap meridia free sony ringtones cheap valium order clomid online hoodia music ringtones but hydrocodone cheap adipex cheap hgh hgh online order levitra free tracfone ringtones free sony ringtones propecia online vicodin online soma online free verizon ringtones buy meridia cheap vicodin free verizon ringtones cheap alprazolam free nokia ringtones free qwest ringtones sharp ringtones free polyphonic ringtones viagra online cheap sildenafil buy didrex

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