C
  • Introduction
    • Fundamentals of a Program
    • Overview of C
    • Features of C
  • Installing Required Software
    • Setting Up VSCode for Windows
    • Setting Up VSCode for macOS
    • Setting Up VSCode for Ubuntu
  • Starting to write code
    • Compiling and Running Your Code
    • Creating Our First C Program
    • Errors and Warnings
    • Program: Writing a C Program to Display Your Name
    • Structure of a C Program
  • Basic Concepts
    • Comments in C
    • Preprocessor in C
    • The #include Statement
    • Displaying Output
    • Reading Input from the Terminal
    • Enums and Chars
    • Data Types and Variables
    • Format Specifiers
    • Command Line Arguments
    • Program: Calculating the Area of a Triangle
  • Operators
    • Converting Minutes to Years and Days
    • Basic Operators
    • Bitwise Operators
    • Program: Byte Sizes of Basic Data Types
    • cast and sizeof Operators
    • Operator Precedence
  • Control Flow
    • If-Else Statements
    • Program: Weekly Pay Calculation
    • Switch Statement
    • For Loop
    • While and Do-While Loops
    • Nested Loops and Loop Control
    • Program: Guess the Number
  • Arrays
    • Introduction to Arrays
    • Program: Prime Number Generator
    • Multidimensional Arrays
    • Program: simple Weather Program
    • Variable Length Arrays (VLAs)
  • Functions
    • Overview of Functions
    • Defining Functions
    • Arguments and Parameters
    • Returning Data from Functions
    • Variable Scoping
    • Program: Tic Tac Toe Game
    • Recursion
  • Strings
    • Defining a String
    • Constant Strings in C
    • Common String Functions
    • Program: Bubble Sort
    • Searching, Tokenizing, and Analyzing Strings
    • Converting Strings
  • Debugging
    • What is Debugging
    • Understanding the Call Stack
    • Common C Mistakes
    • Understanding Compiler Errors
  • Pointer
    • Defining Pointers
    • Accessing Pointers
    • Program: Pointer Demonstration
    • Pointers and Const
    • Void Pointers
    • String Pointers
    • Array Pointers
    • Utilizing Pointers with Functions
    • Pointer Arithmetic
  • Dynamic Memory Allocation
    • malloc, calloc, and realloc
    • Program: User Input String
    • Memory Deallocation
  • Structure
    • Structures and Arrays
    • Nested Structures
    • Structures and Pointers
    • Structures and Functions
    • Program: Structure pointers and Functions
  • File Input and Output
    • Accessing Files
    • Reading from a File
    • Program: Finding the Total Number of Lines in a Text File
    • Writing to a Text File
    • Finding Your Position in a File
    • Program: Converting Characters in a File to Uppercase
    • Program: Printing the Contents of a File in Reverse Order
  • The Standard C Library
    • Various Functions in C
    • Math Functions in C
    • Utility Functions in C
Powered by GitBook
On this page
  • Problem Statement
  • Example Output
  • Algorithm
  • Pseudocode
  • Program
  • Explanation

Was this helpful?

  1. Arrays

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!

PreviousMultidimensional ArraysNextVariable Length Arrays (VLAs)

Was this helpful?