Logo

Programming-Idioms

History of Idiom 195 > diff from v16 to v17

Edit summary for version 17 by Pablitoz:
[Go] previous implementation was incorrect

Version 16

2023-02-04, 11:20:46

Version 17

2023-02-04, 12:11:12

Idiom #195 Pass a two-dimensional array

Pass an array a of real numbers to the procedure (resp. function) foo. Output the size of the array, and the sum of all its elements when each element is multiplied with the array indices i and j (assuming they start from one).

Idiom #195 Pass a two-dimensional array

Pass an array a of real numbers to the procedure (resp. function) foo. Output the size of the array, and the sum of all its elements when each element is multiplied with the array indices i and j (assuming they start from one).

Variables
a,foo,i,j
Variables
a,foo,i,j
Imports
import "fmt"
Imports
import "fmt"
Code
func foo(a [][]int) {
	fmt.Println("array is ", len(a)[0], " x ", len(a))
	x := 0
	for _, v1 := range a {
		for _, v2 := range v1 {
			x += v2
		}
	}
	fmt.Println("sum: ", x)
}
Code
func foo(a [][]int) {
	fmt.Println("array is ", len(a)[0], " x ", len(a))
	x := 0
	for i1, v1 := range a {
		for i2, v2 := range v1 {
			x += v2*(i+1)*(j+1)
		}
	}
	fmt.Println("result: ", x)
}