// Arup Guha
// 9/28/2011
// Written in COP 3223H - processing weather data

#include <stdio.h>

const int NOVALUE = -99;

int main() {

    // Open weather file.
    FILE* ifp = fopen("SDRIYADH.txt", "r");

    // Initialize all necessary variables.
    double sum = 0;
    int num_days = 0;

    double min = 1000, max = -100;

    int min_mon, min_day, min_year;

    double year_sum = 0;
    int days_year = 0;

    int coldest_year;
    double cold_temp = 1000;

    // Go through each day.
    while (1) {

        // Read in this day's data.
        int mon, day, year;
        double day_temp;
        fscanf(ifp, "%d%d%d%lf", &mon, &day, &year, &day_temp);

        // End of data.
        if (mon == -1)
            break;

        // Only add in valid days.
        if (day_temp != NOVALUE) {
            sum = sum + day_temp;
            year_sum = year_sum + day_temp;
            num_days++;
            days_year++;

            // Update min and max, if necessary.
            if (day_temp < min) {
                min = day_temp;
                min_mon = mon;
                min_day = day;
                min_year = year;
            }
            if (day_temp > max)
                max = day_temp;

        }

        // Process the end of the year.
        if (mon == 12 && day == 31) {

            double avg = year_sum/days_year;
            printf("Year %d: Avg %lf\n", year, avg);

            // Update the coldest year.
            if (avg < cold_temp) {
                cold_temp = avg;
                coldest_year = year;
            }

            year_sum = 0;
            days_year = 0;

        }
    }

    // All the information we computed.
    printf("The avg temp is %lf\n", sum/num_days);
    printf("The min temp is %lf\n", min);
    printf("The min was on %d/%d/%d\n", min_mon, min_day, min_year);
    printf("The coldest year was %d.\n", coldest_year);
    printf("The max temp is %lf\n", max);

    return 0;
}
