methods

error checking, overflow

timing

output as a web page
// wyatt
//
// write simple output to browser and console
// use methods
// look at overflow exceptions
// use timing

namespace WebOutput
{
    class Program
    {
        /////////////////////////////////////////////
        // call and display
        static void Main()
        {
            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            int num1 = 1;
            int num2;
            //num2 = 60000; 
            //num2 = 70000;     // 70k SEEMS ok... ??
            num2 = 1000000000;  // what???

            // time the call
            watch.Reset();
            watch.Start();            
            int sum = SumRange(num1, num2);
            watch.Stop();          

            // write outputs           
            System.Console.WriteLine("Summing took " +
                                     watch.Elapsed.TotalSeconds.ToString("0.000") + 
                                     " seconds");   
            WriteToConsole(sum, num1, num2);
            WriteToWebPage(sum, num1, num2)
        }
        
        /////////////////////////////////////////////
        // calculate sum of range of numbers
        // errors in the sum??
        static int SumRange(int start, int stop)
        {
            int sum=0;
            try
            {
               //hope the sum is not too big!
               for (int i = start; i <= stop; i++)
               {
                  //checked
                  { sum += i; }

                  // if false,show msg 
                  //System.Diagnostics.Debug.Assert(sum >= 0, "Problem?! " + sum);
               }
            }
            catch(System.Exception ex)
            {
               System.Console.WriteLine("error " + ex.Message);
               throw;
            }

            return sum;
        }        
        
        
        ///////////////////////////////////////////////////////
        // write output to console
        static void WriteToConsole(long sum, int num1, int num2)
        {
            // include params passed in by caller
            System.Console.WriteLine("Range: " + num1.ToString("N0") + 
                                     " to " + num2.ToString("N0") +
                                     "\nSum: " + sum.ToString("N0") );
        }
        
        
        ///////////////////////////////////////////////////////
        // create out file
        // write html code to file
        // start default browser
        static void WriteToWebPage(long sum, int num1, int num2)
        {
            // create output file
            System.IO.StreamWriter writer;
            writer = new System.IO.StreamWriter("output.html");

            // write html to created file
            writer.WriteLine("<!doctype html>");
            writer.WriteLine("<html>");
            writer.WriteLine("<head>");
            writer.WriteLine("<meta charset=\"UTF-8\" />");
            writer.WriteLine("<title>Output</title>");
            writer.WriteLine("</head>");
            writer.WriteLine("<body>");
            // include params passed in by caller
            writer.WriteLine(
                "<h1>Range: " + num1.ToString("N0") + " to " + num2.ToString("N0") +
                "<br />Sum: " + sum.ToString("N0") + 
                "</h1>");
            writer.WriteLine("</body>");
            writer.WriteLine("</html>");

            // close file
            writer.Flush();
            writer.Close();

            // start the default application associated with "html" file
            System.Diagnostics.Process.Start("output.html"); 
        }
    }
}