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 41 42 43 44 45 46 47 48 49 |
#include <iostream> #include <cstdlib> #include <ctime> // Constants const double INITIAL_TEMPERATURE = 20.0; // Initial temperature in degrees Celsius const double TEMPERATURE_VARIATION = 5.0; // Max variation in temperature per day const double INITIAL_PRECIPITATION = 0.0; // Initial precipitation in mm const double PRECIPITATION_CHANCE = 0.3; // Chance of precipitation per day const double MAX_PRECIPITATION = 20.0; // Max precipitation amount per day const int NUM_DAYS = 30; // Number of days to simulate // Function to generate a random double between min and max double randomDouble(double min, double max) { return min + static_cast<double>(rand()) / (static_cast<double>(RAND_MAX / (max - min))); } // Function to simulate weather patterns void simulateWeather(double initialTemperature, double temperatureVariation, double initialPrecipitation, double precipitationChance, double maxPrecipitation, int numDays) { double temperature = initialTemperature; double precipitation = initialPrecipitation; std::cout << "Day Temperature (°C) Precipitation (mm)\n"; for (int day = 1; day <= numDays; ++day) { // Random temperature variation double tempVariation = randomDouble(-temperatureVariation, temperatureVariation); temperature += tempVariation; // Random precipitation event double dailyPrecipitation = (randomDouble(0.0, 1.0) < precipitationChance) ? randomDouble(0.0, maxPrecipitation) : 0.0; precipitation = dailyPrecipitation; // Output the weather for the day std::cout << day << " " << temperature << " " << precipitation << "\n"; } } int main() { // Seed the random number generator srand(static_cast<unsigned>(time(0))); // Simulate weather for a given number of days simulateWeather(INITIAL_TEMPERATURE, TEMPERATURE_VARIATION, INITIAL_PRECIPITATION, PRECIPITATION_CHANCE, MAX_PRECIPITATION, NUM_DAYS); return 0; } |
Explanation
- Constants:
INITIAL_TEMPERATURE
: Starting temperature in degrees Celsius.TEMPERATURE_VARIATION
: Maximum daily variation in temperature.INITIAL_PRECIPITATION
: Starting precipitation amount in millimeters.PRECIPITATION_CHANCE
: Probability of precipitation occurring on any given day.MAX_PRECIPITATION
: Maximum amount of precipitation that can occur in a day.NUM_DAYS
: Number of days to simulate.
- randomDouble Function:
- Generates a random double between
min
andmax
using therand()
function. - This function is used to add variability to temperature and precipitation values.
- Generates a random double between
- simulateWeather Function:
- Simulates daily weather patterns for a specified number of days.
- Temperature Update: Each day, the temperature is updated with a random variation.
- Precipitation Update: Each day has a chance of precipitation, with the amount determined if precipitation occurs.
- Outputs the daily temperature and precipitation values.
- Main Function:
- Seeds the random number generator with the current time to ensure different results on each run.
- Calls
simulateWeather
to run the simulation and output the weather patterns for the given number of days.
Usage
- Initial Conditions: The simulation starts with a temperature of 20°C and no precipitation.
- Temperature Variation: The temperature changes randomly within a specified range each day.
- Precipitation Chance: There is a 30% chance each day of some precipitation occurring, with the amount varying up to 20 mm.