Animals last time

P

Petrakid

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. I said "pigs and chickens" because they are barn
animals, and that is really all the program requires. An animal with 2
legs and one with four. I changed it to cowboys and horses because it
seemed more...i dunno...realistic??

Anyways, short of the right numbers printing out, I got the program
working flawlessly. The calculation section looks like:

void Print_Possibilities(int min, int max)
{
int index; // The index number to track combinations
int legs; // The minimum number of legs
int horses; // Number of horses
int cowboys; // Number of cowboys

for (legs = min; legs < max; legs++)
{
for (index = min_cowboys; index < legs; index ++)
{
if ((index * legs_cowboys * min_cowboys) < legs)
{
if ((legs - index * legs_cowboys * min_cowboys) %
(min_horses * legs_horses) == 0)
{
cowboys = index * min_cowboys;
horses = 0;
cout <<setw(COL_WIDTH)<<cowboys;
cout <<setw(COL_WIDTH)<<horses<<endl;
}
else if ((legs - index * legs_cowboys * min_cowboys) %
(min_horses * legs_horses) != 0);
{
cowboys = index * min_cowboys;
horses = ((legs - index * legs_cowboys *
min_cowboys) %
(min_horses * legs_horses));
cout <<setw(COL_WIDTH)<<cowboys;
cout <<setw(COL_WIDTH)<<horses;
}
}
}
}
return;
}

This calculation section is a variation of a snippet that a computer
programming friend of mine sent me. His didn't work at all (yet) and
this works but with the incorrect output. Looking at the code, I see
what it's doing, but I can't completely tell that it's doing it
correctly...I'm not a math wiz! Heck, I'm a pre-seminary student that
knows a little about computers and wanted to try programming.

If someone can simply tell me what i'm doing wrong. Once I see my
error i'll learn a lot more than playing guessing games. Thanks! (by
the way this is all there is for the calculation section. The
variables you see that arent defined are defined at globals in the
beginning of the program....

// Global Constants
const int min_cowboys = 3; // The minimum number of cowboys
const int min_horses = 8; // The minimum numbner of horses
const int legs_cowboys = 2; // The number of legs each cowboy has
const int legs_horses = 4; // The number of legs each horse has
const int COL_WIDTH = 10; // Column Width
 
R

Robert Mabee

Petrakid said:
for (index = min_cowboys; index < legs; index ++)

OK, index is a count of cowboys and not less than the given minimum.
min_cowboys has done its work and doesn't need to be referenced
again. Stop multiplying it into all the subsequent computations!
else if ((legs - index * legs_cowboys * min_cowboys) %
(min_horses * legs_horses) != 0);

Ignoring min_cowboys and min_horses, this checks that the legs left
over after the assumed number of cowboys will make an integral
number of horses, with none left over (remainder == 0). If != 0
then the assumption is bad and nothing should be counted/printed
for this case.

Note the semicolon hidden at the end of the line. That ends the
condition statement, so the next line (which prints something) will
always be executed.
I'm not a math wiz!

Don't let yourself panic and shut down your brain; that's a reflex
conditioned into most of us by really bad teachers. Math is just
common sense, even if you pursue it into concepts with no real-world
counterparts.
 
D

David Harmon

On 12 Oct 2006 20:51:48 -0700 in comp.lang.c++, "Petrakid"
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.
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
}
}
}
 
F

Frederick Gotham

Petrakid posted:
Ok i understand your frustration - but I too am quite frustrated.


I'm having a nice day.

This assignment was due yesterday, and i am still not getting the right
calculations.


Unfortunate circumstance.

I said "pigs and chickens" because they are barn animals,


I thought you said them because they were carbon-based life-forms -- forgive
my ignorance.

and that is really all the program requires.


If you say so.

An animal with 2 legs


A human! Wow, that was easy!

(Unless of course you don't count a human as an animal; in which case I
nominate chicken).

and one with four.

Dog!


I changed it to cowboys and horses because it seemed
more...i dunno...realistic??


I think beer bottles are more realistic than cowboys, and that tubes of PVA
glue are more realistic than horses.

Anyways, short of the right numbers printing out, I got the program
working flawlessly.


Short of becoming president, my presendential campaign worked flawlessly!

The calculation section looks like:


<snip code>

Sorry, you come across as a four year old... can't bring myself to read
through your code.

This calculation section is a variation of a snippet that a computer
programming friend of mine sent me. His didn't work at all (yet) and
this works but with the incorrect output.


My microwave works, except its output is cold.

Looking at the code, I see what it's doing, but I can't completely tell
that it's doing it correctly...I'm not a math wiz!


Study computer programming... it might come in handy!

Heck, I'm a pre-seminary student that knows a little about computers and
wanted to try programming.


Why the defeatist attitude? There has been ten year old expert C++
programmers.

If someone can simply tell me what i'm doing wrong.

Talking.


Once I see my error i'll learn a lot more than playing guessing games.
Thanks! (by the way this is all there is for the calculation section.
The variables you see that arent defined are defined at globals in the
beginning of the program....

// Global Constants
const int min_cowboys = 3; // The minimum number of cowboys
const int min_horses = 8; // The minimum numbner of horses
const int legs_cowboys = 2; // The number of legs each cowboy has
const int legs_horses = 4; // The number of legs each horse has
const int COL_WIDTH = 10; // Column Width


May I suggest you spend less money on pot, and more money on books? Or how
about you get your books from the library, and spend your money on shiny
things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,145
Latest member
web3PRAgeency
Top