Write a C-program that does the following: 1. Reads 10 temperatures from the con
ID: 3603053 • Letter: W
Question
Write a C-program that does the following:
1. Reads 10 temperatures from the console (standard input). Each value represents the high temp for a given day in a 10-day period.
2. Stores the temperatures in an array. You must use the same array for all the temperatures.
3. Finds and displays the highest recorded temperature.
4. Finds and displays the day when the highest temperature was recorded. If two or more days tie for the highest temperature, the program will report all the days with the highest temperature. Hint: To save time, create a text file with test data and use input redirection when testing your code.
Explanation / Answer
The C program is given bellow. The explaination is given in the comment within the program.
#include<stdio.h>
#define PERIOD 10
/*
* getMaxTemperature() returns the maximum temperature in the array of temperatures.
*/
float getMaxTemperature(float temperature[], int size)
{
int i;
double maxTemp=temperature[0]; //store the first temperature in maxTemp
for(i=1;i<size;i++){
if(maxTemp < temperature[i]){
maxTemp = temperature[i]; //if we find a new maximum terperature then update maxTemp
}
}
return maxTemp;
}
/*
* main() function.
*
*/
int main(){
int i;
float temperature[PERIOD],maxTemp;
/* read 10 temperatures from the console and store them in temperature array */
for(i=0;i<PERIOD;i++){
printf("Enter highest temperation (in Celsius)) of day %d : ",i+1);
scanf("%f",&temperature[i]);
}
/* Finds and displays the highest recorded temperature */
maxTemp = getMaxTemperature(temperature, PERIOD);
printf(" Highest recorded temperature is %f(in Celsius) ",maxTemp);
/* Finds and displays the day when the highest temperature was recorded */
printf(" Days of highest recorded temperature is/are ");
for(i=0;i<PERIOD;i++){ //Scan all the temperatures to find a match with maxTemp
if(temperature[i] == maxTemp)
printf("Day %d ",i+1);
}
}
NB: Hope you find the desired program. Let me know if anything else is needed from my side. Please provide your feedback to help you better.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.