Saving for a Rainy Day
ItÕs been raining for a week around here so weÕre going to find some information about the recent rainfall from rainfall measurement data stored in a file. The file contains measurements of rainfall in different cities on different days. The measurements are in inches and fractions (ex. 2.3Ó) and measurements were recorded at the end of an hour of rain collection. The measurement devices were emptied after the measurement was recorded. In each city, at least three measurements were taken and recorded and no more than six measurements were recorded. Each line of the file will have the form of
monthNum dayNum cityName rainfall1 rainfall2 rainfall3 É rainfall6
There will be multiple lines in the file. Every line will have this format. There will be different numbers of rainfall measurements on different lines. The city names will always be one word names. (If the name has two words, they should be connected with an underscore, ex. Grand_Prairie.) The lines of data in the file are ordered by date from earliest to latest and then within a single date, the lines are ordered by city name. There are different numbers of cities on different days, there are an unknown number of days, and there are an unknown number of lines in the file. There are no more than seven days worth of data in the file.
We want our program to use the file data to find two daily values for each city and one value for an entire day. Using the data we want to find the daily total rainfall for a city and we want to know the average amount of rainfall per hour based on the given measurements. Once we have read all the data for all the cities on one day, then we want to find an average rainfall amount per day across all the cities recorded. We also want to print these totals and averages for each city and each day. For a line of input like:
10 17 Arlington 0.0 0.7 0.3
The output should look like:
On 10/17 in Arlington the total rainfall was 1.0in with an average of 0.3in per hour
There will be one line of output for each line of input plus one extra line of input for each day, ex:
The average rainfall across the area on 10/17 was 7.18in
----------------------------------------------------------------------
Extra step: Print a list of all the daily rainfall amounts in inches along with the total and average for the city.
Extension: Find the total for the week by using the sum of the daily average rainfall and find the daily average for the whole week.
How to Pseudocode
Break the problem into tasks – donÕt worry about the order of the tasks yet
Keep the tasks at the level they are in the problem right now
Use the words of the problem as much as possible here
Figure out what values need to be recorded and/ or used in calculations – these will be the values that you need to make variables for. Some of your variables may be data structures.
If there are parts of the problem that you do not understand,
ASK THE PERSON WHO ASSIGNED THE PROBLEM!
Just ÒguessingÓ is not a very effective strategy.
Now try to organize your tasks so that you do these things:
[ Some people find pictorial tools like flow charts to be helpful in deciding how to combine the various tasks within their programs. Feel free to use a flow chart to help you organize.]
Declare the variables.
Give initial values to variables that are defined in the problem, if any
Get input information needed, if any
If the input is coming from a specific place, like user input, note that here
List the calculations/processing tasks here
Determine what must be done first to complete calculations/processes
Order the steps to perform the task once
Print data if needed here
Determine if loops are needed to perform the task more than once
Print data if needed here
If some steps need more definition, such as how to choose a highest or lowest
Go back and add details for those steps
If there more tasks to do, keep following the basic approach above.
You may need to break your program into chunks where each chunk does one or more tasks.
You may also consider taking a chunk or a task and turn it into a separate method (function) and call that method from another part of your program
Review all your steps with the original problem to make sure you included
every task and
every output
that is required in the original problem.
If you have met all the problem requirements, your pseudocode is done. Otherwise, go back and see where new tasks should be added.
Rules of thumb
Methods (functions) should perform ONE primary task at one level of abstraction.
Some times you are asked to give a high-level design. A high-level design may be more like one to two sentences to describe an entire method. A high-level design is NOT the same as pseudocode. The high-level design is more abstract than pseudocode.
A high-level design is generally composed of a group of methods (functions) that are used together to solve a problem. There is always a main method. The main method generally calls other methods to perform its detailed tasks. The main method generally has all the shared data declared in it.
When more detailed work is needed, generally that would be put into its own method that performs one more detailed task and that detailed method is called by some ÒhigherÓ or more abstract method
Each line of pseudocode should generally turn into no more than 2 – 3 lines of actual code.
Rainfall initial pass at pseudocode
Tasks from problem description:
Input in a file containing measurements of rainfall in different cities on different days.
The measurements are in inches and fractions (ex. 2.3Ó) and
measurements were recorded at the end of an hour of rain collection.
The measurement devices were emptied after the measurement was recorded.
In each city, at least three measurements and no more than six measurements were recorded. rainfall1 rainfall2 rainfall3 É rainfall6
Each line of the file will have the form of
monthNum dayNum cityName rainfall1 rainfall2 rainfall3 É rainfall6
10 17 Arlington 0.0 0.7 0.3
There will be multiple lines in the file.
Every line will have this format.
There will be different numbers of rainfall measurements on different lines.
The city names cityName will always be one word names. (If the name has two words, they should be connected with an underscore, ex. Grand_Prairie.)
The lines of data in the file are ordered by date from earliest to latest monthNum dayNum
and then within a single date, the lines are ordered by city name. cityName
There are different numbers of cities on different days,
there are an unknown number of days, and
there are an unknown number of lines in the file.
There are no more than seven days worth of data in the file.
find the daily total rainfall for a city
know the average amount of rainfall per hour for a city based on the given measurements.
find an average rainfall amount per day across all the cities recorded.
print these totals and averages for each city and each day.
The output format:
On 10/17 in Arlington the total rainfall was 1.0in with an average of 0.3in per hour
print these totals and averages for each city and each day.
The output format:
The average rainfall across the area on 10/17 was 7.18in
There will be one line of output for each line of input plus one extra line of input for each day, ex:
Rainfall second pass at pseudocode
Tasks from problem description:
Declare int monthNum dayNum //rainfall in different cities on different days.
Declare String cityName //rainfall in different cities
Declare double rain1 rain2 rain3 rain4 rain5 rain6 // inches and fractions (ex. 2.3Ó)
Declare int prevMonth = 0, prevDay = 0
Declare double dailyTotalCity = 0 // find the daily total rainfall for a city
Declare double avgPerHourCity = 0 // know the average amount of rainfall per hour for a city
Declare double avgPerDay = 0 // find an average rainfall amount per day across all the cities
Declare int lineCount = 0 // There will be multiple lines in the file.
Declare int cityCount = 0 // different numbers of cities on different days,
Declare int dayCount = 1 // there are an unknown number of days, and
Declare int msmtCount = 0 // number of rainfall measurements in file
Declare Scanner and File // Input in a file containing
Declare inString // to read one line of file
Declare Scanner // to read from string
Print hello message
Loop while lines in file // there are an unknown number of lines in the file. ; There will be multiple lines in the file.
and dayCount < 8 // There are no more than seven days worth of data in the file.
Increment lineCount
Scan from File to get one line as inString
// The lines of data in the file are ordered by date from earliest to latest monthNum dayNum
// and then within a single date, the lines are ordered by city name. cityName
// This info is not used for this program, except to rely on sorted dates in file data for date count
// Will have to use try-catch here with keyboard input to end and dayCount set to 10
Scan from inString to get elements
//Each line of the file will have the form of ; Every line will have this format.
//monthNum dayNum cityName rainfall1 rainfall2 rainfall3 É rainfall6 //10 17 Arlington 0.0 0.7 0.3
Read int for monthNum // month as number
Read int for dayNum // day as number
Read .next for cityName and increment cityCount // one word names
// There are different numbers of cities on different days,
Read double for rain1, rain2, rain3
dailyTotalCity gets sum of rain1, rain2, rain3
msmtCount gets set to 3
// There will be different numbers of rainfall measurements on different lines. ; at least three and no more than six
If string has a double, read rain4, add it to dailyTotalCity, and increment msmtCount
Then If string has a double, read rain5, add it to dailyTotalCity, and msmtCount++
Then If string has a double, read rain6, add it to total, and msmtCount++
avgPerHourCity gets average of all msmts //dailyTotalCity over msmtCount
Add dailyTotalCity to avgPerDay
Print daily total and average per hour in the city using format:
// On 10/17 in Arlington the total rainfall was 1.0in with an average of 0.3in per hour
If prevMonth isnÕt zero and prevMonth is different from monthName or
[prevDay isnÕt zero and] prevDay isnÕt same as dayName
then
Find average for the day // avgPerDay / dayCount
Print average for the day in format:
// The average rainfall across the area on 10/17 was 7.18in
// 1 line of output per line of input plus 1 line of input for each day
Set cityCount to 0 and
Increment dayCount // there are an unknown number of days,
Set prevMonth to monthName
Set prevDay to dayName
Set dailyTotalCity to 0
//End of loop for lines in file
Print number of lines in file and thank you message