In: Computer Science
In C++ language implement an end of the world simulation. Create 3 global variables earthquake = 30%, hurricane = 35%, volcano = 35%
Implement a class named World. Class World inherited Class CRandom
In class World create three methods Earthquakes, Hurricanes and Volcanoes. These methods return death toll in numbers and percentage up to their global percentage. This percentage is calculated using CRandom.
#include <sys/time.h>
#include <bits/stdc++.h>
#include <time.h>
using namespace std;
//========================================================================//
//========================================================================//
class CRandom{
public:
CRandom(){} // constructor
~CRandom(){} // destructor
int getRandomPublic(int rangeLow, int rangeHigh){
int myRand_scaled;
myRand_scaled=getRandomPrivate(rangeLow, rangeHigh);
return myRand_scaled;
}
private:// uniform distribution between rangeLow and rangeHigh
int getRandomPrivate(int rangeLow, int rangeHigh) {
double myRand = rand()/(1.0 + RAND_MAX);
int range = rangeHigh - rangeLow + 1;
int myRand_scaled = (myRand * range) + rangeLow;
return myRand_scaled;
}
protected:// uniform distribution between rangeLow and rangeHigh
int getRandomProtected(int rangeLow, int rangeHigh) {
double myRand = rand()/(1.0 + RAND_MAX);
int range = rangeHigh - rangeLow + 1;
int myRand_scaled = (myRand * range) + rangeLow;
return myRand_scaled;
}
};
//========================================================================//
//========================================================================//
class World: public CRandom {
public:
// constructor to initialize random functions
World() {
struct timeval time;
gettimeofday(&time, NULL);
srand((unsigned int) time.tv_usec);
}
long long TotalSurvivors() {
// population killed by tsunami
long long earthQuakeKills = EarthQuakes();
long long tsunamiKills = Tsunamis();
long long volcanoKills = Volcanoes();
long long iceAgeKills = IceAge();
long long meteoritesKills = Meteorites();
long long fireKills = Fires();
long long hungerKills = Hunger();
long long zombiesKills = Zombies();
long long hurricaneKills = Hurricanes();
long long totalSurvivors = CURRENT_POPULATION
- (earthQuakeKills
+ tsunamiKills
+ volcanoKills
+ iceAgeKills
+ meteoritesKills
+ fireKills
+ hungerKills
+ zombiesKills
+ hurricaneKills);
return max(0LL, totalSurvivors);
}
void NewWorld() {
long long totalSurvivors = TotalSurvivors();
if (totalSurvivors > 0) {
cout << "Happy Ending! Total number of survivor is " << totalSurvivors << endl;
} else {
cout << "No Happy Ending! There are not survivors" << endl;
}
}
private:
long long CURRENT_POPULATION = 7 * 1LL * 1000000000;
// sum of all adds up to 100 that's why for Tsunami it is taken as 20
long long EarthQuakes() {
int randomKillingPercentage = getRandomProtected(0, 10);
return (CURRENT_POPULATION * randomKillingPercentage) / 100;
}
long long Tsunamis() {
int randomKillingPercentage = getRandomProtected(0, 20);
return (CURRENT_POPULATION * randomKillingPercentage) / 100;
}
long long Volcanoes() {
int randomKillingPercentage = getRandomProtected(0, 10);
return (CURRENT_POPULATION * randomKillingPercentage) / 100;
}
long long IceAge() {
int randomKillingPercentage = getRandomProtected(0, 10);
return (CURRENT_POPULATION * randomKillingPercentage) / 100;
}
long long Meteorites() {
int randomKillingPercentage = getRandomProtected(0, 10);
return (CURRENT_POPULATION * randomKillingPercentage) / 100;
}
long long Fires() {
int randomKillingPercentage = getRandomProtected(0, 10);
return (CURRENT_POPULATION * randomKillingPercentage) / 100;
}
long long Hunger() {
int randomKillingPercentage = getRandomProtected(0, 10);
return (CURRENT_POPULATION * randomKillingPercentage) / 100;
}
long long Zombies() {
int randomKillingPercentage = getRandomProtected(0, 10);
return (CURRENT_POPULATION * randomKillingPercentage) / 100;
}
long long Hurricanes() {
int randomKillingPercentage = getRandomProtected(0, 10);
return (CURRENT_POPULATION * randomKillingPercentage) / 100;
}
};
int main(void){
World world;
world.NewWorld();
return EXIT_SUCCESS;
}