Starting from:
$30

$24

LAB NINE SOLUTION

This lab is all about data processing. You will be reading information from a file that contains thousands of individual pieces of data. There is far too much for a person to deal with in its numeric form, but you will display it in a way that is very easy to understand.




On the class web site, associated with this lab, there are 76 data files, each containing a whole year of weather observations from a different location in the U.S. Choose one of them and down-load it to the computer you are using.




The files all have one line of data for each day of a particular year, and each line consists of exactly nine numbers. But beware: this is real data from real meteorological stations. Sometimes their equipment isn’t in perfect working order, so some days might not appear.

As an example of the data, here are three lines taken from near the end of the file for Mount Washington, NH.




2003 12 16 0 18 32 39 1 77

2003 12 17 25 30 34 39 -1 55

2003 12 18 8 17 32 51 21 82




The first three numbers on each line give the year, month, and date of the observations. The remaining numbers are:

The Minimum Temperature recorded on that day (Fahrenheit)
The Average Temperature recorded during that day (Fahrenheit)
The Maximum Temperature recorded on that day (Fahrenheit)
The Depth of new Snow fall (tenths of an inch)
Total amount of Precipitation, incl. snow melted (tenths of an inch)
The Maximum Wind speed observed (miles per hour)



If you were wondering, their rain meter was broken on 17th December. That’s what -1 indicates in the last three items.




1. Make sure you can read the file




Write a simple program that reads the whole file, and gives you just enough information to verify that it is working properly. Perhaps you could just print out the average temperature for every day, and check that it does generally get warmer at the beginning of the year and colder at the end.




As a reminder, here is a little snippet of code that would open a file, read three numbers from it, then close it again.




ifstream fin("file.name");




int a, b, c;

fin a b c;

if (fin.fail())

cout << “Not enough data in the file!\n”; else




cout << "the average of the numbers was" << (a+b+c)/3 << "\n"; fin.close();

2. Turn the numbers into a graph




Adapt your program so that it displays all the average temperatures as points on a graph. Pick a reasonably large window and pen size so that it will look OK. The vertical position is of course just the average temperature multiplied by some suitable scaling factor, but what about the horizontal position?





























































Make it easy on yourself, just for now. If you pretend that every month is 31 days long, then month*31+day gives a simple basis for the x-coordinate, which you can scale or shift as desired.




The picture above was produced from the Miami FL file. If you want to set the caption of the window like I did, there is a handy little function called set_caption. It takes a string parameter.










3. Get the X coordinate right.




There aren’t really 31 days in every month, and pretending that there are can cause annoying gaps. Fortunately, you recently had an assignment that involved working out what day of the year a particular date is. Make it so that the horizontal position of each dot is correct: the distance between 28th February and 1st March is the same as the distance between 1st March and 2nd March, or any other two consecutive days.







4. Less dottiness




In the summer, the temperature tends not to change so much from day to day, so the dots almost merge into a solid line. In winter, as you can see, it is harder to see what is going on. There are dots all over the place. Make it into a line graph instead.




5. More information




When someone is planning a trip and deciding what to pack, knowing the minimum and maximum temperature is much more useful than just the average. Improve your program so that it shows a red line graph for the daily maximum temperature, and a blue line graph for the daily minimum. On the same axes of course.





























































This is one of those situations where the restrained use of a few variables can be really useful.




6. Make it useful




It’s a nice looking graph, but not really very useful. It’s hard to tell just where the last week of October is, and nobody has any idea what the actual temperatures are: just how low was that dip in January?




Add some clearly labelled axes: vertical lines showing where each month stats, and horizontal lines for each ten degrees or so. If you draw them in gray, it won’t seem too obtrusive.

This is where good clean programming pays off. If you were keeping things reasonably tidy, it will be easy to work out where the lines go, and to shift the graph to make room for the labels.




7. Flexibility




There is more information than just temperatures in those files. Allow the user to select which pieces of information they want to see graphs for, and what colours those graphs should be in. You may have to think about the scaling a little. You won’t see much if wind speed is plotted to the same scale as maximum temperature. Except Up North perhaps.










8. Last thing before you go




Make sure you have written a good robust program. Download a file for a different city, and check that your program displays its data correctly. It is important to make sure that your program works correctly even for data files that have a lot of missing data. One of the data files is for a made-up town called Hopeless, Missouri (at least, I don’t think there’s a real town with that name, but I didn’t check). It was deliberately made with a lot of missing data to help you with testing.










9. Extra Credit




Adaptive scaling would be nice too. All my samples have assumed that temperatures will fit nicely into a 0 to 100 scale. That is untrue for a great many places. Your program could scan the file first to find out what the true range is, then re-read the data and plot it at a custom-made scale.

More products