c语言模拟一天温度的变化
时间: 2024-04-08 20:33:16 浏览: 12
Sure! Here's an example of how you can simulate the temperature variations throughout a day using C language:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_TEMPERATURE 40
#define MIN_TEMPERATURE 10
#define MAX_HOURS 24
int main() {
srand(time(NULL)); // Seed the random number generator
int temperature[MAX_HOURS];
int hour;
// Generate random temperatures for each hour of the day
for (hour = 0; hour < MAX_HOURS; hour++) {
temperature[hour] = (rand() % (MAX_TEMPERATURE - MIN_TEMPERATURE + 1)) + MIN_TEMPERATURE;
}
// Print the temperature for each hour
for (hour = 0; hour < MAX_HOURS; hour++) {
printf("Hour %d: %d°C\n", hour, temperature[hour]);
}
return 0;
}
```
In this code, we use the `rand()` function to generate random temperatures within the range of 10 to 40 degrees Celsius. The `srand()` function is used to seed the random number generator with the current time so that we get different random values each time we run the program.
We store the temperatures for each hour in an array called `temperature`, and then we print the temperature for each hour using a loop.
Please note that this is a simple simulation and does not take into account any real-world factors that may affect temperature variations throughout a day.