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?

Recommended Answers

All 4 Replies

You need to insert some logic here.
If the value for i is less than or equal to 3 (the maximum value for j) then you need to make j start at i to avoid duplicates. If i is greater than j than 3 then there wont be duplicates so j should start at 0.

You can do this by using a variable to set the starting value for j and use an if statement to decide what value to use:

for (int i = 0; i < 5; ++i)
            {
                int start = 0;
                if(i<3)
                    start = i;
                for (int j = start; j < 3; ++j)
                {
                    Console.WriteLine("i = {0} j = {1}", i, j);
                }
            }

Care should be taken here, if you ever decided to change the maximum value for j you would need to update the condition in the if statement to match. You could use a constant field to ensure the logic always matches the loop:

const int JMAX = 3;
            for (int i = 0; i < 5; ++i)
            {
                int start = 0;
                if (i < JMAX)
                    start = i;
                for (int j = start; j < JMAX; ++j)
                {
                    Console.WriteLine("i = {0} j = {1}", i, j);
                }
            }

You can change the value of JMAX in your code to see the effect it has.

commented: Works great under all positive integer conditions :) +0

Wow, thank you both. It evens works when JMAX > i.

const int JMAX= 7;
            int start;

            for (int i = 0; i < 5; ++i)
            {
                start = i < JMAX? i : 0;   // Yay for ternary operator

                for (int j = start; j < JMAX; ++j)
                {
                    Console.WriteLine("i = {0} j = {1}", i, j);
                }
            }

I this how most people would test pairs then?

I'm just trying to think of an application for this in code as the questions came from me playing around with for loops.

Both? aaah...im scitzophrenic lol.
Good use of the ternary operator, thats how i usually write that but i try to avoid adding fluff in sample code, it often confuses more than it helps :p

I cant think of specific examples off the top of mny head, but you might have 2 arrays of values that you want to search for matches, in which case you could use this loop to produce and index for each array.
This kind of exercise is a great way to learn, but dont worry too much about the practical applications :)

Glad the code worked for you. Remember to mark the thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.