On 12 Oct 2006 20:51:48 -0700 in comp.lang.c++, "Petrakid"
<petrakid@gmail.comwrote,
Quote:
>Ok i understand your frustration - but I too am quite frustrated. This
>assignment was due yesterday, and i am still not getting the right
>calculations.
OK, I quit reading the other thread after it looked like you were
just going to beat around the bush and not post any code. so I
haven't seen your code before. And now I see it, and this is the
kind of code, pardon me, that just makes me go crazy.
Quote:
>Looking at the code, I see what it's doing, but I can't completely
>tell that it's doing it correctly...
And that's what I'm talking about. And in fact, you say it isn't
doing it correctly, and it's probably not obvious why to anybody.
You have to break code up into chunks simple enough that you can
understand them. You have to build a piece of the solution, and
make sure it's right, before adding another piece.
So now, while I am still crazy, I'll post an example for you.
Warning, this is untested and off the top of my head, so it may
still be wrong -- but if it's wrong, I think it's simple enough for
you to figure out where it went wrong by following the logic and
printing out the named intermediate variables.
I'd approach things something more like this:
// one ranch = three cowboys. A ranch has 6 legs.
// one herd = four horses. A herd has 32 legs.
for (int legs = min; legs <= max; legs++)
{
for (int herds = 1; (herds*32 < legs); ++herds)
{
int horse_legs = herds*32;
int cowboy_legs = legs - horse_legs;
int ranches = cowboy_legs/6;
if (legs == ranches*6 + herds*32) {
// we have a winner
}
}
}