Nested loops

Hi ! i'trying to output a list like this:
255-255-0 to 255-255-64 ----> 255-254-0 to 255-254-64 ...... 255-0-0 to 255-0-64
254-255-0 to 254-255-64 254-254-0 to 254-254-64 ...... 254-0-0 to 254-0-64
253-255-0 to 253-255-64 253-254-0 to 253-254-64 ...... 253-0-0 to 253-0-64
...... ...... ......
0-255-0 to 0-255-64 0-254-0 to 0-254-64 ...... 0-0-0 to 0-0-64


I'm trying using nested loops but the output is not okay, i'm doing something wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    for (int k = 0; k < 256; k++){

    for (int i = 0; i < 256; i++){

        int hue = 255-i;

    for (int j = 0; j < 65; j++){
            
            int sat = 255-k;

   cout << ofToString(hue)+"-" +ofToString(sat)+"-"+ofToString(j) << endl;
         }
       }
    }
Last edited on
step back and figure out the algorithm to generate what you want. I can't make sense of what you posted, it looks like RBG to HSL (?) (in which case use the algorithm for that). If its just some sort of pattern generator, I don't see the pattern.
Hello egroj1,

Welcome to the forum.

Your little bit of code does not tell me what "ofToString(hue)" function is. Nor is there enough of the program to test it with out the "ofToString" function.

"int hue" is defined in the second for loop and only has scope in that for loop. You need to define "hew" outside of any for loop to be able to use it in another for loop. Looking at the revised code it might work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
for (int k = 0; k < 256; k++)
{

	for (int i = 0; i < 256; i++)
	{

		int hue = 255 - i;

		for (int j = 0; j < 65; j++)
		{

			int sat = 255 - k;

			cout << ofToString(hue) + "-" + ofToString(sat) + "-" + ofToString(j) << endl;
		}
	}
}

Notice how the {}s line up in the same column. This makes the code much easier to read.

I will see what I can do to test this and see what happens.

FYI For the future, this set of for loop maybe where you think the problem is, but it might start somewhere else. In other words one for loop calls the function "ofToString", but no one can see what actually happens there. Most times it is better to show enough code to be able to compile and test than just part.

Hope that helps,

Andy
It would help if you posted code that actually compiles, and with proper indentation for readability. You are making us put more effort into it (and therefore, are less likely to get replies) lol nevermind by having us figure out what you mean when you say the output is "not okay".

A common way to deal with problems like these is to start with smaller numbers.
Instead of 256, try 5. Instead of 65, try 3.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Example program
#include <iostream>

int main()
{
    int n = 5; // 256
    int m = 3; // 65
    for (int k = 0; k < n; k++) {

        for (int i = 0; i < n; i++) {

            int hue = (n-1)-i;
            for (int j = 0; j < m; j++) {

                int sat = (n-1)-k;
                std::cout << hue << "-" << sat << "-" << j << "\n";
            }
        }
    }
}



4-4-0
4-4-1
4-4-2
3-4-0
3-4-1
3-4-2
2-4-0
2-4-1
2-4-2
1-4-0
1-4-1
1-4-2
0-4-0
0-4-1
0-4-2
4-3-0
...


As you can see, your first number is decreasing ( 4 -> 3 -> 2 ...), but your second number is staying as the max value (4) for many iterations.

Your sat variable is the one that relies on k, and therefore will decrease the slowest. Perhaps you meant to make this your hue variable?

Also, check out:
https://blog.codinghorror.com/rubber-duck-problem-solving/
https://en.wikipedia.org/wiki/Rubber_duck_debugging
Last edited on
Hi Andy,

Thanks, ofToString is just converting the the int to a string so i can see the output.
When i submit the question the spaces of my question change here is a link of the pattern i need to output is clearly here.

https://ibb.co/b6xCWJ
Last edited on
C++'s operator<< for std::cout will do the conversion for you.
std::cout << hue << "-" << sat << "-" << j << "\n";
hi egroj1,

In expected output, why are there sets of numbers next to each other? Can you just write it in one vertical list?

e.g.

A
B
C
...
X
Y
Z
a
b
c
...
x
y
z
0
1
2
...
7
8
9


Then it'll probably be much clearer if there's a pattern or not. As of now I can't tell if you want all the black first, and then the blue, or the black/blue/purple/green mixed in together, etc.
Hello egroj1,

Ganado has two good points. Until you get it working the way that you want a smaller size loop will let you see more of what is happening.

For Ganado's second post the "cout" statement can handle all basic type variables like "int", "double" and "string" all in the same line. Just remember to separate everything with "<<" not "+".

Your for loops work, but not necessarily the right way. With what you have now I believe that the numbers are changing at the wrong time. I am thinking that you would start with 255 - 255 - 0 and next 255 - 255 - 1 until you reach 255 - 255 - 64. then the next set would be 255 - 254 - 0 to 255 - 254 - 64. Then when the middle number reaches 0 the first number would change to 254 and the middle and third numbers would start all over again.

I will have to shorten the for loops and do some more testing.

Hope that helps,

Andy
Hello egroj1,

I came up with this. See what you think of it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	for (int k = 0; k < 6; k++)
	{
		int hue = 255 - k;

		for (int i = 0; i < 6; i++)
		{
			int sat = 255 - i;

			for (int j = 0; j < 5; j++)
			{
				std::cout << hue << " - " << sat << " - " << j << ((j + 1) % 5 == 0 ? "\n" : "  ");
			}

			std::cout << std::endl;
		}
	}


And this is a bit of the output it produces:
255 - 255 - 0  255 - 255 - 1  255 - 255 - 2  255 - 255 - 3  255 - 255 - 4

255 - 254 - 0  255 - 254 - 1  255 - 254 - 2  255 - 254 - 3  255 - 254 - 4

255 - 253 - 0  255 - 253 - 1  255 - 253 - 2  255 - 253 - 3  255 - 253 - 4

255 - 252 - 0  255 - 252 - 1  255 - 252 - 2  255 - 252 - 3  255 - 252 - 4

255 - 251 - 0  255 - 251 - 1  255 - 251 - 2  255 - 251 - 3  255 - 251 - 4

255 - 250 - 0  255 - 250 - 1  255 - 250 - 2  255 - 250 - 3  255 - 250 - 4

254 - 255 - 0  254 - 255 - 1  254 - 255 - 2  254 - 255 - 3  254 - 255 - 4

254 - 254 - 0  254 - 254 - 1  254 - 254 - 2  254 - 254 - 3  254 - 254 - 4
In the end you might consider sending this output to a file or restricting the output to the screen to just a small portion.

Hope that helps,

Andy
Topic archived. No new replies allowed.