Detailed Examination of a CSV File

The brutal winter continues in my area, with no end in site! I saved weather data collected for 31 days from December to January in a CSV file. This file was used in last month’s Exercise. For this month’s Exercise, your job is to read the same data file, but also to report on the results.

As a review, a CSV file contains a text database. Each record appears on a line by itself, with fields in each record separated by commas. Sometimes the first line in the CSV file is a header, but in my weather data example, it’s not.

The file weather_data.csv lists 31 records showing the year, month, day, and the high and low temperatures recorded for that day. The year, month, and day values are integers; high and low are real numbers (floating point).

For last month’s Exercise, you had to read the data and display it in a table, which didn’t require any storage. For this month’s Exercise, however, your code must read the data from the file and store it. Then the code must manipulate the data and generate a report. (If you can figure out how to generate the report without storing the data, more power to you!)

The report must output an analysis of the weather data with these specifics:

Date range from month day, year to month day, year:
The average high was x.x
The average low was x.x
The highest high was x.x on month day, year
The lowest high was x.x on month day, year
The highest low was x.x on month day, year
The lowest low was x.x on month day, year

The code shouldn’t display a table of all the data, just output the highpoints as shown above: averages, highest high/low, and lowest high/low, plus the dates for the extremes.

A few suggestions:

  • The number of records is fixed at 31. If you choose to use one or more arrays to store data, then you can set their size to 31. In the real world, you might not be so lucky as to know the number of records in a data file. Coding a solution for that problem will most likely be the topic of some future Exercise.
  • You can use multiple arrays to store the data, but the better solution is some type of structure.
  • No, you don’t have to use pointers unless you’re really keen to use pointers.
  • Your solution will involve many lines code, not because the Exercise is difficult but because of the data manipulation. Keep in mind that a C program with hundreds of lines of code isn’t necessarily a complex program. In fact, the most obfuscated C programs have only a few lines of code.

Click here to view my solution, though I encourage you to first attempt to code this Exercise on your own.

Leave a Reply