kitten population calculation error

Need help with this code ::: the answer i am getting at the end does not match the expected answer ....
At the start there are 1 male and 2 females kitten 2 month old.
After every 4 months each female give birth to 6 kittens , 2
males and 4 females.Females at the age of 6 months start giving birth.Each kitten dies after 2 years both male & female

::: at the end of year one there should be 58 females and 29 males but my code is giving me 50 females and 9 males...can anyone please guide me in the right direction or tell me what am i doing wrong.:::

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
int main ()
{
  float sum_male=1,sum_female=2;
  float y,next_male,next_female,n;
  cout<<" Enter the number of years"<<endl;
  cin>>n;

   y= (n*12+2)/4;

   for ( int i=1;i<=y;i++)
    {
       next_male=(sum_male*2);
       next_female=(sum_female*4);
       sum_male=sum_male+next_male;
       sum_female=sum_female+next_female;
     }

cout<<" Total male after years "<<sum_male<<endl;
cout<<" Total female after years "<<sum_female<<endl;

return 0;
}

Last edited on
Well, I am not sure why you aren't doing this with classes. I don't think a simple loop like that will help you with doing all the calculations (such as females only giving birth after 6 months old).

next_male=(sum_male*2);

^ This line is definitely wrong. The number of males born are dependent on the number of females, not males (well, as long as there is at least 1 male).
thnks for your reply ....because i have an inadequate substitute as a prof...he spent 3 hours staring at this in class..said nobdoy told him we were going to do this n thn declared it a home task -__-
so the whole logic is wrong ?.... maybe we have to do it with fibonacci rabbit sequence...but i am not sure how the month will be used in this sequence .....or where to multiply the females (maybe nex*4 ?)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# include <iostream>
using namespace std;
int main ()
{
 int fact=1,num,a=0,b=1,next=0,i,sum=0;
float month;

cout<< " enter the value : "<<endl;
cin>>num;
cout<<" Enter the number of years"<<endl;
 cin>>n;

month= (n*12)/4;

cout<<a<<endl<<b;

for (i=1;i<=num-2;i++ )
 {    next= a+b;
	cout<<next<<endl;
	a=b;
	b=next;
 }
return 0;
}
Last edited on
Are you familiar with creating your own structs and classes? Or using STL containers such as vectors?

EDIT: Just thought of how you could do it with arrays:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
unsigned long long numMalesAge[24] = { 0 }; //age in months
unsigned long long numFemalesAge[24] = { 0 }; //age in months

numMalesAge[2] = 1;
numFemalesAge[2] = 2;

for (/* every month */)
{
	for (/* ages 23 down to 1 */)
	{
		numMalesAge[age] = numMalesAge[age - 1];
		numFemalesAge[age] = numFemalesAge[age - 1];
	}
	numMalesAge[0] = 0; //male babies
	numFemalesAge[0] = 0; //female babies
	if (/* there are any males */)
	{
		numMalesAge[0] += 2 * /* number of females age 6, 10, 14, etc */;
		numFemalesAge[0] = 2 * /* number of males age 0 */;
	}
}
Last edited on
hey thnx for the reply .... no i am not familiar with structs or classes or vector....i am just a beginner
i tried following the direction but the code keeps giving me garbage value =S ...where am i messing it up

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
using namespace std;
int main ()
{
int i,o,age,w;
float q,n;
int numMalesAge[24] = { 0 }; //age in months
int numFemalesAge[24] = { 0 }; //age in months

cout<<" Enter the number of years"<<endl;
  cin>>n;

q=n*12;

numMalesAge[2] = 1;
numFemalesAge[2] = 2;

for (i=0;i<q;i++)
{
	for (age=24;age>0;age--)
	{
		numMalesAge[age] = numMalesAge[age - 1];
		numFemalesAge[age] = numFemalesAge[age - 1];
	}
	numMalesAge[0] = 0; //male babies
	numFemalesAge[0] = 0; //female babies
	if (numMalesAge[age] > 0)
	{	
		for (w=24;w>=6;w-4)
		{
		numMalesAge[0] = numMalesAge + (2 * w); }
		
		numFemalesAge[0] = numFemalesAge[0] +2 *numMalesAge[0] ;
	
	}
}
cout<<" Female "<<numFemalesAge<<endl;
cout<<" malee "<<numMalesAge<<endl;
return 0;
}
You made some mistakes trying to follow what I gave you.

for (/* ages 23 down to 1 */)
for (age=24;age>0;age--)

^ These do not match. You're counting ages 24 down to 1.

if (/* there are any males */)
if (numMalesAge[age] > 0)

^ These do not match. But I guess you don't even need this if statement anyway, since there will always be males.

numMalesAge[0] += 2 * /* number of females age 6, 10, 14, etc */;
1
2
3
for (w=24;w>=6;w-4)
		{
		numMalesAge[0] = numMalesAge + (2 * w); }

^ These do not match at all.

1
2
cout<<" Female "<<numFemalesAge<<endl;
cout<<" malee "<<numMalesAge<<endl;

^ That is not how you add up the totals.
Topic archived. No new replies allowed.