Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# File I/O Efficiency

I have done a homework assignment, here is the problem statement:

Your program should work as follows:

  1. Ask the user to give you a file name. Get the file name and save it.
  2. Open the file.
  3. From the file read a temperature and a wind speed. Both values should be stored in variables declared as double. The file is a text file. Each line of the file contains a temperature and a wind speed value.
  4. Calculate the wind chill factor using a programmer written method, and display the result in the form:

    For t = temperature from file and v = wind speed from file Wind chill index = calculated result degrees Fahrenheit.

    Show all numbers with two digits after the decimal point. (Remember-no magic numbers!)

  5. Repeat these steps until an end of file is encountered.

I have completed the assignment, my code is below, I was just wondering if there was any way to make it more efficient, or if there are some different and creative ways to accomplish this problem, I already turned this in and got 50/50, but I'm just curious as to how some of you advanced and skilled programmers would approach this problem.

using System;
using System.IO;

class Program
{
    // declare constants to use in wind chill factor equation - no magic numbers
    const double FIRST_EQUATION_NUMBER = 35.74;
    const double SECOND_EQUATION_NUMBER = 0.6215;
    const double THIRD_EQUATION_NUMBER = 35.75;
    const double FOURTH_EQUATION_NUMBER = 0.4275;
    const double EQUATION_EXPONENT = 0.16;
    const int DEGREE_SYMBOL_NUMBER = 176;

    static void Main()
    {
        // declare and initialize some variables
        string filePath = "";
        string line = "";
        double temperature = 0.0;
        double windSpeed = 0.0;
        double windChillFactor = 0.0;
        char degreeSymbol = (char)DEGREE_SYMBOL_NUMBER;

        // ask user for a file path
        Console.Write("Please enter a valid file path: ");
        filePath = Console.ReadLine();

        // create a new instance of the StreamReader class
        StreamReader windChillDoc = new StreamReader(@filePath);

        // start the read loop
        do 
        {
            // read in a line and save it as a string variable
            line = windChillDoc.ReadLine();

            // is resulting string empty? If not, continue execution
            if (line != null)
            {
                string[] values = line.Split();
                temperature = double.Parse(values[0]);
                windSpeed = double.Parse(values[1]);

                windChillFactor = WindChillCalc(temperature, windSpeed);

                Console.WriteLine("\nFor a temperature {0:f2} F{1}", temperature, degreeSymbol);
                Console.WriteLine("and a wind velocity {0:f2}mph", windSpeed);
                Console.WriteLine("The wind chill factor = {0:f2}{1}\n", windChillFactor, degreeSymbol);
            }
        } while (line != null);

        windChillDoc.Close();

        Console.WriteLine("\nReached the end of the file, press enter to exit this program");

        Console.ReadLine();
    }//End Main()

    /// <summary>
    /// The WindChillCalc Method
    /// Evaluates a wind chill factor at a given temperature and windspeed
    /// </summary>
    /// <param name="temperature">A given temperature</param>
    /// <param name="ws">A given windspeed</param>
    /// <returns>The calculated wind chill factor, as a double</returns>
    static double WindChillCalc(double temperature, double ws)
    {
        double wci = 0.0;
        wci = FIRST_EQUATION_NUMBER + (SECOND_EQUATION_NUMBER * temperature) - (THIRD_EQUATION_NUMBER * (Math.Pow(ws, EQUATION_EXPONENT))) + (FOURTH_EQUATION_NUMBER * temperature * (Math.Pow(ws, EQUATION_EXPONENT)));
        return wci;
    }
}//End class Program 

Feel free to tell me what you think of it.

like image 858
Alex Avatar asked Nov 19 '09 23:11

Alex


2 Answers

Your way looks good, but:

  • It would look nicer if you used PascalCase for the constants, as that's what coding conventions for c# use.
  • you should wrap the StreamReader in a using statement, so it gets properly disposed once you're done.
  • You should probably also wrap it in a try block (and a catch to properly handle the exception) to make sure that you don't get a FileNotFound exception.
  • It's probably a better idea to structure your while loop the following way:

while((line = windChillDoc.ReadLine()) != null) { ... } [Darn formatting won't work right!]

Other than that though, i wouldn't know as i'm not familiar with weather calculations :)

like image 166
RCIX Avatar answered Oct 03 '22 09:10

RCIX


You're not going to get much zippier than that for file IO in C#. Depending on the size of the data set it may be worth using a buffered reader, but for sufficiently small files, it's just not worth it. I'd leave it as-is.

like image 42
Ken Mason Avatar answered Oct 03 '22 09:10

Ken Mason