trying_out_go/cmd/playground/averageHeadFirstSolution.go

25 lines
505 B
Go
Raw Permalink Normal View History

package main
import (
"fmt"
"strings"
"trying_out_go/pkg/datafile"
"trying_out_go/pkg/keyboard"
)
func averageHeadFirstSolution() {
fmt.Printf("What is the name of the file: ")
fileString, err := keyboard.GetString()
Error(err)
fileString = strings.TrimSpace(fileString)
numbers, err := datafile.GetFloats(fileString)
Error(err)
var sum float64 = 0
for _, number := range numbers {
sum += number
}
sampleCount := float64(len(numbers))
fmt.Printf("Average: %0.2f\n", sum/sampleCount)
}