93 lines
3.7 KiB
C
Executable File
93 lines
3.7 KiB
C
Executable File
/*
|
|
* In this task, you should write a program that records daily temperature data for a week and perform a basic analysis. The program should do the following:
|
|
*
|
|
Store the temperatures of a full* week (i.e., 7 days) in an array
|
|
Let the user define a temperature threshold for further analysis
|
|
Calculate the average temperature of the week
|
|
Print the number of days with a temperature above the threshold
|
|
|
|
Functions are already declared for these functionalities in the given code, which still need to be implemented:
|
|
|
|
readTemperatures(float temperatures[]): For each day of the week, this function should print Temperature for day n: (where n represents the number of the day) and the user can enter the temperature for the respective day. The input for each day should be stored in an array that is passed when called in the main() function.
|
|
readTemperatureThreshold(): The user can enter a temperature threshold after printing Temperature threshold: . The value should be returned to the main() function.
|
|
calculateAverageTemperature(float temperatures[]): In this function, the average temperature of all values in the array is calculated and returned.
|
|
countDaysWithTemperatureAboveThreshold(float temperatures[], float threshold): This function should return the number of days on which the recorded temperature is above (i.e., greater than) the user-defined threshold.
|
|
|
|
|
|
These functions are called in the main() function. Both the average temperature (with two decimal places) and the threshold (with two decimal places) along the days with temperature above the threshold are printed in the main() (see the example below).
|
|
|
|
This is an example:
|
|
|
|
Temperature for day 1: 24.7
|
|
Temperature for day 2: 22.8
|
|
Temperature for day 3: 28.3
|
|
Temperature for day 4: 28.9
|
|
Temperature for day 5: 30
|
|
Temperature for day 6: 24.8
|
|
Temperature for day 7: 23.2
|
|
Temperature threshold: 25.5
|
|
Average temperature: 26.10
|
|
Days with temperature above 25.50: 3
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#define DAYS_PER_WEEK 7
|
|
|
|
void readTemperatures(float temperatures[]);
|
|
float readTemperatureThreshold();
|
|
float calculateAverageTemperature(float temperatures[]);
|
|
int countDaysWithTemperatureAboveThreshold(float temperatures[], float threshold);
|
|
|
|
int main()
|
|
{
|
|
float temperatures[DAYS_PER_WEEK] = {0};
|
|
float temperature_threshold = 0.0;
|
|
float average_temperature = 0.0;
|
|
int days_above_threshold = 0;
|
|
|
|
// Add your code here
|
|
readTemperatures(temperatures);
|
|
temperature_threshold = readTemperatureThreshold();
|
|
average_temperature = calculateAverageTemperature(temperatures);
|
|
printf("Average temperature: %.2f\n", average_temperature);
|
|
days_above_threshold = countDaysWithTemperatureAboveThreshold(temperatures, temperature_threshold);
|
|
printf("Days with temperature above %.2f: %d", temperature_threshold, days_above_threshold);
|
|
return 0;
|
|
}
|
|
|
|
// Add your code here
|
|
void readTemperatures(float temperatures[])
|
|
{
|
|
for(int day = 0 ; day < DAYS_PER_WEEK ; day++)
|
|
{
|
|
printf("Temperature for day %d: ", (day + 1));
|
|
scanf("%f", &temperatures[day]);
|
|
}
|
|
}
|
|
float readTemperatureThreshold()
|
|
{
|
|
float threshold = 0;
|
|
printf("Temperature threshold: ");
|
|
scanf("%f", &threshold);
|
|
return threshold;
|
|
}
|
|
float calculateAverageTemperature(float temperatures[])
|
|
{
|
|
float average_temperature = 0;
|
|
for(int day = 0 ; day < DAYS_PER_WEEK ; day++)
|
|
{
|
|
average_temperature += temperatures[day];
|
|
}
|
|
return (average_temperature / DAYS_PER_WEEK);
|
|
}
|
|
int countDaysWithTemperatureAboveThreshold(float temperatures[], float threshold)
|
|
{
|
|
int days_above_threshold = 0;
|
|
for(int day = 0 ; day < DAYS_PER_WEEK ; day++)
|
|
{
|
|
if (temperatures[day] > threshold){days_above_threshold++;}
|
|
}
|
|
return days_above_threshold;
|
|
}
|