add: array exercises
This commit is contained in:
42
14-arrays/exercises/11-average/solution/main.go
Normal file
42
14-arrays/exercises/11-average/solution/main.go
Normal file
@ -0,0 +1,42 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
args := os.Args[1:]
|
||||
if l := len(args); l == 0 || l > 5 {
|
||||
fmt.Println("Please tell me numbers (maximum 5 numbers).")
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
sum float64
|
||||
nums [5]float64
|
||||
total float64
|
||||
)
|
||||
|
||||
for i, v := range args {
|
||||
n, err := strconv.ParseFloat(v, 64)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
total++
|
||||
nums[i] = n
|
||||
sum += n
|
||||
}
|
||||
|
||||
fmt.Println("Your numbers:", nums)
|
||||
fmt.Println("Average:", sum/total)
|
||||
}
|
Reference in New Issue
Block a user