Write a program that display different message based on a random generated tempe
ID: 3747648 • Letter: W
Question
Write a program that display different message based on a random generated temperature input.
The loop should run for 100 times unless it hit the number -128. If -40 <= temperature <= 30 ºF, display the temperature and message “Cold”; if 30 < temperature <= 60 ºF, display the temperature and message “Cool”; if 60 < temperature <= 78 ºF, display the temperature and message “Nice”; if 78 < temperature <= 90 ºF, display the temperature and message “Warm”; if 90 < temperature <= 110 ºF, display the temperature and message “Hot”; if the temperature is exactly -128, display: “extreme temperature -128 Fahrenheit. Exit.” and stop the loop; for all temperatures, display: “Unbearable extreme temperature!”
Please use the function rand() provided in Standard C to generate the random temperature, as shown below: signed char temperature; temperature = (signed char) rand();
Explanation / Answer
// C program to generate random numbers
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
int main()
{
float miles;
signed char temperature;
srand(time(0));
for(int i = 0; i<100; i++) {
//printf(" %d ", (signed char)rand());
temperature = (signed char) rand();
if (-40 <= temperature && temperature <= 30 ){
printf("Temparature is %d Farenheit. It is Cold ", temperature);
}
else if (30 < temperature && temperature <= 60 ){
printf("Temparature is %d Farenheit. It is Cool ", temperature);
}
else if (60 < temperature && temperature <= 78 ){
printf("Temparature is %d Farenheit. It is Nice ", temperature);
}
else if ( 79 < temperature && temperature <= 90 ){
printf("Temparature is %d Farenheit. It is Warm ", temperature);
}
else if ( 90 < temperature && temperature <= 110 ){
printf("Temparature is %d Farenheit. It is Hot ", temperature);
}
else if (temperature == -128 ){
printf("Temparature is %d Farenheit. It is Extreme temperature ", temperature);
break;
}
else{
printf("Temparature is %d Farenheit. It is Unbearable extreme temperature ", temperature);
}
}
return 0;
}
--output---------------
Temparature is -81 Farenheit. It is Unbearable extreme temperature
Temparature is 100 Farenheit. It is Hot
Temparature is -117 Farenheit. It is Unbearable extreme temperature
Temparature is 77 Farenheit. It is Nice
Temparature is -107 Farenheit. It is Unbearable extreme temperature
Temparature is -47 Farenheit. It is Unbearable extreme temperature
Temparature is 118 Farenheit. It is Unbearable extreme temperature
Temparature is -1 Farenheit. It is Cold
Temparature is 105 Farenheit. It is Hot
Temparature is -56 Farenheit. It is Unbearable extreme temperature
Temparature is -41 Farenheit. It is Unbearable extreme temperature
Temparature is -18 Farenheit. It is Cold
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.