Iteration across M x N matrix in pseudocode?
-
26-10-2019 - |
Question
Well, I can't post code because of some bug, so I'm going to pseudocode you guys. Please, respond with code and math. I am terrible at math, and trying to figure math out via code is an impossible task for me.
My psuedocode uses only numbers and letters, which will form a 2x2 matrix, but I want to be able to iterate across m by n matrices in this fashion. I just think that it's easier to go from specific (no variables) to general (variable-inclusive).
Letter a, letter b; number 1, number 2 ==>
Letter a id=1, letter b id=1 |
number 1 id=1, number 2 id=2 |
Letter_by_Number 1by1, Letter_by_Number 1by2, Letter_by_Number 2by1, Letter_by_Number 2by2
Can anyone tell me how to do this in C++, Python, PHP's SimpleXML, or XSL? If I can get the math down in any of these languages, I think I can figure everything else out.
Solution
Something like this?
$m = 2;
$n = 2;
for($i = 1; $i <= $m; $i++) {
for($j = 1; $j <= $n; $j++) {
echo $i." x ".$j."\n";
}
}
Results:
1 x 1
1 x 2
2 x 1
2 x 2
OTHER TIPS
You just want to iterate through a 2-dimensional array. In pseudocode you'd do something like:
for ( i = 0; i < M; i++ ) {
for ( j = 0; j < N; j++ ) {
do stuff with array element i,j
}
}