March 2019
Intermediate to advanced
336 pages
9h 9m
English
The add method adds the elements of two 2 x 2 matrices. The following code returns the created matrix by adding the two matrices:
// add methodfunc add(matrix1 [2][2]int, matrix2 [2][2]int) [2][2]int { var m int var l int var sum [2][2]int for l = 0; l < 2; l++ { for m=0; m <2; m++ { sum[l][m] = matrix1[l][m] +matrix2[l][m] } } return sum}
The sum between the two matrices is the result of calling the add method. The parameters that are passed are the matrices to be added, as shown here:
var sum [2][2]int sum = add(matrix1, matrix2)
The example output of the add method is as follows. Adding matrix1 and matrix2 gives a sum matrix:
Read now
Unlock full access