C documentation V1 Help

Program: simple Weather Program

Problem Statement

In this challenge, you are tasked with creating a C program that utilizes a two-dimensional array to analyze weather data. The program should find the total rainfall for each year, the average yearly rainfall, and the average rainfall for each month. Input will be provided as a 2D array with hard-coded values for rainfall amounts over the past 5 years. The array should have 5 rows and 12 columns, where each column represents a month, and each row represents a year. Rainfall amounts can be floating-point numbers.

Example Output

RAINFALL (inches) 32.4 37.9 49.8 44.0 32.9 The yearly average is 39.4 inches. MONTHLY AVERAGES: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec 7.3 7.3 4.9 3.0 2.3 0.6 1.2 0.3 0.5 1.7 3.6 6.7 YEAR 2010 2011 2012 2013 2014

Feel free to attempt the challenge. When you're ready for guidance or if you have questions, let me know!

Algorithm

  1. Initialize a 2D array to store rainfall data for 5 years and 12 months.

  2. Calculate the total rainfall for each year.

  3. Calculate the average yearly rainfall.

  4. Calculate the average rainfall for each month.

  5. Display the total rainfall for each year.

  6. Display the average yearly rainfall.

  7. Display the average rainfall for each month.

Pseudocode

// Initialize a 2D array with hard-coded rainfall data float rainfallData[5][12] = { { ... }, { ... }, { ... }, { ... }, { ... } }; // Calculate total rainfall for each year for (int year = 0; year < 5; year++) { float totalRainfallYear = 0.0; for (int month = 0; month < 12; month++) { totalRainfallYear += rainfallData[year][month]; } Display totalRainfallYear; } // Calculate the average yearly rainfall float totalRainfallAllYears = 0.0; for (int year = 0; year < 5; year++) { for (int month = 0; month < 12; month++) { totalRainfallAllYears += rainfallData[year][month]; } } float averageYearlyRainfall = totalRainfallAllYears / 5; Display averageYearlyRainfall; // Calculate the average rainfall for each month float monthlyAverages[12]; for (int month = 0; month < 12; month++) { float totalRainfallMonth = 0.0; for (int year = 0; year < 5; year++) { totalRainfallMonth += rainfallData[year][month]; } monthlyAverages[month] = totalRainfallMonth / 5; } // Display the monthly averages Display monthlyAverages;

Program

#include <stdio.h> int main() { // Initialize a 2D array with hard-coded rainfall data float rainfallData[5][12] = { {7.3, 5.6, 4.9, 3.0, 2.3, 0.6, 1.2, 0.3, 0.5, 1.7, 3.6, 6.7}, // Add data for the remaining years // ... }; // Calculate total rainfall for each year printf("RAINFALL (inches)\n"); for (int year = 0; year < 5; year++) { float totalRainfallYear = 0.0; for (int month = 0; month < 12; month++) { totalRainfallYear += rainfallData[year][month]; } printf("%.1f\n", totalRainfallYear); } // Calculate the average yearly rainfall float totalRainfallAllYears = 0.0; for (int year = 0; year < 5; year++) { for (int month = 0; month < 12; month++) { totalRainfallAllYears += rainfallData[year][month]; } } float averageYearlyRainfall = totalRainfallAllYears / 5; printf("\nThe yearly average is %.1f inches.\n\n", averageYearlyRainfall); // Calculate the average rainfall for each month float monthlyAverages[12]; for (int month = 0; month < 12; month++) { float totalRainfallMonth = 0.0; for (int year = 0; year < 5; year++) { totalRainfallMonth += rainfallData[year][month]; } monthlyAverages[month] = totalRainfallMonth / 5; } // Display the monthly averages printf("MONTHLY AVERAGES:\n\n"); printf("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec\n"); for (int month = 0; month < 12; month++) { printf("%.1f ", monthlyAverages[month]); } return 0; }

Explanation

This program initializes a 2D array with rainfall data for five years and twelve months. It then calculates the total rainfall for each year, the average yearly rainfall, and the average rainfall for each month. The results are displayed in a formatted manner.

Feel free to run the program and observe the output. If you have any questions or if there's anything you'd like to discuss, feel free to ask!

Last modified: 25 February 2024