// Arup Guha
// 10/3/2011
// Written in COP 3223H - weather program that uses arrays.

#include <stdio.h>

/** Note: I don't know why I can't put
          const int PERIOD = STARTYEAR - CURYEAR + 1
**/
const int STARTYEAR = 1995;
const int CURYEAR = 2011;
const int PERIOD = 2011 - 1995 + 1;
const int MAXLENGTH = 20;
const int NOVALUE = -99;

int main() {

    double averages[PERIOD];
    int numdays[PERIOD];

    // Initialize both of our arrays.
    int i;
    for (i = 0; i<PERIOD; i++) {
        averages[i] = 0;
        numdays[i] = 0;
    }

    // Gets the file name and opens the file.
    char filename[20];
    printf("Enter the name of the file to open.\n");
    scanf("%s", filename);
    FILE* ifp = fopen(filename, "r");

    // Go through each entry.
    while (1) {

        // Read this day's data.
        int month, day, year;
        double temp;
        fscanf(ifp, "%d%d%d%lf", &month, &day, &year, &temp);

        // End of data.
        if (month == -1)
            break;

        // Got a valid value.
        if (temp > NOVALUE) {
            averages[year - STARTYEAR] += temp;
            numdays[year - STARTYEAR]++;
        }
    }

    // Compute averages from sum and number of days
    for (i=0; i<PERIOD; i++)
        averages[i] = averages[i] / numdays[i];

    // Print out each year's average.
    for (i=0; i<PERIOD; i++) {
        printf("%d\t%.2lf\n", i + STARTYEAR, averages[i]);
    }

    // Find biggest increase in temperature.
    double max_diff = 0;
    int year_diff = -1;

    // Go through each gap. Note that this loop runs PERIOD-1 times.
    for (i=0; i<PERIOD-1; i++) {

        double cur_diff = averages[i+1] - averages[i];

        // Update biggest jump.
        if (cur_diff > max_diff) {
            max_diff = cur_diff;
            year_diff = i + STARTYEAR;
        }
    }

    // Print out the results.
    printf("The biggest jump was %.2lf degrees from %d to %d.\n", max_diff, year_diff, year_diff+1);

    fclose(ifp);
    return 0;
}

