Any help would be appreciated
Expand|Select|Wrap|Line Numbers
- #include <stdio.h>
- int main(void)
- {
- # ----------------------------------------------------------------------
- # Constants
- INT DECIMAL_PLACES = 4;
- SET NO_PATTERN = -1
- SET MAX_STATIONS = 22
- SET MAX_MONTHS = 12
- SET FILE_NAME = "data.csv"
- SET TOLERANCE = 0.1
- SET EMPTY_VALUE = 10.0
- SET LOW_ERROR = 0.0
- SET HIGH_ERROR = 1.0
- # ----------------------------------------------------------------------
- # A function that takes a number and returns it with the desired number of decimal places
- FUNCTION roundValue(value, decimalPlaces)
- counter = 0
- magnitude = 1
- FOR counter = 1 TO decimalPlaces
- magnitude = magnitude * 10
- END FOR
- value = (value * magnitude) / magnitude
- return value
- END FUNCTION
- # ----------------------------------------------------------------------
- # A function to convert a value to the absolute value
- FUNCTION ABS(value)
- IF value < 0
- return (value * -1)
- else
- return value
- END FUNCTION
- # ----------------------------------------------------------------------
- # MAIN
- MAIN
- # Variables
- # Used in the file reading
- tempFloat = 0.0
- temp = 0
- newline = ' '
- # General variables
- counter = 0
- tolerance = 0.0
- stationIndex = 0
- monthIndex = 0
- total = 0.0
- min = HIGH_ERROR
- month = 0
- # ----------------------------------------------------------------------
- # Data stores
- # Station data
- stations(MAX_STATIONS)
- stationAvg(MAX_STATIONS)
- stationMonth(MAX_STATIONS)
- stationMin(MAX_STATIONS)
- # Data Stores
- humidityData(MAX_STATIONS, MAX_MONTHS)
- minMaxMap(MAX_STATIONS, MAX_MONTHS)
- toleranceMap(MAX_STATIONS, MAX_MONTHS)
- # Month counts
- monthCount(MAX_MONTHS)
- # ----------------------------------------------------------------------
- # Initialise varaibles
- FOR stationIndex = 1 TO MAX_STATIONS
- stations(stationIndex) = 0
- stationAvg(stationIndex) = 0.0
- stationMonth(stationIndex) = 0
- stationMin(stationIndex) = LOW_ERROR
- monthCount(stationIndex) = 0
- END FOR
- FOR monthIndex = 1 TO MAX_MONTHS
- monthCount(monthIndex) = 0
- END FOR
- # Initialise the arrays to have default values
- FOR stationIndex = 1 TO MAX_STATIONS
- FOR monthIndex = 1 TO MAX_MONTHS
- humidityData(stationIndex, monthIndex) = 0.0
- minMaxMap(stationIndex, monthIndex) = 0.0
- toleranceMap(stationIndex, monthIndex) = 0.0
- END FOR
- END FOR
- # ----------------------------------------------------------------------
- # Open the file for reading
- OPEN(FILE_NAME)
- # Check for errors and exit if found
- IF FILE NOT_OPEN
- PRINT "Error: failed to open file for reading"
- EXIT
- END IF
- # Read the file into the data structure
- FOR stationIndex = 1 TO MAX_STATIONS
- READ Station INTO stations(stationIndex)
- # Read each months values up until the last value
- FOR monthIndex = 1 TO MAX_MONTHS
- READ Month INTO humidityData(stationIndex, monthIndex)
- END FOR
- END FOR
- # Close the file
- CLOSE FILE
- # ----------------------------------------------------------------------
- # Print out the humiditydata array
- # Print the header
- PRINT "--- humidity Data ---"
- PRINT " Month "
- FOR monthIndex = 1 TO MAX_MONTHS
- PRINT monthIndex
- END FOR
- PRINT NEWLINE
- # Print the data for each station
- FOR stationIndex = 1 TO MAX_STATIONS
- PRINT "Station ", stations(stationIndex)
- # Print up until the last value
- FOR monthIndex = 1 TO MAX_MONTHS
- PRINT humidityData(stationIndex, monthIndex)
- END FOR
- PRINT NEWLINE
- END FOR
- # ----------------------------------------------------------------------
- # Create the Min/Max issues (minMaxMap)
- FOR stationIndex = 1 TO MAX_STATIONS
- FOR monthIndex = 1 TO MAX_MONTHS
- tempfloat = roundValue(humidityData(stationIndex, monthIndex), DECIMAL_PLACES))
- # Check for value > top range and set to limit and store the error
- IF tempfloat > HIGH_ERROR THEN
- # Store the error in the error map
- minMaxMap(stationIndex, monthIndex) = humidityData(stationIndex, monthIndex)
- # Adjust the humidity data to the valid range
- humidityData(stationIndex, monthIndex) = HIGH_ERROR
- END IF
- # Check for value < lowest Range and set to limit and stor the error
- IF tempfloat < LOW_ERROR THEN
- # Store the error in the error map
- minMaxMap(stationIndex, monthIndex) = humidityData(stationIndex, monthIndex)
- # Adjust the humidity data to the valid range
- humidityData(stationIndex, monthIndex) = LOW_ERROR
- END IF
- END FOR
- END FOR
- # ----------------------------------------------------------------------
- # Print out the minMaxMap array
- # Print the header
- PRINT " --- Min Max Issues ---"
- PRINT " Month|"
- FOR monthIndex = 1 TO MAX_MONTHS
- PRINT monthIndex
- END FOR
- PRINT NEWLINE
- # Print the data for each station
- FOR stationIndex = 1 TO MAX_STATIONS
- PRINT "Station ", stations(stationIndex)
- # Print up until the last value
- FOR monthIndex = 1 TO MAX_MONTHS
- PRINT minMaxMap(stationIndex, monthIndex)
- END FOR
- PRINT NEWLINE
- END FOR
- # ----------------------------------------------------------------------
- # Calculate and store the averages for each station
- FOR stationIndex = 1 TO MAX_STATIONS
- # Reset the temp average store
- total = 0.0
- # Calculate the total for each station
- FOR monthIndex = 1 TO MAX_MONTHS
- tempfloat = roundValue(humidityData(stationIndex, monthIndex), DECIMAL_PLACES))
- total = total + tempfloat
- END FOR
- # Store the average for the station
- stationAvg(stationIndex) = total / MAX_MONTHS
- END FOR
- # ----------------------------------------------------------------------
- # Print out the Rounded and Corrected Data
- # Print the header
- PRINT " --- Rounded and Corrected Data ---"
- PRINT " Month "
- FOR monthIndex = 1 TO MAX_MONTHS
- PRINT monthIndex
- END FOR
- PRINT "Average "
- # Print the data for each station
- FOR stationIndex = 1 TO MAX_STATIONS
- PRINT "Station ", stations(stationIndex)
- # Print up until the last value
- FOR monthIndex = 1 TO MAX_MONTHS
- PRINT roundValue(humidityData(stationIndex, monthIndex), DECIMAL_PLACES)
- END FOR
- # Print the average for the station
- PRINT stationAvg(stationIndex)
- PRINT NEWLINE
- END FOR
- # ----------------------------------------------------------------------
- # Determine the values that are within the tolerance
- FOR stationIndex = 1 TO MAX_STATIONS
- FOR monthIndex = 1 TO MAX_MONTHS
- # Calculate the tolerance
- tolerance = ABS(humidityData(stationIndex, monthIndex) - stationAvg(stationIndex))
- # Check if in range and store
- IF tolerance <= TOLERANCE THEN
- toleranceMap(stationIndex, monthIndex) = tolerance
- ELSE
- # Store as an empty value
- toleranceMap(stationIndex, monthIndex) = EMPTY_VALUE
- END IF
- END FOR
- END FOR
- # ----------------------------------------------------------------------
- # Print out the Tolerances array
- PRINT " --- Tolerances ---"
- # Print the header
- PRINT " Month|"
- FOR monthIndex = 1 TO MAX_MONTHS
- PRINT monthIndex
- END FOR
- PRINT NEWLINE
- # Print the data for each station
- FOR stationIndex = 1 TO MAX_STATIONS
- PRINT "Station ", stations(stationIndex)
- # Print up until the last value
- FOR monthIndex = 1 TO MAX_MONTHS
- # If the value is not an empty value print value
- IF toleranceMap(stationIndex, monthIndex) != EMPTY_VALUE THEN
- PRINT toleranceMap(stationIndex, monthIndex)
- ELSE
- PRINT " |"
- END IF
- END FOR
- PRINT NEWLINE
- END FOR
- # ----------------------------------------------------------------------
- # Determine the min value for each station and record the month. Keep count of the month.
- FOR stationIndex = 1 TO MAX_STATIONS
- # Read up until the last value
- # Set the min to highest so the comparison will work
- min = HIGH_ERROR
- # Reset month variable
- month = 0
- FOR monthIndex = 1 TO MAX_MONTHS
- IF toleranceMap(stationIndex, monthIndex) < min THEN
- # Store the min
- min = toleranceMap(stationIndex, monthIndex)
- # Record the month
- month = monthIndex
- END IF
- END FOR
- # Store the results
- stationMonth(stationIndex) = month
- stationMin(stationIndex) = min
- monthCount(month) = monthCount(month) + 1
- END FOR
- # ----------------------------------------------------------------------
- # Display the Station/Month table
- PRINT NEWLINE
- PRINT "Schedule"
- PRINT "Station | Lowest | Month"
- FOR stationIndex = 1 TO MAX_STATIONS
- PRINT stationIndex, stationMin(stationIndex), stationMonth(stationIndex)
- END FOR
- # ----------------------------------------------------------------------
- # Display the Month count
- PRINT NEWLINE
- PRINT "Summary"
- PRINT "Month | Count"
- FOR counter = 1 TO MAX_MONTHS
- PRINT counter, monthCount(counter)
- END FOR
- END MAIN