# Find number of legs on a farm...

Discussion in 'C++' started by Petrakid, Oct 10, 2006.

1. ### PetrakidGuest

Hey, I have a task to complete. I am trying to figure out the best
way, in C++ to determine the following. There is this farm with pigs
and chickens. Only the legs of the pigs and chickens look exactly the
same, so for a short person to determine how many possible pigs and how
many possible chickens, all they can do is count the total legs of the
two.

- Chickens seem to always run around in groups of 3
- Pigs seem to always clump together in groups of 8
- pigs ALWAYS have 4 legs and chickens ALWAYS have 2 legs

I need to figure out the total number of possibilities ( like there are
300 different possibilities) AND list the possibilities at the users
request (8 pigs and 3 chicken or 600 pigs and 89 chickens, etc).

I CANNOT use arrays or the exact chinese remainder theorem (i can use a
variation of it).

this is an assignment, so I don't need the complete answer. I'm having
the most trouble with the calculation steps.

I've already determined that there will be 2 calculation functions,
though if it can be done with 1 calculation function, that's great.

If yo could help me out, I would appreciate it!!!!!

Petrakid, Oct 10, 2006

2. ### Alf P. SteinbachGuest

* Petrakid:
>
> this is an assignment, so I don't need the complete answer.

Good.

Hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Alf P. Steinbach, Oct 10, 2006

3. ### Victor BazarovGuest

Petrakid wrote:
> Hey, I have a task to complete. I am trying to figure out the best
> way, in C++ to determine the following. There is this farm with pigs
> and chickens. Only the legs of the pigs and chickens look exactly the
> same, so for a short person to determine how many possible pigs and
> how many possible chickens, all they can do is count the total legs
> of the two.
>
> - Chickens seem to always run around in groups of 3
> - Pigs seem to always clump together in groups of 8
> - pigs ALWAYS have 4 legs and chickens ALWAYS have 2 legs
>
> I need to figure out the total number of possibilities ( like there
> are 300 different possibilities) AND list the possibilities at the
> users request (8 pigs and 3 chicken or 600 pigs and 89 chickens, etc).
>
> I CANNOT use arrays or the exact chinese remainder theorem (i can use
> a variation of it).
>
> this is an assignment, so I don't need the complete answer. I'm
> having the most trouble with the calculation steps.
>
> I've already determined that there will be 2 calculation functions,
> though if it can be done with 1 calculation function, that's great.
>
> If yo could help me out, I would appreciate it!!!!!

Well, you have the number of legs, right? You need to figure out
all solutions of

x*3*2 + y*8*4 = N

where 'N' is the number of legs and 'x' and 'y' is the number of
groups of chickens and pigs, respectively. Then you multiply the
'x' by 3 and you get the number of chickens. And my multiplying
'y' by 8 you get the number of pigs.

V
--

Victor Bazarov, Oct 10, 2006
4. ### Frederick GothamGuest

Petrakid posted:

> There is this farm with pigs
> and chickens.

Cool, I like pigs and chickens.

> Only the legs of the pigs and chickens look exactly the
> same,

Extrememly ambiguous statment.

> so for a short person to determine how many possible pigs and how
> many possible chickens, all they can do is count the total legs of the
> two.
>
> - Chickens seem to always run around in groups of 3

That's 6 legs per group then no?

> - Pigs seem to always clump together in groups of 8

That's 32 legs per group, no?

> - pigs ALWAYS have 4 legs and chickens ALWAYS have 2 legs

OH MY GOD ARE YOU SERIOUS?

> If yo could help me out, I would appreciate it!!!!!

If there are 192 legs, then there could be:

(1) Thirty-two groups of chickens, zero groups of pigs.
(2) Six groups of pigs, zero groups of chickens.

Here's how _I_ would go about it:

Start off with the original figure like "92 legs".

(1) Subtract 32 from it.
(2) See if the result % 6 is false.
(3) If so, you have a combination.
(4) Repeat until result < 32

Then do it with 6:

(1) Subtract 6 from it.
(2) See if the result % 32 is false.
(3) If so, you have a combination.
(4) Repeat until result < 6.

Something along those lines in anyway.

--

Frederick Gotham

Frederick Gotham, Oct 10, 2006
5. ### PetrakidGuest

Thanks for the help I'll start plugging in the calculations tonight.
Yeah, yeah I had to make sure everyone understood that the pigs and
chickens aren't deformed or genetically modified in any way!!

Any further ideas are still welcome!

Petrakid wrote:
> Hey, I have a task to complete. I am trying to figure out the best
> way, in C++ to determine the following. There is this farm with pigs
> and chickens. Only the legs of the pigs and chickens look exactly the
> same, so for a short person to determine how many possible pigs and how
> many possible chickens, all they can do is count the total legs of the
> two.
>
> - Chickens seem to always run around in groups of 3
> - Pigs seem to always clump together in groups of 8
> - pigs ALWAYS have 4 legs and chickens ALWAYS have 2 legs
>
> I need to figure out the total number of possibilities ( like there are
> 300 different possibilities) AND list the possibilities at the users
> request (8 pigs and 3 chicken or 600 pigs and 89 chickens, etc).
>
> I CANNOT use arrays or the exact chinese remainder theorem (i can use a
> variation of it).
>
> this is an assignment, so I don't need the complete answer. I'm having
> the most trouble with the calculation steps.
>
> I've already determined that there will be 2 calculation functions,
> though if it can be done with 1 calculation function, that's great.
>
> If yo could help me out, I would appreciate it!!!!!

Petrakid, Oct 10, 2006
6. ### Jim LangstonGuest

"Petrakid" <> wrote in message
news:...
> Thanks for the help I'll start plugging in the calculations tonight.
> Yeah, yeah I had to make sure everyone understood that the pigs and
> chickens aren't deformed or genetically modified in any way!!

I once met a farmer who had chickens with three legs. I asked what the heck
these were, he said him, ma and his son all liked chiken legs so they
developed a breed with three legs. I asked him how they tasted. He said he
didn't know, wasn't able to catch one yet.

>
> Any further ideas are still welcome!
>
> Petrakid wrote:
>> Hey, I have a task to complete. I am trying to figure out the best
>> way, in C++ to determine the following. There is this farm with pigs
>> and chickens. Only the legs of the pigs and chickens look exactly the
>> same, so for a short person to determine how many possible pigs and how
>> many possible chickens, all they can do is count the total legs of the
>> two.
>>
>> - Chickens seem to always run around in groups of 3
>> - Pigs seem to always clump together in groups of 8
>> - pigs ALWAYS have 4 legs and chickens ALWAYS have 2 legs
>>
>> I need to figure out the total number of possibilities ( like there are
>> 300 different possibilities) AND list the possibilities at the users
>> request (8 pigs and 3 chicken or 600 pigs and 89 chickens, etc).
>>
>> I CANNOT use arrays or the exact chinese remainder theorem (i can use a
>> variation of it).
>>
>> this is an assignment, so I don't need the complete answer. I'm having
>> the most trouble with the calculation steps.
>>
>> I've already determined that there will be 2 calculation functions,
>> though if it can be done with 1 calculation function, that's great.
>>
>> If yo could help me out, I would appreciate it!!!!!

>

Jim Langston, Oct 10, 2006
7. ### Kevin HandyGuest

Petrakid wrote:
> Hey, I have a task to complete. I am trying to figure out the best
> way, in C++ to determine the following. There is this farm with pigs
> and chickens. Only the legs of the pigs and chickens look exactly the
> same, so for a short person to determine how many possible pigs and how

How short is this person, that he can't see over the top of a chicken?
Or are these those strange KFC critters?

> many possible chickens, all they can do is count the total legs of the
> two.
>
> - Chickens seem to always run around in groups of 3
> - Pigs seem to always clump together in groups of 8
> - pigs ALWAYS have 4 legs and chickens ALWAYS have 2 legs

What about the "he was too good to eat all at once" thing?

Are you really ruling out KFC chickens? Then your guy must be really
really short.

> I need to figure out the total number of possibilities ( like there are
> 300 different possibilities) AND list the possibilities at the users
> request (8 pigs and 3 chicken or 600 pigs and 89 chickens, etc).
>
> I CANNOT use arrays or the exact chinese remainder theorem (i can use a
> variation of it).

What is the "chinese remainder theorem"?

> this is an assignment, so I don't need the complete answer. I'm having
> the most trouble with the calculation steps.

Don't worry about individual critters. Handle them as
a '3-chicken' with 6 legs, or a '8-pig' with 32 legs.
Then just loop through all possible 8-pigs, and see if
the remainder fits as a 3-chicken (no left over legs,
which would get messy).

> I've already determined that there will be 2 calculation functions,
> though if it can be done with 1 calculation function, that's great.
>
> If yo could help me out, I would appreciate it!!!!!
>

You should then target the e-mail at 'yo', whoever he is.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Kevin Handy, Oct 11, 2006
8. ### PetrakidGuest

Alright, now if I wanted to do this as a range (say numbers between
500-1000), what would I do? Sorry to ask these things, but the
instructor tends to be pretty vague when it comes to this stuff, and
i'm using any resource i can to find help

Thanks

Petrakid wrote:
> Hey, I have a task to complete. I am trying to figure out the best
> way, in C++ to determine the following. There is this farm with pigs
> and chickens. Only the legs of the pigs and chickens look exactly the
> same, so for a short person to determine how many possible pigs and how
> many possible chickens, all they can do is count the total legs of the
> two.
>
> - Chickens seem to always run around in groups of 3
> - Pigs seem to always clump together in groups of 8
> - pigs ALWAYS have 4 legs and chickens ALWAYS have 2 legs
>
> I need to figure out the total number of possibilities ( like there are
> 300 different possibilities) AND list the possibilities at the users
> request (8 pigs and 3 chicken or 600 pigs and 89 chickens, etc).
>
> I CANNOT use arrays or the exact chinese remainder theorem (i can use a
> variation of it).
>
> this is an assignment, so I don't need the complete answer. I'm having
> the most trouble with the calculation steps.
>
> I've already determined that there will be 2 calculation functions,
> though if it can be done with 1 calculation function, that's great.
>
> If yo could help me out, I would appreciate it!!!!!

Petrakid, Oct 11, 2006
9. ### osmiumGuest

"Petrakid" writes:

> Alright, now if I wanted to do this as a range (say numbers between
> 500-1000), what would I do? Sorry to ask these things, but the
> instructor tends to be pretty vague when it comes to this stuff, and
> i'm using any resource i can to find help

I assume "this" means you want the user to enter numbers only in the range
300-500. If the user enters a number outside of that range, tell him the
rules (again, perhaps) and tell him to re-enter the number. Note that the
valid entries always take the form:

n = 6c + 32 p
where c is the number of groups of chickens and p is the number of groups
of pigs.

osmium, Oct 11, 2006
10. ### PetrakidGuest

Not necessarily. The range should be kept limited to keep the
processor from going crazy, but there is no limit to the range in the
program - just in the instructions.

Regardless, i'm assuming that the same formula n = 6c + 32p still
stands.

osmium wrote:
> "Petrakid" writes:
>
> > Alright, now if I wanted to do this as a range (say numbers between
> > 500-1000), what would I do? Sorry to ask these things, but the
> > instructor tends to be pretty vague when it comes to this stuff, and
> > i'm using any resource i can to find help

>
> I assume "this" means you want the user to enter numbers only in the range
> 300-500. If the user enters a number outside of that range, tell him the
> rules (again, perhaps) and tell him to re-enter the number. Note that the
> valid entries always take the form:
>
> n = 6c + 32 p
> where c is the number of groups of chickens and p is the number of groups
> of pigs.

Petrakid, Oct 11, 2006
11. ### PetrakidGuest

Got that part and yes these chicken are VERY BIG KFC chicken - they can
kill!!

Check out

to see where I'm at now.j

Thanks!!

Kevin Handy wrote:
> Petrakid wrote:
> > Hey, I have a task to complete. I am trying to figure out the best
> > way, in C++ to determine the following. There is this farm with pigs
> > and chickens. Only the legs of the pigs and chickens look exactly the
> > same, so for a short person to determine how many possible pigs and how

>
> How short is this person, that he can't see over the top of a chicken?
> Or are these those strange KFC critters?
>
> > many possible chickens, all they can do is count the total legs of the
> > two.
> >
> > - Chickens seem to always run around in groups of 3
> > - Pigs seem to always clump together in groups of 8
> > - pigs ALWAYS have 4 legs and chickens ALWAYS have 2 legs

>
> What about the "he was too good to eat all at once" thing?
>
> Are you really ruling out KFC chickens? Then your guy must be really
> really short.
>
> > I need to figure out the total number of possibilities ( like there are
> > 300 different possibilities) AND list the possibilities at the users
> > request (8 pigs and 3 chicken or 600 pigs and 89 chickens, etc).
> >
> > I CANNOT use arrays or the exact chinese remainder theorem (i can use a
> > variation of it).

>
> What is the "chinese remainder theorem"?
>
> > this is an assignment, so I don't need the complete answer. I'm having
> > the most trouble with the calculation steps.

>
> Don't worry about individual critters. Handle them as
> a '3-chicken' with 6 legs, or a '8-pig' with 32 legs.
> Then just loop through all possible 8-pigs, and see if
> the remainder fits as a 3-chicken (no left over legs,
> which would get messy).
>
> > I've already determined that there will be 2 calculation functions,
> > though if it can be done with 1 calculation function, that's great.
> >
> > If yo could help me out, I would appreciate it!!!!!
> >

>
> You should then target the e-mail at 'yo', whoever he is.
>
> ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
> http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
> ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Petrakid, Oct 12, 2006
12. ### Duane HebertGuest

"Kevin Handy" <> wrote in message
news:...
> Petrakid wrote:
> What is the "chinese remainder theorem"?

http://www.cut-the-knot.org/blue/chinese.shtml
oddly enough...

>
> You should then target the e-mail at 'yo', whoever he is.

can't argue with that.

> ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet
> News==----
> http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+
> Newsgroups
> ----= East and West-Coast Server Farms - Total Privacy via Encryption
> =----

Duane Hebert, Oct 12, 2006
13. ### PetrakidGuest

It wasn't me who asked that - I know what the theorem is - i just can't
use it...directly...in the program.

I got the program printing out numbers now, and printing out the total
of possible solutions, but the solutions and numbers it's printing
aren't right.

So the program itself is setup right, it's just the arithmatic that is
wrong. I'm going through it step by step, and maybe I'll find the
problem. I also posted the program up here to look at, but I have
since changed it:

#include <iostream>
#include <iomanip>
using namespace std;

// Global Constants
const int min_cowboys = 3;
const int min_horses = 8;
const int legs_cowboys = 2;
const int legs_horses = 4;
const int COL_WIDTH = 10;

void Instructions();
void Print_Possibilities(int min, int max);
int Calculate_Possibilities(int min, int max);

int main()
{
int min_legs;
int max_legs;
int total_poss;

void Instructions();
do
{
cout << "Please enter the lowest number in the range: ";
cin >> min_legs;
cout << "Next, enter the highest number in the range: ";
cin >> max_legs;

if (min_legs < (min_horses * legs_horses))
{
cout << "You have entered an invalid range!"<<endl;
} else
{
total_poss = Calculate_Possibilities(min_legs, max_legs);

cout << "There are a total of "<<total_poss<<" ways to
group this"<<endl;
cout << "many legs."<<endl;
cout << "Would you like to see the possibilities? ";
{
cout <<setw(COL_WIDTH)<<"Cowboys";
cout <<setw(COL_WIDTH)<<"Horses"<<endl;
Print_Possibilities(min_legs, max_legs);
cout <<endl;

}
else
cout <<endl;
}
cout << "Would you like to try again? ";
return(0);
}

int Calculate_Possibilities(int min, int max)
{
int index;
int legs;
int possibilities;

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)
{
possibilities++;
}
}
}
}
return(index);
}

void Print_Possibilities(int min, int max)
{
int index;
int legs;

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)
{
cout <<setw(COL_WIDTH)<<index * 3;
cout <<setw(COL_WIDTH)<<endl;
}
}
}
}
return;
}

void Instructions()
{
cout << "This program will look at a given range of numbers and
determine"<<endl;
cout << "the total number of possible leg combinations, given that
all"<<endl;
cout << "cowboys have 2 legs and all horses have 4 legs. It is
also given"<<endl;
cout << "that there are no less than 3 cowboys and no less than 8
horses in"<<endl;
cout << "the corral at any time. Because both cowboys and horses,
in general"<<endl;
cout << "have an even number of legs, no odd numbers will be
considered."<<endl;
return;
}

The calculation portion is a suggestion from a good friend of mine.

roy

Duane Hebert wrote:
> "Kevin Handy" <> wrote in message
> news:...
> > Petrakid wrote:
> > What is the "chinese remainder theorem"?

> http://www.cut-the-knot.org/blue/chinese.shtml
> oddly enough...
>
> >
> > You should then target the e-mail at 'yo', whoever he is.

>
> can't argue with that.
>
> > ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet
> > News==----
> > http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+
> > Newsgroups
> > ----= East and West-Coast Server Farms - Total Privacy via Encryption
> > =----

Petrakid, Oct 12, 2006
14. ### Earl PurpleGuest

Petrakid wrote:
> // Global Constants
> const int min_cowboys = 3;
> const int min_horses = 8;
> const int legs_cowboys = 2;
> const int legs_horses = 4;
> const int COL_WIDTH = 10;

Where did cowboys and horses come in (what no indians?) I thought we
were dealing with pigs and chickens.

Anyway, if we had groups of 32 and groups of 6, the first ambiguous
number would be 96. The highest even number that can't appear is 58.

Earl Purple, Oct 12, 2006
15. ### PetrakidGuest

Look, the person counting can't tell the difference between cowboys,
chickens, horses OR pigs. Do ya think it matters to them whether the
program prints out as cowboys or chickens? Obviously they aren't too
connected to reality anyway. :-D

Earl Purple wrote:
> Petrakid wrote:
> > // Global Constants
> > const int min_cowboys = 3;
> > const int min_horses = 8;
> > const int legs_cowboys = 2;
> > const int legs_horses = 4;
> > const int COL_WIDTH = 10;

>
> Where did cowboys and horses come in (what no indians?) I thought we
> were dealing with pigs and chickens.
>
> Anyway, if we had groups of 32 and groups of 6, the first ambiguous
> number would be 96. The highest even number that can't appear is 58.

Petrakid, Oct 12, 2006
16. ### osmiumGuest

"Petrakid" wrote:

> It wasn't me who asked that - I know what the theorem is - i just can't
> use it...directly...in the program.
>
> I got the program printing out numbers now, and printing out the total
> of possible solutions, but the solutions and numbers it's printing
> aren't right.
>
> So the program itself is setup right, it's just the arithmatic that is
> wrong. I'm going through it step by step, and maybe I'll find the
> problem. I also posted the program up here to look at, but I have
> since changed it:

I find your posting technique confusing. You now have two threads and two
sets of names (cowboys and pigs) for one problem. Then you post code for
people to "look at" but it is not your current code. Or is it the *problem*
you have changed, rather than the code?

Nevertheless, I will point out one fragment of this code that I find
interesting.

<snip>

> int Calculate_Possibilities(int min, int max)
> {
> int index;
> int legs;
> int possibilities;
>
> 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)
> {
> possibilities++;

How can it make sense to add something to possibilities? Since you didn't
initialize it, it contains garbage.

You also say in the rules that you will ignore odd numbers. Perhaps it is
there but I didn't see the code to back up that assertion.

osmium, Oct 12, 2006
17. ### BobRGuest

Earl Purple wrote in message
<>...
>
>Petrakid wrote:
>> // Global Constants
>> const int min_cowboys = 3;
>> const int min_horses = 8;
>> const int legs_cowboys = 2;
>> const int legs_horses = 4;
>> const int COL_WIDTH = 10;

>
>Where did cowboys and horses come in (what no indians?) I thought we
>were dealing with pigs and chickens.

Sold the farm for a ranch!
If the three cowboys were on the horses (something ONLY the indians and Matt
Dillon could determine. [1]), then there would only be (min) 32 legs to
consider ( don't count the cowboy legs when they are on horses).

The cowboys ate the chickens and smoked the hams (oooou, the colors! the
colors!).

Huh?!?

To OP:
You did work all this out with pencil-n-paper before you started your code,
right??
It's SOP.

[1] - ref. Hollywood western movies.
--
Bob <G> R
POVrookie

BobR, Oct 14, 2006