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

Write program in C++ using dynamic memory allocation Note: Be sure to deallocate

ID: 3597926 • Letter: W

Question

Write program in C++ using dynamic memory allocation

Note: Be sure to deallocate correctly.

Please DO NOT use "malloc" for memory allocation of dynamic arrays -- use "new"

Requirements

Animal Class

The Animal class has the following member variables:

Age

Adult if age >= 3 days

Baby if age < 3 days

Cost

Tiger cost $10,000

Penguin cost $1,000

Turtle cost $100

Number of Babies

Tigers have 1 baby

Penguins have 5 babies

Turtles have 10 babies

Base Food Cost

You can get this base food cost from the user or set it as a constant. Example base food cost per animal per day: $10.

Tigers have a feeding cost of 5 times the base cost

Penguins have a feeding cost that is the same as the base cost

Turtles have a feeding cost that is 50% the base cost

Payoff

A tiger’s payoff per day is 20% of their cost per animal. (not counting bonus)

A penguin’s payoff per day is 10% of their cost per animal

A turtle’s payoff per day is 5% of their cost per animal

Note: please do not modify the variables names or add more member variables to this class.

Game Flow:

The player begins with a specific amount of money in the bank, e.g. 100,000 dollars. At the start, the user needs to buy three types of animals (tigers, penguins, turtles) to start the business. Each type should have a quantity of either 1 or 2. For each animal bought, the cost is subtracted from the bank. All newly bought animals are 1 day old.

Each turn is a “day”. At the beginning of the day, all animals increase age by 1 day, and the user needs to pay the feeding cost of each animal. Feeding is required so the animals don’t die. After the feeding cost is subtracted from the bank, one randomized event takes place during the day. You can determine how to implement the random functions by yourself. The random function will pick one random event from the following list:

Random Events:

A sickness occurs to an animal in the zoo:

Pick an animal at random that will die

Remove one animal of that type from the exhibit. (dynamic array in the zoo)

A boom in zoo attendance occurs:

Generate a random bonus for the day, 250-500 dollars for each tiger in the zoo

Add the bonus payoff for each tiger to the total payoff of the day as a reward

A baby animal is born:

Pick an animal at random to have a baby

Check if there is an animal old enough to be a parent (age >= 3), add babies to the zoo depending on the “number of babies” specific to the type of animal. If no animal is old enough of the randomly selected type, pick another type of animal. Baby animals start at age 0. For simplicity, you don’t need to consider the gender of the adult animals in order to have babies. One adult animal is good enough to have babies.

Note: If no animals are able to give birth to baby animals in the zoo, your program needs to be able to recognize this and recover.

4. Nothing happens

After the random event, calculate the profit for the day based on the number of each animals and their payoff. If there is a bonus for the day, add it to the profit as well. Before the day ends, ask the player if they would like to buy an adult animal. If they do, ask for the type of animal they would like, then add the animal to the zoo and subtract that cost from the bank. The adult animal that is bought will be 3 days old.

After the end of a day (the end of the game play loop), prompt user whether to keep playing or end the game. If the user has no money, print a message to tell the user the game is over, and end the game.

Class requirements

The following classes are required: zoo, animal, tiger, penguin, and turtle. The program also must use inheritance; the tiger, penguin, and turtle class must inherit from animal class.

Also, the zoo class should have a dynamic array for each type of animal. Each dynamic array should have a capacity of 10 animals to start with. If more animals are added, you should resize the dynamic array by doubling the starting capacity to hold more animals.

Explanation / Answer

Program:

#include<iostream>
#include<algorithm>
#include<stdlib.h>
using namespace std;
class animal
{ public:
   animal *type;
   int typenumber;
   int age;
   int cost;
   int babies;
   int basefoodcost=10;
   int foodcost=0;
   int payoff;



  
};

class tiger: public animal
{
   public:
       tiger()
       {   age=0;
           cost=10000;
           babies=1;
           //foodcost=50;
           //payoff=(0.2)*cost;
           //typenumber=1;
       }
      
       void setage(int n)
       {
           this->age=n;
       }
};
class penguin: public animal
{
   public:
       penguin()
       {   age=0;
           cost=1000;
           babies=5;
           //foodcost=10;
           //payoff=(0.1)*cost;
           //typenumber=2;
       }
      
       void setage(int n)
       {
           this->age=n;
       }
};
class turtles: public animal
{
   public:
       turtles()
       {   age=0;
           cost=100;
           babies=10;
           //foodcost=5;
           //payoff=(0.05)*cost;
           //typenumber=3;
       }
      
       void setage(int n)
       {
           this->age=n;
       }
};
class zoo
{   public:
   int amount=100000;
   int tanimal=0;
   int limit;
   animal *animals;
  
   int getamount()
   {
       return this->amount;
   }
  
   void allocate(int n)
   {
   animals=new animal[n];
   limit=n;
   }
};
int main()
{
   cout<<"welcome to zoo!!! ";
  
   zoo z;int ch;int q;tiger *t;penguin *t1;turtles *t2;int amt=0,v=1;int i=0;
   z.allocate(10);
   cout<<"Total initial amount in bank==>$"<<z.getamount()<<" ";
   cout<<"please buy animals of 3 types (quantity 1 or 2) ";
   do
   {
   cout<<"1.Tiger 2.penguin 3.Turtle Input your choice:";
   cin>>ch;
   switch(ch)
   {
       case 1: cout<<"enter the number of tigers you want ";
               amt++;
               cin>>q;
               cout<<"you bought "<<q<<" tiger(s) for ur zoo ";
              t=new tiger[q];
              z.tanimal=z.tanimal+q;
            
              for(;i<z.tanimal;i++)
              {
             cout<<"i==>"<<i<<" ";
              z.animals[i].type=&t[i];
              z.animals[i].type->age++;
              z.animals[i].type->payoff=2000;
              z.animals[i].type->typenumber=1;
              cout<<"age =="<<t[i].age;
              cout<<"foodcost=="<<t[i].foodcost;
              }
              z.amount=z.amount-q*t[0].cost;
              cout<<"total amount remaining in bank==>$"<<z.getamount();
              if(q==1)
              delete t;
              else
              delete [] t;
              break;
       case 2: cout<<"enter the number of penguins you want ";
               amt++;
               cin>>q;
               cout<<"you bought "<<q<<" penguin(s) for ur zoo ";
              t1=new penguin[q];
              z.tanimal=z.tanimal+q;
              for(;i<z.tanimal;i++)
              {
                    
              cout<<"i==>"<<i<<" ";
              z.animals[i].type=&t1[i];
              z.animals[i].type->age++;
              z.animals[i].type->foodcost=10;
             z.animals[i].type->payoff=100;
             z.animals[i].type->typenumber=2;
              cout<<"foodcost=="<<t1[i].foodcost;
              }
            
              z.amount=z.amount-q*t1[0].cost;
              cout<<"total amount remaining in bank==>$"<<z.getamount();
              if(q==1)
              delete t1;
              else
              delete[] t1;
              break;
       case 3: cout<<"enter the number of turtles you want ";
               cin>>q;
               amt++;
               cout<<"you bought "<<q<<" turtle(s) for ur zoo ";
              t2=new turtles[q];
                 z.tanimal=z.tanimal+q;
              for(;i<z.tanimal;i++)
              {
                 cout<<"i==>"<<i<<" ";
              t2[i].age++;
              z.animals[i].type=&t2[i];
              z.animals[i].type->foodcost=5;
              z.animals[i].type->payoff=5;
              z.animals[i].type->typenumber=3;
              cout<<"foodcost=="<<z.animals[i].type->foodcost;
          }
              z.amount=z.amount-q*t2[0].cost;
              cout<<"total amount remaining in bank==>$"<<z.getamount();
              if(q==1)
              delete t2;
              else
              delete[] t2;
              break;         
   }
   if(amt<3)
   cout<<"please buy 3 types of animal ";
   else
   {
   cout<<"want to buy more animals press 0/1???";
   cin>>v;
    }
  
}while(v==1&&amt<3);

cout<<"total animals in zoo==>"<<z.tanimal<<" ";

int input=1,input1=0,zz=0;
do
{
   cout<<"Start of a new day ";
   for(i=0;i<z.tanimal;i++)
   {
   z.animals[i].type->age++;
   cout<<"foodcost=="<<z.animals[i].type->foodcost;
   z.amount=z.amount-z.animals[i].type->foodcost;
    }
    cout<<"Total amount left==>"<<z.getamount();
    int x=rand()%4+1;
    switch(x)
    {
       case 1:zz=rand()%z.tanimal;
              delete (z.animals[zz].type);
              delete(&z.animals[zz]);
              break;
       case 2:int bonus=(250+rand()%250);
               for(i=0;i<z.tanimal;i++)
               {
               if(z.animals[i].typenumber==1)
               {
                  z.animals[i].type->payoff=z.animals[i].type->payoff+bonus;
               }
               }
                break;
       //case 3:to be written;
      
                    
   }
   for(i=0;i<z.tanimal;i++)
   {
       z.amount=z.amount+z.animals[i].type->payoff;
   }
   cout<<"if you want to buy an adult animal press 0/1??";
   cin>>input1;
   if(input1==1)
   {
   cout<<"1.Tiger 2.penguin 3.Turtle Input your choice:";
   cin>>ch;
   tiger *temp;
   penguin *temp1;
   turtles *temp2;
   if(z.tanimal<z.limit)
   {   z.tanimal++;
       switch(ch)
       {
           case 1:temp=new tiger();
                   z.animals[z.tanimal-1].type=temp;
                   z.animals[z.tanimal-1].type->age=3;
                   break;
           case 2:temp1=new penguin();
                   z.animals[z.tanimal-1].type=temp1;
                   z.animals[z.tanimal-1].type->age=3;
                   break;
           case 3:temp2=new turtles();
                   z.animals[z.tanimal-1].type=temp2;
                   z.animals[z.tanimal-1].type->age=3;
                   break;            
                 
       }
   }
   else if(z.tanimal==z.limit)
   {
       animal *tem=new animal[z.limit];
       tem=z.animals;
       delete [] z.animals;
       z.allocate(z.limit*(z.tanimal/10+1));
       for(int l=0;l<z.tanimal;l++)
       {
           z.animals[i]=tem[i];
       }
   }
   cout<<"total animals ==>"<<z.tanimal;
  
     }
else
{   if(z.amount<=0)
    {
   cout<<"game is over ";input=0;
    }
    else
    {
   cout<<"do you want to play more ... press 0/1?";
   cin>>input;
    }
}
  
}while(input);

}

n

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