Find number of legs on a farm...

P

Petrakid

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!!!!!
 
V

Victor Bazarov

Petrakid said:
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
 
F

Frederick Gotham

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.
 
P

Petrakid

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!
 
J

Jim Langston

Petrakid said:
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.
 
K

Kevin Handy

Petrakid said:
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.
 
P

Petrakid

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
 
O

osmium

Petrakid said:
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.
 
P

Petrakid

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.
 
P

Petrakid

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;
char answer;

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? ";
cin >> answer;
if ((answer == 'y') || (answer == 'Y'))
{
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? ";
cin >> answer;
} while ((answer == 'y') || (answer == 'Y'));
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
 
E

Earl Purple

Petrakid said:
// 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.
 
P

Petrakid

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
 
O

osmium

Petrakid said:
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.

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.
 
B

BobR

Earl Purple wrote in message
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.
 

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

Staff online

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top