473,412 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Hi, Could you please help me convert the following pseudocode to C? Any help would

Hi, Could you please help me convert the following pseudocode to C?
Any help would be appreciated
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h> 
  2.  
  3. int main(void)
  4. {
  5.  
  6. # ----------------------------------------------------------------------
  7. # Constants
  8. INT DECIMAL_PLACES = 4;
  9. SET NO_PATTERN  = -1
  10. SET MAX_STATIONS = 22
  11. SET MAX_MONTHS = 12
  12. SET FILE_NAME = "data.csv"
  13. SET TOLERANCE = 0.1
  14. SET EMPTY_VALUE = 10.0
  15. SET LOW_ERROR = 0.0
  16. SET HIGH_ERROR = 1.0
  17. # ----------------------------------------------------------------------
  18. # A function that takes a number and returns it with the desired number of decimal places
  19. FUNCTION roundValue(value, decimalPlaces)
  20.     counter = 0
  21.     magnitude = 1
  22.     FOR counter = 1 TO decimalPlaces
  23.         magnitude = magnitude * 10
  24.     END FOR
  25.     value = (value * magnitude) / magnitude
  26.  
  27.     return value
  28.  
  29. END FUNCTION
  30. # ----------------------------------------------------------------------
  31. # A function to convert a value to the absolute value 
  32. FUNCTION ABS(value)
  33.     IF value < 0
  34.         return (value * -1)
  35.     else
  36.         return value
  37. END FUNCTION
  38. # ----------------------------------------------------------------------
  39. # MAIN
  40. MAIN
  41.     # Variables
  42.     # Used in the file reading
  43.     tempFloat = 0.0
  44.     temp = 0
  45.     newline = ' '
  46.     # General variables
  47.     counter = 0
  48.     tolerance = 0.0
  49.     stationIndex = 0
  50.     monthIndex = 0
  51.     total = 0.0
  52.     min = HIGH_ERROR
  53.     month = 0
  54.     # ----------------------------------------------------------------------
  55.     #  Data stores
  56.     # Station data
  57.     stations(MAX_STATIONS)
  58.     stationAvg(MAX_STATIONS)
  59.     stationMonth(MAX_STATIONS)
  60.     stationMin(MAX_STATIONS)
  61.     # Data Stores
  62.     humidityData(MAX_STATIONS, MAX_MONTHS)
  63.     minMaxMap(MAX_STATIONS, MAX_MONTHS)
  64.     toleranceMap(MAX_STATIONS, MAX_MONTHS)
  65.     # Month counts
  66.     monthCount(MAX_MONTHS)
  67.     # ----------------------------------------------------------------------
  68.     # Initialise varaibles
  69.     FOR stationIndex = 1 TO MAX_STATIONS
  70.         stations(stationIndex) = 0
  71.         stationAvg(stationIndex) = 0.0
  72.         stationMonth(stationIndex) = 0
  73.         stationMin(stationIndex) = LOW_ERROR
  74.         monthCount(stationIndex) = 0
  75.     END FOR
  76.  
  77.     FOR monthIndex = 1 TO MAX_MONTHS
  78.         monthCount(monthIndex) = 0
  79.     END FOR
  80.  
  81.     # Initialise the arrays to have default values
  82.     FOR stationIndex = 1 TO MAX_STATIONS
  83.         FOR monthIndex = 1 TO MAX_MONTHS
  84.             humidityData(stationIndex, monthIndex) = 0.0
  85.             minMaxMap(stationIndex, monthIndex) = 0.0
  86.             toleranceMap(stationIndex, monthIndex) = 0.0
  87.         END FOR
  88.     END FOR
  89.     # ----------------------------------------------------------------------
  90.     # Open the file for reading 
  91.     OPEN(FILE_NAME)
  92.  
  93.     # Check for errors and exit if found
  94.     IF FILE NOT_OPEN
  95.         PRINT "Error: failed to open file for reading"
  96.         EXIT
  97.     END IF
  98.  
  99.     # Read the file into the data structure
  100.     FOR stationIndex = 1 TO MAX_STATIONS
  101.         READ Station INTO stations(stationIndex)
  102.         # Read each months values up until the last value
  103.         FOR monthIndex = 1 TO MAX_MONTHS
  104.             READ Month INTO humidityData(stationIndex, monthIndex)
  105.         END FOR
  106.     END FOR
  107.     # Close the file
  108.     CLOSE FILE
  109.     # ----------------------------------------------------------------------
  110.     # Print out the humiditydata array
  111.     # Print the header
  112.     PRINT "--- humidity Data ---"
  113.     PRINT "      Month "
  114.     FOR monthIndex = 1 TO MAX_MONTHS
  115.         PRINT monthIndex
  116.     END FOR
  117.     PRINT NEWLINE
  118.     # Print the data for each station
  119.     FOR stationIndex = 1 TO MAX_STATIONS
  120.         PRINT "Station ", stations(stationIndex)
  121.         # Print up until the last value
  122.         FOR monthIndex = 1 TO MAX_MONTHS
  123.             PRINT humidityData(stationIndex, monthIndex)
  124.         END FOR
  125.         PRINT NEWLINE
  126.     END FOR
  127.     # ----------------------------------------------------------------------
  128.     # Create the Min/Max issues (minMaxMap)
  129.     FOR stationIndex = 1 TO MAX_STATIONS
  130.         FOR monthIndex = 1 TO MAX_MONTHS
  131.             tempfloat = roundValue(humidityData(stationIndex, monthIndex), DECIMAL_PLACES))
  132.  
  133.             # Check for value  > top range and set to limit and store the error
  134.             IF tempfloat > HIGH_ERROR THEN
  135.                 # Store the error in the error map
  136.                 minMaxMap(stationIndex, monthIndex) = humidityData(stationIndex, monthIndex)
  137.                 # Adjust the humidity data to the valid range
  138.                 humidityData(stationIndex, monthIndex) = HIGH_ERROR
  139.             END IF
  140.  
  141.             # Check for value  < lowest Range and set to limit and stor the error
  142.             IF tempfloat < LOW_ERROR THEN
  143.                 # Store the error in the error map
  144.                 minMaxMap(stationIndex, monthIndex) = humidityData(stationIndex, monthIndex)
  145.                 # Adjust the humidity data to the valid range
  146.                 humidityData(stationIndex, monthIndex) = LOW_ERROR
  147.             END IF
  148.         END FOR
  149.     END FOR
  150.     # ----------------------------------------------------------------------
  151.     # Print out the minMaxMap array
  152.     # Print the header
  153.     PRINT " --- Min Max Issues ---"
  154.     PRINT "      Month|"
  155.     FOR monthIndex = 1 TO MAX_MONTHS
  156.         PRINT monthIndex
  157.     END FOR
  158.     PRINT NEWLINE
  159.     # Print the data for each station
  160.     FOR stationIndex = 1 TO MAX_STATIONS
  161.         PRINT "Station ", stations(stationIndex)
  162.         # Print up until the last value
  163.         FOR monthIndex = 1 TO MAX_MONTHS
  164.             PRINT minMaxMap(stationIndex, monthIndex)
  165.         END FOR
  166.         PRINT NEWLINE
  167.     END FOR
  168.     # ----------------------------------------------------------------------
  169.     # Calculate and store the averages for each station
  170.     FOR stationIndex = 1 TO MAX_STATIONS
  171.         # Reset the temp average store
  172.         total = 0.0
  173.         # Calculate the total for each station
  174.         FOR monthIndex = 1 TO MAX_MONTHS
  175.             tempfloat = roundValue(humidityData(stationIndex, monthIndex), DECIMAL_PLACES))
  176.             total = total + tempfloat
  177.         END FOR
  178.         # Store the average for the station
  179.         stationAvg(stationIndex) = total / MAX_MONTHS
  180.     END FOR
  181.     # ----------------------------------------------------------------------
  182.     # Print out the Rounded and Corrected Data 
  183.     # Print the header
  184.     PRINT " --- Rounded and Corrected Data  ---"
  185.     PRINT "      Month "
  186.     FOR monthIndex = 1 TO MAX_MONTHS
  187.         PRINT monthIndex
  188.     END FOR
  189.     PRINT "Average "
  190.     # Print the data for each station
  191.     FOR stationIndex = 1 TO MAX_STATIONS
  192.         PRINT "Station  ", stations(stationIndex)
  193.         # Print up until the last value
  194.         FOR monthIndex = 1 TO MAX_MONTHS
  195.             PRINT roundValue(humidityData(stationIndex, monthIndex), DECIMAL_PLACES)
  196.         END FOR
  197.         # Print the average for the station
  198.         PRINT stationAvg(stationIndex)
  199.  
  200.         PRINT NEWLINE
  201.     END FOR
  202.     # ----------------------------------------------------------------------
  203.     # Determine the values that are within the tolerance
  204.     FOR stationIndex = 1 TO MAX_STATIONS
  205.         FOR monthIndex = 1 TO MAX_MONTHS
  206.             # Calculate the tolerance
  207.             tolerance = ABS(humidityData(stationIndex, monthIndex) - stationAvg(stationIndex))
  208.             # Check if in range and store
  209.             IF tolerance <= TOLERANCE THEN
  210.                 toleranceMap(stationIndex, monthIndex) = tolerance
  211.             ELSE  
  212.                 # Store as an empty value
  213.                 toleranceMap(stationIndex, monthIndex) = EMPTY_VALUE
  214.             END IF
  215.         END FOR
  216.     END FOR
  217.     # ----------------------------------------------------------------------
  218.     # Print out the Tolerances array
  219.     PRINT " --- Tolerances  ---"
  220.     # Print the header
  221.     PRINT "      Month|"
  222.     FOR monthIndex = 1 TO MAX_MONTHS
  223.         PRINT monthIndex
  224.     END FOR
  225.     PRINT NEWLINE
  226.     # Print the data for each station
  227.     FOR stationIndex = 1 TO MAX_STATIONS
  228.         PRINT "Station ", stations(stationIndex)
  229.         # Print up until the last value
  230.         FOR monthIndex = 1 TO MAX_MONTHS
  231.             # If the value is not an empty value print value
  232.             IF toleranceMap(stationIndex, monthIndex) != EMPTY_VALUE THEN
  233.                 PRINT toleranceMap(stationIndex, monthIndex)
  234.             ELSE
  235.                 PRINT "        |"
  236.             END IF
  237.         END FOR
  238.  
  239.         PRINT NEWLINE
  240.     END FOR
  241.     # ----------------------------------------------------------------------
  242.     # Determine the min value for each station and record the month.  Keep count of the month.
  243.     FOR stationIndex = 1 TO MAX_STATIONS
  244.         # Read up until the last value
  245.         # Set the min to highest so the comparison will work
  246.         min = HIGH_ERROR
  247.         # Reset month variable
  248.         month = 0
  249.         FOR monthIndex = 1 TO MAX_MONTHS
  250.             IF toleranceMap(stationIndex, monthIndex) < min THEN
  251.                 # Store the min
  252.                 min = toleranceMap(stationIndex, monthIndex)
  253.                 # Record the month
  254.                 month = monthIndex
  255.             END IF
  256.         END FOR
  257.         # Store the results
  258.         stationMonth(stationIndex) = month
  259.         stationMin(stationIndex) = min
  260.         monthCount(month) = monthCount(month) + 1
  261.     END FOR
  262.     # ----------------------------------------------------------------------
  263.     # Display the Station/Month table
  264.     PRINT NEWLINE 
  265.     PRINT "Schedule"
  266.     PRINT "Station | Lowest | Month"
  267.     FOR stationIndex = 1 TO MAX_STATIONS
  268.         PRINT stationIndex, stationMin(stationIndex), stationMonth(stationIndex)
  269.     END FOR
  270.     # ----------------------------------------------------------------------
  271.     # Display the Month count
  272.     PRINT NEWLINE 
  273.     PRINT "Summary"
  274.     PRINT "Month | Count"
  275.     FOR counter = 1 TO MAX_MONTHS
  276.         PRINT counter, monthCount(counter)
  277.     END FOR
  278.  
  279. END MAIN
Sep 5 '18 #1
2 1114
zmbd
5,501 Expert Mod 4TB
Zeewa19921992:
Welcome to Bytes.com

We're not a code writing service. If you have a specific issue converting the posted script to C then we can help you work through that issue.
Sep 5 '18 #2
donbock
2,426 Expert 2GB
Lines 3 and 4 (C code for start of function main) should be moved to immediately above or immediately below line 40 (pseudo code MAIN).

Did you write the pseudo code yourself?
Have you written any C programs before?
Sep 6 '18 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Rex_chaos | last post by:
Hi all, I have a question about datatype converting. Consider the following types std::complex<double> and struct MyComplex { double re, im; }
3
by: William Buchanan | last post by:
Hi I have the following stored proc in interbase (which might contain errors - i'm doing it off the top of my head), which I would like to convert into oracle. Can you help? What I want back is...
1
by: Oliver | last post by:
Hello, I am writing an application that will be used across multiple time zones (Eastern to Pacific) and since much of the application revolves around time stamps, I need a best practice for...
4
by: Lee | last post by:
If I please request help on convert ASP page to be ASPX page. My code is: If NOT RS.EOF Then Else Code... End If Me cannot get If..Then...Else work. ASP dot NET not like EOF.
5
by: manmit.walia | last post by:
Hello All, I am stuck on a conversion problem. I am trying to convert my application which is written in VB.NET to C# because the project I am working on currently is being written in C#. I tried...
4
by: Dhruba Bandopadhyay | last post by:
I need help converting a ASP page to ASP.NET. I have a ASP page that is in the form of: <% Sub Start() Call DrawPage1() Call DrawPage2() End Sub Sub DrawPage1() %> <!-- Full HTML code for a...
4
by: -D- | last post by:
I'm new to .net and just learning c#. I have the following code in vb and want to convert this to c# Public Class PositionData Private strText As String Private strUrl As String Public...
2
by: Don I | last post by:
Microsoft Visual Basic .NET 55501-640-5372443-18381 I'm trying to access the program's help files but all I get is "Page can not be displayed". Any idea what's up? Thx. Don I
6
by: Sergey Poberezovskiy | last post by:
I have the following code in C# that I have trouble converting to VB(2.0): private delegate void openDialog(); private void openWindowsDialog(openDialog open) { Thread thread = new Thread(new...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.