C
C#2y ago
MattK

Standard deviation calculation code not working as intended

Hello! I am currently trying to code a button that will calculate the mean and standard deviation from a set of 10 data values, however, my after coding my button calculates the mean properly but for whatever reason does not calculate the standard deviation and instead puts the value of the mean in its position instead. I will leave a copy of the code below.
8 Replies
MattK
MattK2y ago
private void button1_Click(object sender, EventArgs e) { int y1; y1 = (x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10) / 10; lblMeanText.Text = y1.ToString(); double sumsquared = Math.Pow(x1 - y1, 2) + Math.Pow(x2 - y1, 2) + Math.Pow(x3 - y1, 2) + Math.Pow(x4 - y1, 2) + Math.Pow(x5 - y1, 2) + Math.Pow(x6 - y1, 2) + Math.Pow(x7 - y1, 2) + Math.Pow(x8 - y1, 2) + Math.Pow(x9 - y1, 2) + Math.Pow(x10 - y1, 2); lblStdDeviation.Text = y1.ToString(); double standarddeviation = Math.Sqrt(sumsquared / 10); }
Angius
Angius2y ago
lblStdDeviation.Text = y1.ToString(); You set the text of both lblMeanText and lblStdDeviationText to y1 variable So of course they will be the same
MattK
MattK2y ago
oh haha let me quickly fix that
Angius
Angius2y ago
Also, $code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
MattK
MattK2y ago
oh I see thank you! I Does the calculation for the standard deviation look correct?
Angius
Angius2y ago
Seems so
MattK
MattK2y ago
it seems to be working now, I appreciate the assistance! I will close the post shortly