I am writing a program that reads a file that contains daily temperatures for a given month and reports to the UI the High, and Low temps, but i cannot figure out how to sort the temperatures. I have created a class separate from my main. Here is the code for my class
namespace Program_4
{
public class Day
{
public double _temp;
//constructor
public Day()
{
_temp = 0.0;
}
//collect the temps
public double Temp
{
get { return _temp; }
set { _temp = value; }
}
}
}
and here is the code for my main, how do i sort this and return the high and low values to the UI? I am simply using labels to report the values
namespace Program_4
{
public partial class MainForm : Form
{
//
public MainForm()
{
InitializeComponent();
}
//Declare Input File
StreamReader inputFile;
//Select File
private void btnSelectFile_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
inputFile = File.OpenText(openFileDialog1.FileName);
txbxFile.Text = openFileDialog1.FileName;
}
else
{
FileError openFileError = new FileError();
openFileError.ShowDialog();
}
Clear();
}
//Clear Labels
private void Clear()
{
lblAvg.Text = string.Empty;
lblLow.Text = string.Empty;
lblHigh.Text = string.Empty;
}
//Report Totals
private void btnReport_Click(object sender, EventArgs e)
{
//Create array to store monthly data
const int MONTHDAY = 33;
string month;
int count = 0;
double dailyTemp, totalTemp = 0.0, avgTemp;//highest, tempTrackerHigh = -500, tempTrackerLow = 500, tempHolderHigh = 0, tempHolderLow = 0;
//Create an instance of the Day class
Day[] days = new Day[MONTHDAY];
//Get the month
month = inputFile.ReadLine();
//Continue reading the file
while (!inputFile.EndOfStream)
{
if (double.TryParse(inputFile.ReadLine(), out dailyTemp));
for (int index = 0; index < days.Length; index++)
{
days[index] = new Day();
count++;
}
foreach (Day aDay in days)
{
totalTemp += dailyTemp;
}
avgTemp = totalTemp / count;
lblAvg.Text = "The average temp was " + avgTemp.ToString("f2");
Day[] sorted = days.OrderBy(c => c._temp).ToArray();
label1.Text = days[1]._temp.ToString("f0");
}
}
}
}
Comments
Post a Comment