在二进制矩阵C#中提供帮助 [英] Help In Binary matrix C#
问题描述
为我提供真实代码的帮助
Help me with Homework please...
I have a question that one place randomly In any matrix lets say [3,2] and I must find and gather the values next to this place let's say for ---example--- if I go this binary matrix
int[,] M = {{7,5,4},{9,8,7},{2,3,4}}; and I must gather the values beside [1,1] I must get 7+5+4+9+7+2+3+4=41 in this case it is easy but how I can gather in please [0,0] in this place I will got 9+8+5=22
Help me in real code please in C#
REMEMBER THE PLACES AND THE MATRIX ALL ARE RANDOMLY (I JUST GIVEN THE REAL PLACE THAT I MUST GATHER THE VALUSE NEAR IT WHICH IS GIVEN BY THE USER)
LET'S SAY THE USER GAVE ME PLACE [0,0] I MUST GATHER ALL VALUES NEAR [0,0] I DO NOT KNOW THE MATRIX!!!
推荐答案
If the place is named [a, b], where a and b are integer variables given by the user, then create a loop, which increments the i variable between a-1 and a+1, for example: ‘for( int i = a-1; i <= a+1; ++i)’.
放置在另一个类似的循环中,该循环会递增另一个变量- j —在 b-1 和 b + 1 之间.
Place inside another similar loop that increments another variable — j — between b-1 and b+1.
然后使用 i 和 j 从矩阵中提取值并将其加和.但是,必须排除不属于矩阵的像元,例如负数. i 和 j ,或者大于矩阵大小.您还必须排除[ a b ]单元格.这可以通过 if 轻松完成,例如:" if(i< 0)继续".
Then use i and j to extract the value from the matrix and add to the sum. However, you must exclude the cells that do not belong to the matrix, such as negative i and j, or which are greater than the matrix sizes. You must also exclude the [a, b] cell. This can be easily done with an if, for example: ‘if( i < 0) continue’.
显示您的代码(如果它不起作用).
这篇关于在二进制矩阵C#中提供帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!