I'm now trying out for loops with different sized iterations (is that the right word?)
This loop
for (int i = 0; i < 5; ++i)
{
for (int j = 0; j < 3; ++j)
{
Console.WriteLine("i = {0} j = {1}", i, j);
}
}
outputs
i = 0 j = 0
i = 0 j = 1
i = 0 j = 2
i = 1 j = 0
i = 1 j = 1
i = 1 j = 2
i = 2 j = 0
i = 2 j = 1
i = 2 j = 2
i = 3 j = 0
i = 3 j = 1
i = 3 j = 2
i = 4 j = 0
i = 4 j = 1
i = 4 j = 2
This has repeating pairs of numbers (e.g. i = 0 j = 2 and i = 2 j = 0)
One of the forum members (apegram) suggested a method for non repeating pairs using
int maxIndex = 5;
for (int i = 0; i <= maxIndex; i++)
{
for (int j = i; j <= maxIndex; j++)
{
Console.WriteLine("i = {0} j = {1}", i, j);
}
}
In this case though maxIndex is no longer the same number. If I use that idiom with my current indices.
for (int i = 0; i < 5; ++i)
{
for (int j = i; j < 3; ++j)
{
Console.WriteLine("i = {0} j = {1}", i, j);
}
}
The output is
i = 0 j = 0
i = 0 j = 1
i = 0 j = 2
i = 1 j = 1
i = 1 j = 2
i = 2 j = 2
There are no repeating pair of numbers but all the possible pairs of numbers haven't been found.
How is it possible to find all pairs of numbers in this case?