2024-02-16 18:26:12 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
var result float64
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if len(os.Args) < 2 {
|
|
|
|
fmt.Println("Usage: ./sum-args 1 2 3 4 5 6")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i < len(os.Args); i++ {
|
2024-02-17 21:41:21 +01:00
|
|
|
sum, _ := strconv.ParseFloat(os.Args[i], 64)
|
2024-02-16 18:26:12 +01:00
|
|
|
result = result + float64(sum)
|
|
|
|
}
|
|
|
|
|
2024-02-17 21:41:21 +01:00
|
|
|
averageResult := getAverage(result, len(os.Args)-1)
|
|
|
|
|
|
|
|
fmt.Printf("Total sum: %.4v\n", result)
|
|
|
|
fmt.Printf("Average: %.4v\n", averageResult)
|
|
|
|
}
|
|
|
|
func getAverage(num float64, quantity int) (finalNum float64) {
|
|
|
|
finalNum = num / float64(quantity)
|
|
|
|
return finalNum
|
2024-02-16 18:26:12 +01:00
|
|
|
}
|