Compute the Average

  • + 1 comment
    read count_of_number
    if [[ "$count_of_number" -ge 1 && "$count_of_number" -le 500 ]]; then
        list_of_integers=0
        for number in $(seq 1 $count_of_number)
            do
                read numbers
                if [[ "$numbers" -ge -10000 && "$numbers" -le 10000 ]]; then
                list_of_integers=$(($numbers + $list_of_integers))
                else
                    echo "the number: $numbers is out of the range (-10000 <= x <= 10000)"
                    exit 1
                fi
            done
        average=$(echo "scale=4; $list_of_integers / $count_of_number" | bc -l | xargs printf "%.3f")
        echo $average
    else
        echo "the count of number is out of the range (1 <= N <= 500)"
    fi