https://github.com/gagniuc/single-for-loop-traversal-of-2d-arrays
It demonstrates the use of a single "for-loop" in traversing a matrix. The example shown here is made in 10 programming languages: C#, C++, Java, Javascript, PERL, PHP, Python, Ruby and VB/VBA.
https://github.com/gagniuc/single-for-loop-traversal-of-2d-arrays
array cpp csharp java javascript matrix perl php python ruby vb6 vba vba-excel vba-macros
Last synced: about 2 months ago
JSON representation
It demonstrates the use of a single "for-loop" in traversing a matrix. The example shown here is made in 10 programming languages: C#, C++, Java, Javascript, PERL, PHP, Python, Ruby and VB/VBA.
- Host: GitHub
- URL: https://github.com/gagniuc/single-for-loop-traversal-of-2d-arrays
- Owner: Gagniuc
- License: mit
- Created: 2023-04-07T21:34:06.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-04-08T07:17:20.000Z (about 2 years ago)
- Last Synced: 2025-01-15T07:31:53.063Z (3 months ago)
- Topics: array, cpp, csharp, java, javascript, matrix, perl, php, python, ruby, vb6, vba, vba-excel, vba-macros
- Language: Java
- Homepage: https://link.springer.com/book/10.1007/978-3-031-23277-0
- Size: 37.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Single for-loop traversal over 2D arrays
It demonstrates the use of a single for-loop for two-dimensional arrays. It shows the traversal of a two-dimensional array by one for-loop structure. A 2D-array variable (A) is declared. A string variable t is initially set to empty. A variable v is set to zero and it represents the main counter of the for-loop. Another two variables (ie. i and j) are initialized with value zero and are the main coordinates for element identification. Each dimension of array A is stored in variables n and m, namely the number of rows in n and the number of columns in m. The upper limit of the for-loop is calculated based on the two known dimensions n and m. Thus, m times n establishes the upper limit of the for-loop. Here, the value of the counter v from the for-loop is used to calculate the i and j values that are used as an index to traverse the array variable A. The value of variable j is computed as the v % m and the result of this expression indicates the reminder (ex. 5 mod 3 is 2). The secret to this implementation is a condition that increments a variable i (rows) each time j (columns) equals zero. Thus, in this manner this approach provides the i and j values that a nested for-loop provides. At each iteration, the value from an element is added to the content of variable t. Once the end of the for-loop is reached, the value collected in variable t is printed in the output for inspection. Please also see: [Single "for-loop" traversal over three dimensional arrays](https://github.com/Gagniuc/Single-for-loop-traversal-of-3D-arrays).
# C#
```C#
using System;
class HelloWorld {
static void Main() {
string[, ] A = new string[, ] {
{"a","b"},
{"c","d"},
{"e","f"},
{"g","h"}
};
string t = "";
int n = A.GetLength(0);
int m = A.GetLength(1);
int i = 0;
int j = 0;for (int v = 0; v < n*m; v++) {
j = v % m;
if(v!=0 && j == 0){i++;}
t += v + " A["+i+","+j+"]=";
t += A[i,j] + "\n";
}Console.WriteLine(t);
}
}
```# C++
```c++
#include
using namespace std;int main()
{
string A[][2] = {
{"a","b"},
{"c","d"},
{"e","f"},
{"g","h"},
};
string t = "";
int n = sizeof(A) / sizeof A[0];
int m = sizeof A[0] / sizeof(string);int i = 0;
int j = 0;for (int v = 0; v < n*m; v++) {
j = v % m;
if(v!=0 && j == 0){i++;}
t += to_string(v);
t += " A["+to_string(i)+"][";
t += to_string(j)+"]=";
t += A[i][j] + "\n";
}
cout<
```# Perl
```perlmy @A = (
["a", 88, 146],
["b", 34, 124],
["c", 96, 564],
[100, 12, "d"],
);my $t = "";
my $n = $#A + 1; # rows
my $m = $#{$A[0]} + 1; # columnsmy $i = 0;
my $j = 0;for ($v = 0; $v < $n*$m; $v++) {
$j = $v % $m;
if($v != 0 && $j == 0){$i++;}$t .= $v . " A[".$i."][".$j."]=";
$t .= $A[$i][$j] . "\n";
}print $t;
```# Python
```pythonA = [["a", 88, 146],
["b", 34, 124],
["c", 96, 564],
[100, 12, "d"]]t = ""
n = len(A) # rows
m = len(A[0]) # columnsi = 0
j = 0for v in range(0, n*m):
j = v % m
if(v != 0 and j == 0):
i = i + 1
t += str(v) + " A[" + str(i) + "]["
t += str(j) + "]=" + str(A[i][j]) + "\n"print(t)
```# Ruby
```ruby
A = [
["a", 88, 146],
["b", 34, 124],
["c", 96, 564],
[100, 12, "d"],
]t = ""
n = (A.size) # rows
m = (A[0].size) # columnsi = 0
j = 0for v in 0..(n*m)-1
j = v % m
if(v != 0 and j == 0)
i = i + 1
end
t += v.to_s + " A[" + i.to_s + "]["
t += j.to_s + "]=" + A[i][j].to_s + "\n"
endputs(t)
```# VB
```vb
Sub main()Dim A(0 To 1, 0 To 2) As String
Dim i As Integer
Dim t As StringA(0, 0) = "a"
A(0, 1) = "b"
A(0, 2) = "c"
A(1, 0) = "d"
A(1, 1) = "e"
A(1, 2) = "f"
' rows
n = UBound(A, 1) - LBound(A, 1) + 1
' columns
m = UBound(A, 2) - LBound(A, 2) + 1
i = 0
j = 0
For v = 0 To (n * m) - 1
j = v Mod mIf (v <> 0 And j = 0) Then i = i + 1
t = t & v & " A(" & i & "," & j & ")="
t = t & A(i, j) & vbCrLfNext v
Debug.Print (t)End Sub
```# References
- Paul A. Gagniuc. An Introduction to Programming Languages: Simultaneous Learning in Multiple Coding Environments. Synthesis Lectures on Computer Science. Springer International Publishing, 2023, pp. 1-280.