made averageCalc variadic

This commit is contained in:
John O'Keefe 2023-11-10 12:53:12 -05:00
parent 805c0a7e21
commit 95241405da

View File

@ -0,0 +1,26 @@
package main
import (
"fmt"
"os"
"strconv"
)
func averageCalc(numbers ...float64) float64 {
var sum float64 = 0
for _, number := range numbers {
sum += number
}
return sum / float64(len(numbers))
}
func averageHeadFirstVariadic() {
arguments := os.Args[1:]
var numbers []float64
for _, argument := range arguments {
number, err := strconv.ParseFloat(argument, 64)
Error(err)
numbers = append(numbers, number)
}
fmt.Printf("Average: %0.2\n", averageCalc(numbers...))
}