Could someone give me feedback on my algorithm?

Started by
23 comments, last by alvaro 11 years, 4 months ago
In C++, indices start at zero, not one, so your first one should be [font=courier new,courier,monospace]0 + 0 / 8[/font], your second [font=courier new,courier,monospace]1 + 1 / 8[/font], third [font=courier new,courier,monospace]2 + 2 / 8[/font], etc.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
Advertisement
Hey Cornstalks I wrote a test and it works but It is switching 1 square too soon.

The test actually works, but you are misinterpreting the results. Square 1 is the second square of the first row, and square 8 is the first square of the second row. Both square 1 and square 8 should be red. Square 0, the first square of the first row, on the other hand, should be (and is) white.

In C++, indices start at zero, not one, so your first one should be [font=courier new,courier,monospace]0 + 0 / 8[/font], your second [font=courier new,courier,monospace]1 + 1 / 8[/font], third [font=courier new,courier,monospace]2 + 2 / 8[/font], etc.


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

anyways look how elegant you made it look. I am very pleased.


if((k+k/8)%2 == 0) {
(*rects[(k)]).set_fill_color(Color::red);
}
else if((k+k/8)%2 == 1) {
(*rects[(k)]).set_fill_color(Color::white);
}
k++;

[quote name='black_darkness' timestamp='1355441798' post='5010392']Hey Cornstalks I wrote a test and it works but It is switching 1 square too soon.

The test actually works, but you are misinterpreting the results. Square 1 is the second square of the first row, and square 8 is the first square of the second row. Both square 1 and square 8 should be red. Square 0, the first square of the first row, on the other hand, should be (and is) white.
[/quote]


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.


#include <iostream>;
int main()
{
using namespace std;
for(int i=0;i<5;i++)
{
cout<<i<<"\n";
}
return 0;
}


and this was the output.
Untitled.png
bool is_red(int x, int y) {
return (x+y)%2 == 0;
}

// ...
rects[k]->set_fill_color(is_red(k,k/8) ? Color::red : Color::white);
// ...
Couldn't resist bringing this thread to its logical conclusion by one-lining Álvaro's code. 87-fold code compression relative to the initial post. wub.png
rects[k]->set_fill_color(!((k+k/8)%2) ? Color::red : Color::white);
edit: and since it's a beginner thread, public service announcement: mangling code like I just did is evil and has absolutely no point besides comedic purposes; don't do this if you intend to actually use the code for something.
Well then at least remove the "!" by switching the colors around happy.png


rects[k]->set_fill_color((k+k/8)%2 ? Color::white : Color::red);

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 biggrin.png
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 [font=courier new,courier,monospace]int [/font][font=courier new,courier,monospace]i = 0[/font]). 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 smile.png)

// 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->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...
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
To test for odd vs even don't use modulation. Just binary-and with 1.

0 & 1 = 0
1 & 1 = 1
2 & 1 = 0
3 & 1 = 1
4 & 1 = 0
5 & 1 = 1
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

To test for odd vs even don't use modulation. Just binary-and with 1.

This is what I sometimes do, but I'm going to mention this for future readers because I know someone's going to do this and spend some good time debugging:
Operator precedence for [font=courier new,courier,monospace]&[/font] is different than it is for [font=courier new,courier,monospace]%[/font]. [font=courier new,courier,monospace]%[/font] has the same precedence as [font=courier new,courier,monospace]*[/font] and [font=courier new,courier,monospace]/[/font], but [font=courier new,courier,monospace]&[/font] has precedence just below [font=courier new,courier,monospace]==[/font] and [font=courier new,courier,monospace]!=[/font]. Just be aware of this, all you readers.

Example:

if (8 & 1 == 0) // uh oh.. this actually reads as (8 & (1 == 0)), so you better use parentheses!
if (8 % 2 == 0) // this does what you expect and reads as ((8 % 2) == 0)
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

This topic is closed to new replies.

Advertisement