Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Pig/Chicken question revisited.

8 views
Skip to first unread message

Petrakid

unread,
Oct 11, 2006, 2:15:03 PM10/11/06
to
Alright, so I asked earlier about this pig/chicken leg count thing and
got some good help with it, thanks.

I come to find out now that I do not supply just one number, but a
range of numbers. So there could be between 500 and 1000 chicken/pig
legs that I need to input and the program must determine all of the
possibilities of legs between the chickens and the pigs.

See

http://groups.google.com/group/comp.lang.c++/browse_thread/thread/18fc24f405aaf432

for the original thread.

Once i get this additional step added in, I'll post the code if I have
any further trouble.

Any additional help would be appreciated! I'm still pretty new to C++
and our instructor is very vague on the math part of the program. He
even suggests looking around on the internet or in books for help in
this area.

osmium

unread,
Oct 11, 2006, 2:52:53 PM10/11/06
to
"Petrakid" writes:

I think the magic word you might want is "partitions". For a given n, you
want to find all the possible partitions.I suspect you already know that the
solution involves Diophantine equations.


Robert Mabee

unread,
Oct 11, 2006, 5:05:03 PM10/11/06
to
Petrakid wrote:
> Alright, so I asked earlier about this pig/chicken leg count thing
...

> and our instructor is very vague on the math part of the program.

So perhaps you have the freedom to ignore the math, and use the
overkill power of the computer to try all plausible numbers,
reporting only those for which the computed results correspond
to real-world possibilities. For example, the number of chickens
is surely between 0 and the number of legs, so iterate between
those limits computing the resulting number of pigs, and silently
ignore all cases of negative or fractional pigs.

If the number of legs is not pinned down, then it seems you are
asked, not to provide a solution, but to print a table that could
be used to get the solution (a row of possible chicken/pig counts)
by looking up the leg count.

Petrakid

unread,
Oct 11, 2006, 7:44:39 PM10/11/06
to
Here's the program:

#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;

total_poss = Calculate_Possibilities(min_legs, max_legs);


cout << "There are a total of "<<total_poss<<" ways to"<<endl;
cout << "group this many legs."<<endl;
cout << "Would you like to see the possibilities? ";
cin >> answer;
if ((answer == 'y') || (answer == 'Y'))
{
Print_Possibilities(min_legs, max_legs);
cout <<setw(COL_WIDTH)<<"Cowboys";
cout <<setw(COL_WIDTH)<<"Horses"<<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(possibilities);
}

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)<<legs / 4<<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;
}

It isn't outputting the proper information. I know the legs / 4 in the
Print_Possibilities function is screwy, but even so, the number of
possibilities is always a minimum of 16900 or so, even when there's
only 1-10 legs!! What am i doing wrong?

osmium

unread,
Oct 11, 2006, 8:55:15 PM10/11/06
to
"Petrakid" writes:

Garbage + garbage = garbage
<snip>


Robert Mabee

unread,
Oct 11, 2006, 11:36:48 PM10/11/06
to
Petrakid wrote:
> for (index = min_cowboys; index < legs; index++)
> {
> if ((index * legs_cowboys * min_cowboys) < legs)

I understand multiplying a count of cowboys (where did the chickens
go?) by legs per cowboy, but not multiplying that by the minimum
number of cowboys. One good way to spot this kind of error is
dimensional analysis: cowboys * legs/cowboy = legs; your expression
yields legs squared, which should raise a red flag because it doesn't
correspond to anything we know about the real world or the problem
space, and then comparing that to a count of legs is a clear error.

> if ((legs - index * legs_cowboys * min_cowboys) %
> (min_horses * legs_horses) == 0)

As above. Perhaps you can see how to make two separate comparisons,
one to verify the remaining legs make an integral number of horses,
and the other to check the number of those horses.

> It isn't outputting the proper information. I know the legs / 4 in the
> Print_Possibilities function is screwy, but even so, the number of
> possibilities is always a minimum of 16900 or so, even when there's
> only 1-10 legs!! What am i doing wrong?

Sounds like a good excuse to learn to use a debugger, or if none,
debugging print statements, to verify that variables contain the
values that you are sure they do, and that the flow of control goes
the way you expect.

0 new messages