Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Please help! ! ! You are to write a program that generates and manipulates r

ID: 3765250 • Letter: C

Question

C++ Please help! ! ! You are to write a program that generates and manipulates rabbit objects in certain ways and output the results after each loop. The loop will run 20 times or until all rabbits die off, or 5000 rabbits are born, whichever comes first. You will start off with 5 rabbits. So at the beginning of the code you should set the values in the first 5 rabbit objects. The constructor for each Rabbit object should create the following class variables: Age: Set to a random number from 1 to 10 Gender: Set to either M or F randomly with 50/50 odds what gender is assigned Color: White, brown, black, or spotted (randomly chosen) After the initial rabbits are all all created, then the program will go into a loop. The loop will simulate a one year cycle. During that year the rabbits will grow older by a year and may give birth to more rabbits. If a rabbit is older than 10, it is killed off. At the end of each loop (year) you will print out the current rabbit stock. Color, gender, age. To store the rabbits you *could* create an array of rabbit objects, but you have no idea how many rabbits you'll end up with. So it's best to use either vectors (which are like arrays but can grow) or a linked list of pointers (an advanced topic which you can tackle if you want, but it might drive you nuts). For a vector, each time you get a new rabbit object, you would use the push_back method. For example, if your rabbit object was called Rabbits you could declar a vector of Rabbits as vector rabbits; ... then you would use the "rabbits.push_back(Rabbit());" to add a rabbit to the vector. You could then use rabbits.size() to see how many rabbit objects (alive or dead) you have created To determine birth rates, you have to check to see how many males and females you have. If you don't have any males, no births that year. If you have females and they are at least two years old, each female will produce one new rabbit that you will construct. To keep track of how many males and females you have you can use a global variable and as you construct each rabbit and determine its age, add one to the appropriate counter. Then each time through the loop just check the counter value. For females you will just add one to the female counter when they reach age of 2. For death, you can just assume any rabbit > 10 has gone to meet its maker and just ignore that rabbit when it comes time to print the rabbit list for the year. Not the best programming practice. Best practice would be to delete the object and mark the array as empty at that element, or to maintain the list of rabbits in a linked list (of pointers) and delete the dead rabbit out of the list. Feel free to add that into your code if you want to impress the teacher! :) So as is, the age will determine the status of the rabbit. If it is zero, it's not yet born. If it is > 10 it's dead. So it's important in the constructor for each rabbit object to set the age to zero.

Sample program output

Year: 1 Rabbits: 5 brown F 1 black F 6 black M 8 spotted M 1 white M 7

Year: 2 Rabbits: 7 white M 5 spotted F 9 brown F 2 black F 7 black M 9 spotted M 2 white M 8

Year: 3 Rabbits: 8 black M 10 black F 7 brown F 7 white M 6 brown F 3 black F 8 spotted M 3 white M 9

Explanation / Answer

Answer:

/**** C++ PROGRAM FOR RABBITS****/

#include<iostream>

#include<vector>

#include<string>

using namespace std;

string clr[]={"white","brown","black","spotted"};

int femaleCounter=0;

int maleCounter=0;

//CLASS RABBIT

class Rabbit

{

private:

int Age;

char Gender;

string color;

public:

//CONSTRUCTOR

Rabbit()

{

Age=rand()%10;

Gender=((rand()%2)==1)?'M':'F';

color=clr[rand()%4];

}

//INCREASE THE RABBIT AGE BY 1

void increaseAge()

{

Age++;

}

//GET RABBIT GENDER

char getRabbitGender()

{

return Gender;

}

//RETURN THE AGE OF THE RABBIT

int getRabbitAge()

{

return Age;

}

//PRINT THE RABBIT INFORMATION

void printRabbitInfo()

{

cout<<" "<<color<<" "<<Gender<<" "<<Age;

}

};

//MAIN METHOD

int main()

{

srand(time(NULL));

//VECTOR TO HOLD RABBITS

vector<Rabbit> rabbits;

//FIRST STORE 5 RABBIT OBJECTS

for(int k=0;k<5;k++)

{

rabbits.push_back(Rabbit());

}

//LOOP THROUGH 1 TO20OR UNTIL 5000 RABBITS

for(int k1=1;k1<=20;k1++)

{//CHECK 5000 RABBITS BORN OR ALL ARE DIE

if(rabbits.size()==5000|| rabbits.size()==0)

break;

cout<<" Year:"<< k1<<endl;

//PRINT THE RABBIT INFO

for(int k2=0;k2<rabbits.size();k2++)

{

//DELETE THE RABBITS WITH AGE>10

if(rabbits.at(k2).getRabbitAge()>10)

     rabbits.erase(rabbits.begin()+k2);

rabbits.at(k2).printRabbitInfo();

}

//INCREASE THE RABBIT AGE

for(int k2=0;k2<rabbits.size();k2++)

rabbits.at(k2).increaseAge();

maleCounter=0;

femaleCounter=0;

//GET THE FEMALE AND MALE RABBITS COUNTER

for(int k2=0;k2<rabbits.size();k2++)

{

if(rabbits.at(k2).getRabbitGender()=='M')

maleCounter++;

if(rabbits.at(k2).getRabbitGender()=='F'&&rabbits.at(k2).getRabbitAge()>=2)

femaleCounter++;

}

//LOOP TO GENERATE NEW RABBITS

int countNewRabbit=0;

//to produce new rabbit

if(maleCounter>0)

{

for(int k2=0;k2<rabbits.size();k2++)

{

//CHECK IF RABBIT IS FEMALE WITH AGE>=2

if(rabbits.at(k2).getRabbitGender()=='F'&&rabbits.at(k2).getRabbitAge()>=2)

{

countNewRabbit++;

}

}

//ADD NEW RABBITS TO THE VECTOR

for(int k1=0;k1<countNewRabbit;k1++)

rabbits.push_back(Rabbit());

}

}

return 0;

}//END MAIN

Output:

This is a part of the output:

Year:1

brown F 4 white F 3 spotted M 9 black F 7 white F 7

Year:2

brown F 5 white F 4 spotted M 10 black F 8 white F 8 brown F 9 white F 1 black M 2 spotted F 5

Year:3

brown F 6 white F 5 black F 9 white F 9 brown F 10 white F 2 black M 3 spotted F 6 white M 0 black F 2 white M 3 spotted F 6 black M 1 white M 9 spotted F 3

Year:4

brown F 7 white F 6 black F 10 white F 10 white F 3 black M 4 spotted F 7 white M 1 black F 3 white M 4 spotted F 7 black M 2 white M 10 spotted F 4 spotted M 3 spotted M 5 spotted M 0 white M 9 black M 5 spotted M 1 white M 0 spotted F 1 white M 5 brown M 4

Year:5

brown F 8 white F 7 white F 11 white F 4 black M 5 spotted F 8 white M 2 black F 4 white M 5 spotted F 8 black M 3 spotted F 5 spotted M 4 spotted M 6 spotted M 1 white M 10 black M 6 spotted M 2 white M 1 spotted F 2 white M 6 brown M 5 white F 9 white M 2 brown M 9 black M 8 black F 9 brown F 9 white F 3 black M 9 brown M 8 black M 7

Year:6

brown F 9 white F 8 white F 5 black M 6 spotted F 9 white M 3 black F 5 white M 6 spotted F 9 black M 4 spotted F 6 spotted M 5 spotted M 7 spotted M 2 black M 7 spotted M 3 white M 2 spotted F 3 white M 7 brown M 6 white F 10 white M 3 brown M 10 black M 9 black F 10 brown F 10 white F 4 black M 10 brown M 9 black M 8 black F 6 black F 6 spotted F 2 black F 9 spotted M 8 spotted M 2 spotted F 7 black M 9 spotted M 9 black F 0 black F 7 spotted M 1 spotted M 3

Year:7

brown F 10 white F 9 white F 6 black M 7 spotted F 10 white M 4 black F 6 white M 7 spotted F 10 black M 5 spotted F 7 spotted M 6 spotted M 8 spotted M 3 black M 8 spotted M 4 white M 3 spotted F 4 white M 8 brown M 7 white M 4 black M 10 brown F 11 white F 5 brown M 10 black M 9 black F 7 black F 7 spotted F 3 black F 10 spotted M 9 spotted M 3 spotted F 8 black M 10 spotted M 10 black F 1 black F 8 spotted M 2 spotted M 4 brown M 1 spotted M 9 black M 2 black F 5 spotted M 6 white F 1 spotted F 4 spotted M 3 black M 5 white F 6 brown M 5 white M 1 spotted M 6 spotted M 8 white M 2 white F 9 white M 5 white F 2

Year:8

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote