I built a little program that calculates the average of 15 numbers or less. There are 15 text-boxes, each one’s default value is ‘0’. The program knows to get the sum of all typed numbers and divide it by numbers of text boxes that don’t return ‘0’. But if the user deletes in mistake one of the ‘0’os in one of the text boxs.. run-time error.
Originally I solved this problam by writing this “if statement” 15 times(one for each text-box):
if (t1.Text == "") { tr1 = 0; }
else
{
tr1 = Double.Parse(t1.Text);
}
this code checks if there isn’t a thing in text box(for example, named t1), if true, the program is giving the double ‘tr1′(don’t confuse with ‘t1’), the value of ‘0’, if false, the code gives the double ‘tr1’ the text of ‘t1’.
i had to write this ‘if’ 15 times. i wanted to know if i can write the same code with arrays and a for loop, and how?
here is the whole code (sorry for var names are not similar to var’s use.):
private void goyouidiot_Click(object sender, EventArgs e)
{
double tr1;
double tr2;
double tr3;
double tr4;
double tr5;
double tr6;
double tr7;
double tr8;
double tr9;
double tr10;
double tr11;
double tr12;
double tr13;
double tr14;
double tr15;
if (t1.Text == "") { tr1 = 0; }
else
{
tr1 = Double.Parse(t1.Text);
}
if (t2.Text == "") { tr2 = 0; }
else
{
tr2 = Double.Parse(t2.Text);
}
if (t3.Text == "") { tr3 = 0; }
else
{
tr3 = Double.Parse(t3.Text);
}
if (t4.Text == "") { tr4 = 0; }
else
{
tr4 = Double.Parse(t4.Text);
}
if (t5.Text == "") { tr5 = 0; }
else
{
tr5 = Double.Parse(t5.Text);
}
if (t6.Text == "") { tr6 = 0; }
else
{
tr6 = Double.Parse(t6.Text);
}
if (t7.Text == "") { tr7 = 0; }
else
{
tr7 = Double.Parse(t7.Text);
}
if (t8.Text == "") { tr8 = 0; }
else
{
tr8 = Double.Parse(t8.Text);
}
if (t9.Text == "") { tr9 = 0; }
else
{
tr9 = Double.Parse(t9.Text);
}
if (t10.Text == "") { tr10 = 0; }
else
{
tr10 = Double.Parse(t10.Text);
}
if (t11.Text == "") { tr11 = 0; }
else
{
tr11 = Double.Parse(t11.Text);
}
if (t12.Text == "") { tr12 = 0; }
else
{
tr12 = Double.Parse(t12.Text);
}
if (t13.Text == "") { tr13 = 0; }
else
{
tr13 = Double.Parse(t13.Text);
}
if (t14.Text == "") { tr14 = 0; }
else
{
tr14 = Double.Parse(t14.Text);
}
if (t15.Text == "") { tr15 = 0; }
else
{
tr15 = Double.Parse(t15.Text);
}
double[] sch = { tr1, tr2, tr3, tr4, tr5, tr6, tr7, tr8, tr9, tr10, tr11, tr12, tr13, tr14, tr15 };
double total = 0;
double sorf = 0;
for (int i = 0; i != 14; i++)
{
sorf = sorf + sch[i];
if (sch[i] > 0)
{ total++; }
}
double totalic = sorf / total;
string glass = totalic.ToString();
result.Text = ("your score: " + glass);
}
will set tr1 to the text box’s numeric value, or 0.0 if it failed to convert it for some reason. It’ll also return true if the conversion succeeded or false if it failed, but you don’t care about the return value if the default is 0.0.
Added bonus: it won’t throw an exception if someone decides to put “This is not a number.” into a text box. It’ll just see the value as 0.
To do this in an array…
UPDATE:
Note, it’s perfectly reasonable to expect to be able to compute an average of numbers that includes 0. In order to do this:
Set your TextBoxes’ default values to blank (“”), and then you’ll know how many were legitimately 0’s entered by the user and how many were blank. Divide the sum by
valid_countto get an accurate average. (But be surevalid_count > 0, or you’ll likely get a divide-by-zero exception.)