Sum and product of an array: Difference between revisions

Content added Content deleted
(Sum and Product of an Array in Rapira)
(→‎{{header|UNIX Shell}}: Use an actual array)
Line 3,002: Line 3,002:
echo $sum $prod</lang>
echo $sum $prod</lang>


{{works with|GNU bash|3.2.0(1)-release (i386-unknown-freebsd6.1)}}
{{works with|Bourne Again SHell}}
{{works with|Korn Shell}}
From variable:
{{works with|Zsh}}
Using an actual array variable:


<lang bash>LIST='20 20 2';
<lang bash>list=(20 20 2);
SUM=0; PROD=1;
(( sum=0, prod=1 ))
for i in $LIST; do
for n in "${list[@]}"; do
SUM=$[$SUM + $i]; PROD=$[$PROD * $i];
(( sum += n, prod *= n ))
done;
done
printf '%d\t%d\n' "$sum" "$prod"
echo $SUM $PROD</lang>
</lang>

{{Out}}
<pre>42 800</pre>


=={{header|UnixPipes}}==
=={{header|UnixPipes}}==