I was taking a challenge on hackerrank . The aim is:
read (int) N; then read N integers and print their avg to three decimal places.
Here's the code:
#!/bin/bash
#file name:rdlp.sh
read N
s=0
i=1
while (($i<=$N))
do
read a
s=$((s+a))
i=$((i+1))
done
s=$s/$N
echo "scale=3;$s"|bc -l
fi
When I run the code for some inputs:
3 #(value of N)
4 #(N = 3 integers)
4
3
Then the output is 3.666, but it should be 3.667.
So the QUESTION is that is there anyway to get it right (correct rounding off), or does it work like that only?
(the question came off when the above code was run for Testcase2 of the challenge at hackerrank)