You are on page 1of 3

Nama : Muhammad Nabil Asrofi

NRP : 4122600044
Kelas : 2 D4 Mekatronika B

Assignment: Weather Analysis Tool


Background:
Weather plays a crucial role in our daily lives. From deciding what to wear to planning outdoor
activities, we often rely on weather forecasts. In this assignment, you will develop a simple
weather analysis tool that allows a user to input daily temperature readings for a week and then
analyze the data to provide useful insights.
Objec0ve:
Develop a C program that takes daily temperature readings for a week, calculates the average
temperature, identifies the hottest and coldest days, and determines if there was a heatwave
(defined as three or more consecu8ve days with temperatures above 32°C).
Instructions:
1. Data Input:
• Prompt the user to enter daily temperature readings for seven consecutive days.
Store these readings in an array.
• Ensure that the temperature readings are between -45°C and 65°C. If a user
enters a value outside this range, prompt them to enter a valid temperature.
2. Data Analysis:
• Calculate the average temperature for the week.
• Iden8fy the hottest and coldest days (i.e., the days with the highest and lowest
temperatures).
• Determine if there was a heat wave during the week. A heat wave is defined as
three or more consecu8ve days with temperatures above 32°C.
3. Output:
• Display the average temperature for the week.
• Display the hottest and coldest days along with their respec8ve temperatures.
• If there was a heatwave, display a message indica8ng that. Otherwise, display a
message that states that there was no heat wave.
Constraints:
• Use only variables, loops (for or while), and if-else condi8ons.
• Do not use any external libraries or func8ons other than the standard input/output
func8ons.
Tips:
• Think about how you can use loops to both collect and analyze data.
• Consider how you can determine if there is a heat wave without manually checking
every possible three-day combina8on.
Submission:
Submit your C code along with a brief report explaining your approach to the problem, any
challenges you faced, and how you overcame them. Your report should also include test cases
that demonstrate the functionality of your program.
Evaluation Criteria:
Your program will be evaluated based on the following:
• Correctness of the code.
• Efficiency of your algorithms.
• Clarity and organization of your code.
• The comprehensiveness of your test cases.
• Quality of your report.
Good luck and remember that the goal is not just to write code that works, but to develop a
solution that is efficient, clear, and demonstrates a deep understanding of the core concepts of
C programming.

Code :
#include <stdio.h>

int main() {

// Initialize variables and arrays

float temperatures[7];

float averageTemp, hottestTemp, coldestTemp;

int hottestDay, coldestDay;

int consecutiveHotDays = 0;

// Data Input

printf("Enter daily temperature readings for a week:\n");

for (int i = 0; i < 7; i++) {

do {

printf("Day %d: ", i + 1);

scanf("%f", &temperatures[i]);

if (temperatures[i] < -45 || temperatures[i] > 65) {

printf("Invalid temperature. Please enter a valid temperature between -45°C and 65°C.\n");

} while (temperatures[i] < -45 || temperatures[i] > 65);

// Initialize hottestTemp and coldestTemp with the temperature of the first day

hottestTemp = coldestTemp = temperatures[0];

hottestDay = coldestDay = 1;

// Data Analysis

for (int i = 1; i < 7; i++) {

if (temperatures[i] > hottestTemp) {

hottestTemp = temperatures[i];

hottestDay = i + 1;

if (temperatures[i] < coldestTemp) {


coldestTemp = temperatures[i];

coldestDay = i + 1;

// Check for a heatwave

if (temperatures[i] > 32) {

consecutiveHotDays++;

if (consecutiveHotDays >= 3) {

printf("Heatwave detected!\n");

break; // Exit the loop if a heatwave is found

} else {

consecutiveHotDays = 0;

// Calculate the average temperature

averageTemp = 0;

for (int i = 0; i < 7; i++) {

averageTemp += temperatures[i];

averageTemp /= 7;

// Output

printf("Average temperature for the week: %.2f°C\n", averageTemp);

printf("Hottest day: Day %d, Temperature: %.2f°C\n", hottestDay, hottestTemp);

printf("Coldest day: Day %d, Temperature: %.2f°C\n", coldestDay, coldestTemp);

if (consecutiveHotDays < 3) {

printf("No heatwave detected.\n");

return 0;

You might also like