File I/O and 2-D array: Write a C++ program which reads data from a file and pro
ID: 3686744 • Letter: F
Question
File I/O and 2-D array: Write a C++ program which reads data from a file and processes it as listed in the requirements. Download the file “Data file for assignment 9” from Blackboard to your account in the lab and save it with the filename rainfall.dat. This file contains the total rainfall for each month of a year in millimeters for 20 different weather stations. Each line of the file contains the data for one weather station, starting with the weather station number and followed by the 12 rainfall measurements – one for each month. This is a text file and you can open it and view the data. You will see that all the values are integers.
Here is the link to rainfall file. http://www.filedropper.com/rainfall
Requirements:
Name the source file for your program program9.cpp
To make it easy to change the program to work for different numbers of weather stations, define the following constant (may be local or global):
const int WEATHER_STATIONS = 20;
Then, wherever the number of weather stations is needed in the program, use the constant WEATHER STATIONS instead of the literal number 20.
Make an array with the definition (must be a local variable):
double rainfall[WEATHER_STATIONS][12];
Read in the data from the file rainfall.dat, convert each value to inches, and save the rainfall measurement in inches to this array. To do the conversion, use the formula: 25.4 mm = 1 in
Write four functions with the following prototypes:
void display_years_more(double rainfall[][12], int stations, double cutoff); void display_years_less(double rainfall[][12], int stations, double cutoff); void display_months_more(double rainfall[][12], int stations, double cutoff); void display_months_less(double rainfall[][12], int stations, double cutoff);
These four functions are used to respond to the user’s choices from the menu. For each of these functions, the parameter rainfall is a 2-dimensional array of the number stations (the second parameter) of 12 months each. The third parameter cutoff is a value to which data in rainfall is compared. The type of comparison differs between the four different functions:
For display years more: If the total rainfall of all 12 months for a weather station is GREATER than cutoff, then display the rainfall data (for all 12 months) for that station.
For display years less: If the total rainfall of all 12 months for a weather station is LESS than cutoff, then display the rainfall data (for all 12 months) for that station.
For displaymonths more: Display the data for months whose rainfall is GREATER than cutoff.
For displaymonths less: Display the data for months whose rainfall is LESS than cutoff.
The output displayed by these functions must have the layout and format shown in the sample run below.
The program must display a menu of choices to the user, accept their selection from the menu and respond to it, then display the menu again, until the user chooses to quit.
The valid menu choices are the numbers 1, 2, 3, 4, and 5. If the user enters an invalid integer, the menu must be redisplayed. The program is only required to work if the menu choice is an integer.
If the user make a choice of 1 through 4 from the menu, prompt them to enter a value for the cutoff, and then call the relevant function to respond to their choice.
Rainfall values displayed in the output must be shown with 2 decimal places.
Numbers which form columns in the output must be right-aligned as shown in the sample run below.
A sample run of your program should look like:
Rainfall data to display:
Weather stations with total rainfall for year MORE than a cutoff
Weather stations with total rainfall for year LESS than a cutoff
Rainfall for a month MORE than a cutoff
Rainfall for a month LESS than a cutoff
Quit
Enter the number of your choice: 1
Enter the rainfall for which to find years with more rain (in inches): 21
Weather stations with more than 21.00 inches rain for the year:
WS 13: 0.51 0.83 0.79 1.54 1.26 1.61 2.36 2.76 2.95 3.23 2.09 1.65
WS 14: 0.91 0.79 0.75 0.75 4.69 4.76 4.65 4.61 4.69 4.69 4.72 4.76 WS 15: 0.91 1.69 2.44 3.19 3.94 4.76 5.47 6.14 6.50 7.24 8.03 10.04 WS 19: 0.67 0.98 0.91 1.65 2.05 1.50 4.80 4.96 4.88 9.33 10.20 5.04
Rainfall data to display:
Weather stations with total rainfall for year MORE than a cutoff
Weather stations with total rainfall for year LESS than a cutoff
Rainfall for a month MORE than a cutoff
Rainfall for a month LESS than a cutoff
Quit
Enter the number of your choice: 2
Enter the rainfall for which to find years with less rain (in inches): 8.2
Weather stations with less than 8.20 inches rain for the year:
WS 5: 0.63 0.47 0.35 0.31 0.08 0.55 0.59 0.67 0.59 0.75 0.43 0.71 WS 10: 0.75 0.28 0.51 0.35 0.31 0.75 0.51 0.98 0.87 1.02 0.91 0.43
WS 18: 0.39 0.35 0.28 0.24 0.24 0.47 0.71 1.34 1.06 1.10 0.83 0.51
WS 20: 0.47 0.55 0.28 0.35 0.35 0.67 1.10 1.18 0.75 0.71 0.63 0.67
Rainfall data to display:
Weather stations with total rainfall for year MORE than a cutoff
Weather stations with total rainfall for year LESS than a cutoff
Rainfall for a month MORE than a cutoff
Rainfall for a month LESS than a cutoff
Quit
Enter the number of your choice: 3
Enter the rainfall for which to find months with more rain (in inches): 6.5
Weather stations and months with more than 6.50 inches rain for the month:
Weather station 15, month 10: 7.24 inches
Weather station 15, month 11: 8.03 inches
Weather station 15, month 12: 10.04 inches
Weather station 19, month 10: 9.33 inches
Weather station 19, month 11: 10.20 inches
Rainfall data to display:
Weather stations with total rainfall for year MORE than a cutoff
Weather stations with total rainfall for year LESS than a cutoff
Rainfall for a month MORE than a cutoff
Rainfall for a month LESS than a cutoff
Quit
Enter the number of your choice: 4
Enter the rainfall for which to find months with less rain (in inches): 0.3
Weather stations and months with less than 0.30 inches rain for the month:
Weather station 4, month 4: 0.28 inches
Weather station 5, month 5: 0.08 inches
Weather station 10, month 2: 0.28 inches
Weather station 18, month 3: 0.28 inches
Weather station 18, month 4: 0.24 inches
Weather station 18, month 5: 0.24 inches
Weather station 20, month 3: 0.28 inches
Rainfall data to display:
Weather stations with total rainfall for year MORE than a cutoff
Weather stations with total rainfall for year LESS than a cutoff
Rainfall for a month MORE than a cutoff
Rainfall for a month LESS than a cutoff
Quit
Enter the number of your choice: 5
Hints:
Use the functions open, fail, and close from the fstream library.
Note that:
Because the weather station numbers are consecutive, it isn’t necessary to store these numbers in the program – they can be obtained from the array positions.
The weather station and month numbers shown in the output start at 1, so are 1 higher than the array index which the data comes from.
Remember to set the display format for floating point numbers to 2 decimal places.
Use setw to line up the numbers in columns.
Explanation / Answer
#include #include #include using namespace std; const int WEATHER_STATIONS = 20; // helper function to print workstation void printWS(double rainfall[][12], int wsIndex) { coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.