UCL WIKI

Child pages
• Session 4 - Input and output
Go to start of banner

Session 4 - Input and output

Contents:

Game of life load and save

Go to `code/4_io/exercises/gol_with_io.cpp` here you will find the game of life with load and save stub code. OR use your own game of life from yesterday.

Build the program. You'll see it asks for an input file. If you type `rand` it will just generate a random pattern and run for `MAX_TICS` iteration and then exit.

You should do the following:

1. Add save functionality by following the instructions in the `saveWorld` function.
2. Once you have it working, set the `MAX_TICS` (near the top of the file) to something lower and run the program. After `MAX_TICS` it will save the file. Open it in an editor and see if the world is there.
3. Now complete the `loadWorld` function by following the instructions.
4. Run the program and try inputting one of your saved worlds to see if it worked.

Now go to this website:

http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life#Examples_of_patterns

and find some good patterns. Code them into your world by hand in a text editor and load them. See if they work as they should. Gosper glider gun is especially nice to try.

Numerical analysis from file

heathrow.dat

`wget --no-check-certificate https://wiki.ucl.ac.uk/download/attachments/26516266/heathrow.dat`

Go to `code/4_io/exercises/heathrow.dat`. Here you will find meteorological data measurements at Heathrow since 1984. There are seven columns that mean the following:

Year (integer)

Month number (integer)

T.max (float)

T.min (float)

Air frost (integer)

Rain (float)

Sun hours (float)

Missing data is represented by the string "---".

Write a program that reads in all the data and find:

1. The overall min and max temperatures (look up std::min/max) and the midpoint between these two.
2. The frostiest December.
3. The rainiest month of the year on average.

You should get the following answers:
Temperatures - min = -4.6, max = 28.2, mean = 11.8
Frostiest December = 12 / 1962, air frost = 22
Raniest month = October

• No labels