# Sum up a given slice of elements in an array (bash)

Okay here is my code you can probably get the jist of what I'm trying to do. I'm not that good with Linux bash.

``````#random numbers
MAXCOUNT=100
count=1

while [ "\$count" -le \$MAXCOUNT ]; do
number[\$count]=\$RANDOM
let "count += 1"
done

function sum()
{
echo \$((\$1+\$2))
}

#main section
first="\${count[1-20]}"
echo "The sum of the first 20 elements are: \$first"
last="\${count[1-20]}"
echo "The sum of the last 20 elements is: \$last"

if [ \$first -gt \$last ]; then
echo "The sum of the first 20 numbers is greater."
else
echo "The sum of the last 20 numbers is greater."
fi
``````

My goals with this script:

• Get and echo the sum of the first 20 numbers of the array containing randoms numbers.

• Get and echo the sum of the LAST 20 numbers of the array containing randoms numbers.

• Echo whether the first sum is greater than the second sum.

Any help would this would be great! Bash please.

Let's start with the sum function. We actually want to make it a bit more generalized -- make it add up ALL arguments so we can get rid of a couple of loops doing something like `reduce func array`.

``````# Since you are using bash, let's use declare to make things easier.
# Don't use those evil `function foo` or `function foo()` stuffs -- obsolete bourne thing.
sum(){ declare -i acc; for i; do acc+=i; done; echo \$acc; }
``````

The rest is quite easy.

``````MAXCOUNT=100 num=()
# Let's use the less evil native 0-based indices.
for ((i=0; i<MAXCOUNT; i++)); do nums+=(\$RANDOM); done

# https://gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion
# set f to the sum of the 20 elements of nums starting from elem 0
f=\$(sum "\${nums[@]:0:20}"); echo f20=\$f
# set l to the sum of the LAST 20 elems of nums, mind the space
l=\$(sum "\${nums[@]: -20}"); echo l20=\$l
if ((f > l)); then echo f20g; else echo l20g; fi
``````
• At the end on f20g, and l20g what is the "20g" for? Dec 5, 2015 at 22:16
• @DavidPrentice Just too lazy to type those 'last 20 blah greater' thing. Dec 5, 2015 at 22:19
• Okay well it is taking me a bit to digest this, but you 100% answered my question. Dec 5, 2015 at 22:25
``````unset  num[@] sum; c=-1
while  num[c+=1]=\$RANDOM
do     case \$c  in      ([2-7]?) ;;
(\$((sum+=num[c])):|99) ! eval '
printf "\$@\$sum" "\$'"\$((sum<\$3?2:4))\" greater";;
(19)    set "The sum of the %-5s 20 elements is:\t%s\n" \
first "\$sum" last "";  sum=0
esac||break
done
``````

``````The sum of the first 20 elements is:    308347
The sum of the last  20 elements is:    306596
The sum of the first 20 elements is:    greater
``````

Another possible solution:

``````#!/bin/bash

max=100 rng=20   ### Problem conditions

texta="The sum of the %.3sst %d elements is: %d\n"      ### Output
textb="The first sum is %ser than the last sum\n"       ### Output

unset   num                                             ### used vars
for     (( s1=s2=c=0 ;  c<max ; c++ ))
do      num[c]=\$RANDOM
(( c<rng      )) && (( s1+=num[c] ))            ### first sum.
(( c>=max-rng )) && (( s2+=num[c] ))            ### last sum.
done

compare=small; (( s1 > s2 )) && compare=bigg

printf "\$texta" "first" "\$rng" "\$s1"
printf "\$texta" " last" "\$rng" "\$s2"
printf "\$textb" "\$compare"
``````

``````The sum of the first 20 elements is: 348899
The sum of the  last 20 elements is: 336364
The first sum is bigger than the last sum
``````