• Create Account

### #ActualCornstalks

Posted 13 December 2012 - 09:13 PM

Thank you for the algorithm. I will have to keep this in mind. Is there a formal name for this type of algorithm?

Yes, the formal name is the Cornstalks Algorithm
Just kidding, there isn't a formal name for it. It might be possible to generalize it to a larger, more complex algorithm that has a formal name, but I just came up with this off the top of my head. Years of programming will do that to you...

I always thought that if you wrote a for loop it added one then ran what was inside the curly braces. oops. You were right. I wrote this test. I always did get a lot of 1 off errors.

I'll break down a for loop like this:
for (A; B; C)
D;

Step 1: A is done (A is usually creating and setting a variable, like int i = 0). Step 2: B is checked (B is the looping condition, and as long as it's true the loop is run). Step 3: D is run. Step 4: C is run (which is usually what updates the loop counter). Then it goes back to step 2 and repeats until B is false.

Couldn't resist bringing this thread to its logical conclusion by one-lining Álvaro's code.

One liner? (Added the loop )
// This:
for (int y = 0; y < 8; ++y) for (int x = 0; x < 8; ++x) rects[x + y * 8]->set_fill_color(((x + y) % 2) ? Color::white : Color::red);

// Or:
for (int i = 0; i < rects.size(); ++i) rects[i]->set_fill_color(((i + i / 8) % 2) ? Color::white : Color::red);

// But seriously, I hope no one ever does this in real life. Use *at least* two lines...


### #2Cornstalks

Posted 13 December 2012 - 09:13 PM

Thank you for the algorithm. I will have to keep this in mind. Is there a formal name for this type of algorithm?

Yes, the formal name is the Cornstalks Algorithm
Just kidding, there isn't a formal name for it. It might be possible to generalize it to a larger, more complex algorithm that has a formal name, but I just came up with this off the top of my head. Years of programming will do that to you...

I always thought that if you wrote a for loop it added one then ran what was inside the curly braces. oops. You were right. I wrote this test. I always did get a lot of 1 off errors.

I'll break down a for loop like this:
for (A; B; C)
D;

Step 1: A is done (A is usually creating and setting a variable, like int i = 0). Step 2: B is checked (B is the looping condition, and as long as it's true the loop is run). Step 3: D is run. Step 4: C is run (which is usually what updates the loop counter). Then it goes back to step 2 and repeats until B is false.

Couldn't resist bringing this thread to its logical conclusion by one-lining Álvaro's code.

One liner? (Added the loop )
// This:
for (int y = 0; y < 8; ++y) for (int x = 0; x < 8; ++x) rects[x + y * 8]->set_fill_color(((x + y) % 2) ? Color::white : Color::red);

// Or:
for (int i = 0; i < rects.size(); ++i) rects[i]->set_fill_color(((i + i / 8) % 2) ? Color::white : Color::red);

// But seriously, I hope no one ever does this in real life. Use *at least* two lines...


### #1Cornstalks

Posted 13 December 2012 - 09:12 PM

Thank you for the algorithm. I will have to keep this in mind. Is there a formal name for this type of algorithm?

Yes, the formal name is the Cornstalks Algorithm ;)
Just kidding, there isn't a formal name for it. It might be possible to generalize it to a larger, more complex algorithm that has a formal name, but I just came up with this off the top of my head. Years of programming will do that to you...

I always thought that if you wrote a for loop it added one then ran what was inside the curly braces. oops. You were right. I wrote this test. I always did get a lot of 1 off errors.

I'll break down a for loop like this:
for (A; B; C)
D;

Step 1: A is done (A is usually creating and setting a variable, like int i = 0). Step 2: B is checked (B is the looping condition, and as long as it's true the loop is run). Step 3: D is run. Step 4: C is run (which is usually what updates the loop counter). Then it goes back to step 2 and repeats until B is false.

Couldn't resist bringing this thread to its logical conclusion by one-lining Álvaro's code.

One liner? (Added the loop )
// This:
for (int y = 0; y < 8; ++y) for (int x = 0; x < 8; ++x) rects[x + y * 8]->set_fill_color(((x + y) % 2) ? Color::white : Color::red);

// Or:
for (int i = 0; i < rects.size(); ++i) rects[i]->set_fill_color(((i + i / 8) % 2) ? Color::white : Color::red);

// But seriously, I hope no one ever does this in real life. Use *at least* two lines...


PARTNERS