How to find a decommission pole in the matrix
-
package main import "fmt" func main() { array:=[4][4]int {{2,1,6,5}, {7,6,8,0},{4,6,3,1},{5,6,7,4}} b:=0; for i:=0;i<4;i++{ for j:=0;j<5;j++{ if array[i+1][j]<array[i][j] { b=b+1 } } } fmt.Println(b) }
-
array:=[4][4]int {{2,1,6,5}, {7,6,8,0},{4,6,3,1},{5,6,7,4}} ^^^^^^ b:=0; for i:=0;i<4;i++{ ^^^^^^^^ for j:=0;j<5;j++{ ^^^^^^^^ if array[i+1][j]<array[i][j] { ^^^^^^^^ b=b+1 } } }
The size of the 4x4, but the indexing begins with 0, which means the ratios are 0, 1, 2, 3.
For the latest cycle
i
You'.i = 3
Which means handlingarray[i+1][j]
by indexarray[3+1][j]
outside the range.For the latest cycle
j
You'll have an appeal.array[i+1][4]
- back outside the range.So both cycles need to end just a little sooner to stay outside the range.
With regard to the essence of the task, for example, you may consider the collapsing elements in the internal cycle, which you are doing now. Only
b
We need to be forced before the internal cycle starts, and after the internal cycle, we need to check thatb
coincides with the number of internal cycle terations, and in this case, the index of departing poles can be increased, for which you need another separate variable. It is possible to do this differently, to set a flag in front of the internal cycle and to drop it if the condition of exit is not fulfilled at least once. After the cycle, check this condition and change or not change the compendium of reduced columns.