In: Computer Science
How to find the day of the highest and lowest temperatures? And how to find an average of high and low temperatures? C language
You can use a loop to find the maximum, minimum & average temperatures respectively as shown below:
Assuming that the temperature readings are stored in an array of chronological order, we can loop through all of the elements of that array and keep updating the min & max temperatures that we have found till the current iteration. The index of these elements will represents the day.
The following code shows all these operations:
Sample Code:
#include <stdio.h>
int main()
{
float tempratures[5] = {25, 10, 14, 35, 45}; //Initialize random
temprature values in daily chronological order
float max_t, min_t, avg_t;
int min_day, max_day;
max_t = tempratures[0]; //Initialize max & min tempratures to
first day's temprature
min_t = tempratures[0];
min_day = 0;
max_day = 0;
for (int i = 0; i < 5; i++)
{
if (tempratures[i] < min_t) {
min_t = tempratures[i];
min_day = i+1; //Update day to i+1 to represent the day in
order
} if (tempratures[i] > max_t)
{
max_t = tempratures[i];
max_day = i+1;
}
}
printf("Day of highest temperature: %d\n", max_day);
printf("Day of lowest temperature : %d\n", min_day);
avg_t = (max_t + min_t) / 2;
printf("Average temperature = %.2f\n", avg_t);
}
OUTPUT:
(*Note: Please up-vote. If any doubt, please let me know in the comments)