working with average
This commit is contained in:
parent
af6ae12947
commit
9fe6dcac42
24
averageHeadFirstSolution.go
Normal file
24
averageHeadFirstSolution.go
Normal file
@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"git.linuxhg.com/john-okeefe/datafile"
|
||||
"git.linuxhg.com/john-okeefe/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)
|
||||
}
|
@ -11,14 +11,7 @@ import (
|
||||
"git.linuxhg.com/john-okeefe/keyboard"
|
||||
)
|
||||
|
||||
func Error(err error) {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func average() {
|
||||
func averageMySolution() {
|
||||
fmt.Printf("What is the name of the file: ")
|
||||
fileString, err := keyboard.GetString()
|
||||
Error(err)
|
10
error.go
Normal file
10
error.go
Normal file
@ -0,0 +1,10 @@
|
||||
package main
|
||||
|
||||
import "log"
|
||||
|
||||
func Error(err error) {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return
|
||||
}
|
1
go.mod
1
go.mod
@ -11,6 +11,7 @@ require (
|
||||
)
|
||||
|
||||
require (
|
||||
git.linuxhg.com/john-okeefe/datafile v0.0.0-20231019003321-0d46a9c14627 // indirect
|
||||
github.com/atotto/clipboard v0.1.4 // indirect
|
||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
|
||||
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
|
||||
|
2
go.sum
2
go.sum
@ -1,3 +1,5 @@
|
||||
git.linuxhg.com/john-okeefe/datafile v0.0.0-20231019003321-0d46a9c14627 h1:CssED3WO+ie9309BqxqHWPzU2dJiVgn/M6m2Xi9B0mg=
|
||||
git.linuxhg.com/john-okeefe/datafile v0.0.0-20231019003321-0d46a9c14627/go.mod h1:HexHt1UMGUDMV5bhB81nAKdvGaCnxKA2upRd7cV8laI=
|
||||
git.linuxhg.com/john-okeefe/keyboard v0.0.0-20231016005355-05ed8b28800e h1:oCGqHZUhesu14KQUELz0jxIPnL/KcGFhsmNnKoAZMo4=
|
||||
git.linuxhg.com/john-okeefe/keyboard v0.0.0-20231016005355-05ed8b28800e/go.mod h1:zxW/S0TzdzmyfloJ0ZHwMaTjMgfd8hK8TycSEAyMRkY=
|
||||
git.linuxhg.com/john-okeefe/keyboard v0.0.0-20231016161119-714c103574cc h1:J5VWew9I6gCCYVxQ/5NEtsD4wcMZcc4e05V44OiSNi4=
|
||||
|
@ -28,8 +28,10 @@ func main() {
|
||||
double()
|
||||
case "Convert To Celsius":
|
||||
toCelsius()
|
||||
case "Get the Average":
|
||||
average()
|
||||
case "Get the Average My Solution":
|
||||
averageMySolution()
|
||||
case "Get the Average Head First Solution":
|
||||
averageHeadFirstSolution()
|
||||
}
|
||||
return nil
|
||||
})
|
||||
@ -42,7 +44,8 @@ func main() {
|
||||
menu.Option("myIntPointer", nil, false, nil)
|
||||
menu.Option("Double", nil, false, nil)
|
||||
menu.Option("Convert To Celsius", nil, false, nil)
|
||||
menu.Option("Get the Average", nil, false, nil)
|
||||
menu.Option("Get the Average My Solution", nil, false, nil)
|
||||
menu.Option("Get the Average Head First Solution", nil, false, nil)
|
||||
err := menu.Run()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
Loading…
Reference in New Issue
Block a user