1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
package main
import
"fmt"
func main() {
/* 数组长度为 5 */
var
balance = []int {100, 1, 2, 7, 4}
var
avg float32
var
sum int
/* 数组作为参数传递给函数 */
avg = getAverage( balance, 5 ) ;
sum = getSum(balance,5);
/* 输出返回的平均值 */
fmt.Printf(
"平均值为: %.2f \n"
, avg );
fmt.Println(
"和为:%d"
,sum);
}
func getAverage(arr []int, size int) float32 {
var
i,sum int
var
avg float32
for
i = 0; i < size;i++ {
sum += arr[i]
}
avg = float32(sum / size)
return
avg ;
}
/*和计算公式*/
func getSum(arr []int,size int) int{
var
i,sum int
for
i = 0; i < size; i++{
fmt.Println(arr[i])
sum += arr[i]
}
return
sum
}
|
本文转自 swq499809608 51CTO博客,原文链接:http://blog.51cto.com/swq499809608/1920886