Could someone give me feedback on my algorithm?
This is true. Personally I always use parens to avoid ambiguity and in this case since you're feeding it to a ? operator you can just swap the result order and drop the explicit comparison to zero.
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);
}
You need to read this article, and perhaps some of the comment as well:
http://thedailywtf.com/Articles/ButAnything-Can-Happen!.aspx
The thing you need to learn here is that you are mis-using else-if. There are only two possible outcomes of a number mod 2, ergo it is appropriate to just use an else for the second case.
Lol @ the article.
[source lang="cpp"]for(int i = 0; i < rects.size(); ++i) rects->set_fill_color((i & 1) ? Color::white : Color::red);[/source]
[source lang="cpp"]for(int i = 0; i < rects.size(); ++i) rects->set_fill_color((i & 1) ? Color::white : Color::red);[/source]
To test for odd vs even don't use modulation. Just binary-and with 1.
Use whichever one you find more clear. I personally find %2 more clear than &1 in this situation.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement