using System; using System.IO; using System.Linq; using System.Collections.Generic; using System.Diagnostics; public class station_data { private int year_; private int month_; private double tmax_; private double tmin_; private int af_days_; private double rainfall_; private double sunshine_; public station_data(int year,int month,double tmax,double tmin,int af_days,double rainfall,double sunshine) { year_ = year; month_ = month; tmax_ = tmax; tmin_ = tmin; af_days_ = af_days; rainfall_ = rainfall; sunshine_ = sunshine; } public int year { get { return year_; } set { } } public int month { get { return month_; } set { } } public double tmax { get { return tmax_; } set { } } public double tmin { get { return tmin_; } set { } } public int af_days { get { return af_days_; } set { } } public double rainfall { get { return rainfall_; } set { } } public double sunshine { get { return sunshine_; } set { } } public override string ToString() { return string.Format(" {0,4} {1,2} {2,5:##0.0} {3,5:##0.0} {4,3:##0} {5,5:##0.0} {6,5:##0.0}", year_, month_, tmax_, tmin_, af_days_, rainfall_, sunshine_); } } public class weather { public static int Main() { double t1,t2,t3; Console.WriteLine(DateTime.Now); Console.Write("Program starts"); var sw = Stopwatch.StartNew(); t1=sw.ElapsedMilliseconds / 1000.0; Console.WriteLine(" {0,5:0.000}",t1); // The program works with the Cwmystwyth site. // There are 618 records const int n_records = 618; double rainfall_sum = 0.0; double rainfall_average = 0.0; station_data[] x = new station_data[n_records]; read_station_data(x,n_records); Console.Write("Reading"); t2=sw.ElapsedMilliseconds / 1000.0; t3=t2-t1; Console.WriteLine(" {0,5:0.000}",t3); t1=t2; t3=t2; var select_rainfall = from t in x where t.rainfall > -98.0 select t.rainfall; rainfall_sum = select_rainfall.Sum(); rainfall_average = select_rainfall.Average(); Console.WriteLine(" Sum = {0,7:#####.0}", rainfall_sum ) ; Console.WriteLine(" Average = {0,8:####.00}", rainfall_average ) ; Console.Write("Sequential"); t2=sw.ElapsedMilliseconds / 1000.0; t3=t2-t1; Console.WriteLine(" {0,5:0.000}",t3); t1=t2; t3=t2; var select_parallel_rainfall = from t in x.AsParallel() where t.rainfall > -98.0 select t.rainfall; rainfall_sum = select_parallel_rainfall.Sum(); rainfall_average = select_parallel_rainfall.Average(); Console.WriteLine(" Sum = {0,7:#####.0}", rainfall_sum ) ; Console.WriteLine(" Average = {0,8:####.00}", rainfall_average ) ; Console.Write("Parallel "); t2=sw.ElapsedMilliseconds / 1000.0; t3=t2-t1; Console.WriteLine(" {0,5:0.000}",t3); Console.WriteLine("Program ends"); Console.WriteLine(DateTime.Now); return (0); } public static void read_station_data(station_data [] x,int n_records) { int year; int month; double tmax; double tmin; int af_days; double rainfall; double sunshine; const int h=7; int i; string line; string filename ="cwmystwythdata.txt"; if (!File.Exists(filename)) { Console.WriteLine("{0} not found.", filename); Environment.Exit(1); } StreamReader input_file=File.OpenText(filename); // skip 7 header lines // print out the header lines for (i=0;i(IEnumerable x, string heading) { Console.WriteLine(" {0} ", heading); foreach (T i in x) Console.WriteLine(i); } }