Skip to content

Commit 297a20f

Browse files
Create array_summation.sh
1 parent becf2b4 commit 297a20f

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

array_summation.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Creating a function with a local base variable
2+
function sum_array () {
3+
local sum=0
4+
5+
# Looping through, adding to base variable
6+
for number in "$@"
7+
do
8+
sum=$(echo "$sum + $number" | bc)
9+
done
10+
11+
12+
echo $sum
13+
}
14+
15+
# Calling function with array
16+
test_array=(14 12 23.5 16 19.34)
17+
total=$(sum_array "${test_array[@]}")
18+
echo "The sum of the test array is $total"

0 commit comments

Comments
 (0)