Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Arrays & Functions: Tracking Precipitation
The National Climatic Data Center has hired you to write a program to fix a major mess with one of their precipitation report generating systems. Normally this would be straightforward, but a computer glitch has caused some of the data to appear out of order, to be missing, or to have a corrupted date. You are responsible for storing as much valid data as possible, recognizing errors in the data file, and displaying a summary of daily precipitation amounts for the specified month in 2003.
Sample input data:
Here is a sample input file, named Precip.txt, for the program
The line contains the full month name, which is capitalized, followed immediately by a comma, and then the year 2003 for recorded information. Note that 2003 is not a leap year. Each of the remaining lines contains two pieces of data for a single date in the month, separated by whitespace (either spaces or tabs):
date an integer that may be valid or invalid for the specified month
precipitation the amout of precipitation recorded on this date
There is no specified limit on the number of data lines, so your program must be designed to detect when it's out of input and stop automatically.
Calculations and error checking:
Your program will read and store the precipitation information in an array. The array must be large enough to store information for any month, although the entire array may not always be used. Remember to initialize the array values to some impossible precipitation value so that you can tell is a given day has valid data. Once all of the input data has been read in, you will need to calculate the minimum, maximum, and average precipitation values.
December, 2003
2 0
4 0
5 0
26 0
6 0
8 0.01
9 0
10 0.02
13 0
114 0
15 0
17 0.55
19 1.8
8 0.08
20 4.12
24 0
32 0.73
28 0
11 0.05
12 0.01
29 0The input file may contain two kinds of errors. The first is that the day given in the input might be invalid (too small or too large). The second is that a file might contain multiple entries for the same day. These errors should be recognized , and an error message should be displayed, including the line number where the error occurred. See the sample output file below for the exact format of the error messages.
Sample output:
Here is a sample output file, named Report.txt, which corresponds to the input data given above:December,2003
Error Day Line
Invalid 114 13
Repeated 8 17
Invalid 32 20Day AmountGraph
1 NA
2 0.00
3 NA
4 0.00
5 0.00
6 0.00
7 NA
8 0.01*
9 0.00
10 0.02*
11 0.05*
12 0.01*
13 0.00
14 NA
15 0.00
16 NA
17 0.55***
18 NA
19 1.80********
20 4.12*****************
21 NA
22 NA
23 NA
24 0.00
25 NA
26 0.00
27 NA
28 0.00
29 0.00
30 NA
31 NA
Minimum Maximum Average
0.00 4.12 0.21
The first line contains the month, and year of the precipitation data. Remaining information is printed as follows:
If any errors were found in the input data, then a line of header information is printed for the error output. The errors should be listed in the same order as they were discovered in the input file. For each error, list the type of error (either "Invalid" or "Repeated"), the Day value listed in the input file, and what line number in the input file the error was detected. After the errors, a blank line should be output.
The next line contains the headers for the precipitation histogram. For each day, the date number should be listed, followed by the amount of precipitation for that day. If no precipitation is available for a day, then NA should be printed for that day. Following the day and amount, a graph containing one star for each .25 inches or part thereof is displayed. For example, 0.01-0.25 inches will display one star, 0.26-0.50 two stars, 0.51-0.75 three stars. If no precipitation was detected on a given day, or if no data is available, then no stars should be displayed. After the histogram graph for the whole month, there should be a blank line.
The maximum, minimum, and average precipitation amounts are printed. The average is the sum of the valid precipitation values divided by the number of days in the month. If no information is available for the entire, NA should be printed for all three values.The program, at a minimum must contain 5 functions, in addition to main().
At least one of these functions must return a non-trivial value.
All functions need a header comment in addition to any necessary in-line comments.
Your basic data structure for the precipitation data must be an array.
Hello all
I tried this program, but the problem is when i read the input for the month and the precipitation values and when i store them in an array, it stores upto the values of precipitation when the date is valid. But as soon as 114 (thats the date in the input file) comes the array does not store the correct values.I initialized the array with -2 value. But I am unable to store the values of precipitation into an array.
here is what i am trying to do:
while (In )
{
precipitationArray[date-1 =precipitation;
In>>date;
In>>precipitation;
}I initialized the array with this function
void array( double arr[])
{
for (int usage=0; usage31; usage++)
{
Array[usage]= -2.0;
}
}Please anyone can help me how should i store the values in an array and if anyone can read the program question and has any other suggestions, please help me.
I am using visual c++.net 2002.Please help.
patrick.
email: patrick_devens@yahoo.com

You didn't include your array declaration, but from looking at the other code, I would guess that you declared the array to have 31 elements.
So when you input 114, your code will be trying to store the precipition value in position 113 of the 31 element array. This won't work.
The best solution is to check the input date and if it is outside of the acceptable range (1-31), then print an error message and don't try to store that piece of data.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |