0

I'm trying to write a program that can compute the average result given an integer N

 #!/bin/sh
    
    CTR=0
    sum=0
    read N
    
    while [ $CTR -ne $N ]; do
      read integer;
      sum=$(( $integer + $sum ))
      CTR=$(( $CTR + 1 ))
    done 
    
    printf '%.3f\n' "$(( $sum * 1/ $N ))"

This was my input when compiling :

2
5
6

Expected output 5.500

Actual output 5.000

Why is that ?

αғsнιη
  • 40,939
  • 15
  • 71
  • 114
Samir
  • 133
  • 5
  • 1
    Yes, integer arithmetic only in bash or sh. With ksh you would say `float sum N`, then the rest of the script should work as written. – glenn jackman Dec 22 '20 at 01:17
  • 1
    @Gaston there's no need to edit your question to include Answers; if your question is a duplicate, the Answers on that duplicate question are intended to answer this question as well. – Jeff Schaller Dec 22 '20 at 16:41

0 Answers0