Description
This Programming Assignment is similar to P6.35 in your text and you must refer to that problem for the general
idea of what this problem does. The Python representation of the matrix must be a list of lists. It differs from
P6.35 in the following ways:
• It is not restricted to 10×10 matrices. It should work for any size from 5×5 and larger.
• The matrix will be read from a file (see chapter 7) named terrain.txt. This file will have a row of integers
representing elevations on each line. The integers will be separated by spaces. For ease of formatting the
output you can assume that all elevations are less than 1000.
• A second file named flood.txt will contain one integer per line. These represent flood levels and your
program should produce a map (same size as read from terrain.txt) to show the flooded terrain for each
integer in the file.
• Your solution must include at least the following functions:
o readTerrain() – open and read terrain.txt, close the file, return the matrix (a list of lists)
o printTerrain(elevMatrix) – print a map of terrain elevations (from readTerrain() )
o calcFlood(elevMatrix, h2oElevation) – return a matrix of strings (each position a “*” or a ” ” for
“flooded” and “not flooded” respectively) Any location whose elevation is less than or equal to the
h2oElevation is considered flooded.
o printFlood(floodMatrix) – cleanly print the matrix as returned by calcFlood
• Sometimes input presented to a program contains bad data. Any elevation value that is not an integer
should be printed as “-“ with printing the flood map. This program will be easy to implement if you leave
the data as strings in elevMatrix and use try-except (see chapter 8) when converting to integers for
comparing to the flood level.
• Do NOT use list comprehensions or the .format string method.
A sample output is shown below. Build your own files to test your code.
100 104 107 103 109 106 112 115
102 101 105 100 106 xxx yyy zzz
103 99 102 96 101 105 110 122
97 94 98 100 104 100 109 113
94 93 95 98 100 103 108 110
xx yy zz 101 104 108 110 115
99 101 104 107 110 110 115 125
Flooding at elevation = 95
– – –
*
* * *
– – –
Flooding at elevation = 100
*
* – – –
* *
* * * * *
* * * * *
– – –
*
Flooding at elevation = 105
* * *
* * * * – – –
* * * * * *
* * * * * *
* * * * * *
– – – * *
* * *
Table to left printed by printTerrain(matrix)
Tables to left printed by printFlood using data
Provided by calcFlood
Submit your program to the designated D2L drop box. Your program should be named PA4.py. For programming
assignments, early submissions gain a bonus percent: 1 day early – 10%; 3 or more days early – 20%.