Visual C#

Microsoft Visual C# Example Application: The Standard Deviation

Considering a series of values, the standard deviation is the tendency by which the values vary from the mean. To calculate the standard deviation, you…

2 min read524 words

Introduction#

When examining a sequence of numbers, the standard deviation represents the degree to which those values differ from their average. Use the formula provided here to compute the standard deviation:

This equation consists of the following components:

FactorDescription
xEach of the values used in the series
nThe number of values in the series
xThe mean of the values
ΣThe sum of values
sThe standard deviation

Practical Exercise: Determining the Standard Deviation

  1. Start Microsoft Visual Studio
  2. To create a new application, on the main menu, click File -> New Project...
  3. In the middle list, click Windows Forms Application
  4. Change the Name to StandardDeviation1
  5. Click OK
  6. In the Solution Explorer, right-click Form1.cs and click Rename
  7. Type Exercise.cs and press Enter
  8. Design the form as follows: Control Name Text Label Value: TextBox txtValue Button btnAdd Add ListBox lbxValues Button btnClear Clear Label Standard Deviation: TextBox txtStandard Deviation Button btnClose Close
  9. Double-click an unoccupied area of the form
  10. Change the file as follows: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace MeasuresOfCenter { public partial class Measures : Form { List<double> values; public Measures() { InitializeComponent(); } private void Measures_Load(object sender, EventArgs e) { values = new List<double>(); } void ShowValues() { lbxValues.Items.Clear(); for (int i = 0; i < values.Count; i++) lbxValues.Items.Add(values[i]); } } }
  11. Return to the form
  12. Double-click the Add button
  13. Implement the event as follows: private void btnAdd_Click(object sender, EventArgs e) { // This the value that will be added to the text box double value = 0.00; double sumForMean = 0.00, bigSum = 0.00, mean = 0.00; double stdDev = 0.00; // Check that the user entered a value in the text box if (txtValue.Text.Length == 0) { MessageBox.Show("You must enter a value.", "Standard Deviation"); return; } try { // Get the value the user entered value = double.Parse(txtValue.Text); // Add it to the collection values.Add(value); // Show the values in the list box ShowValues(); txtValue.Text = ""; txtValue.Focus(); } catch (FormatException) { MessageBox.Show("The value you entered is invalid.", "Standard Deviation"); } // Calculate the total for the mean for (int i = 0; i < values.Count; i++) sumForMean += values[i]; // Calculate the mean mean = sumForMean / values.Count; // Calculate the total for the standard deviation for (int i = 0; i < values.Count; i++) bigSum += Math.Pow(values[i] - mean, 2); // Now we can calculate the standard deviation stdDev = Math.Sqrt(bigSum / (values.Count - 1)); // Display the values(standard deviationand stuff) txtCount.Text = values.Count.ToString(); txtSum.Text = sumForMean.ToString("F"); txtMean.Text = mean.ToString("F"); txtStandardDeviation.Text = stdDev.ToString("F"); }
  14. Return to the form
  15. Double-click the Clear button
  16. Implement its event as follows: private void btnClear_Click(object sender, EventArgs e) { values.Clear(); lbxValues.Items.Clear(); txtValue.Text = ""; txtCount.Text = ""; txtSum.Text = ""; txtMean.Text = ""; txtStandardDeviation.Text = ""; txtValue.Focus(); }
  17. Return to the form
  18. Double-click the Close button
  19. Implement the event as follows: private void btnClose_Click(object sender, EventArgs e) { Close(); }
  20. To execute, press F5
  21. Enter a few values and click Add after each
  22. Close the form and return to your programming environment

Application Home Copyright © 2010-2011 C# Key