Home

Microsoft Visual C# Example Application: Measures of Center

     

Introduction

Some of the regular operations performed in statistics are the mean, the median, the mode, and the midrange.

In statistics-related calculations, we consider two categories of values:

  • A population is the a collection of all values based on the items considered. They can be people, insects, cars, students
  • A sample is a sub-collection derived from a population

ApplicationApplication: Starting the Application

  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 MeasuresOfCenter
  5. Click OK
  6. In the Solution Explorer, right-click Form1.cs and click Rename
  7. Type Measures.cs and press Enter
  8. Design the form as follows:
     
    Measures of Center
    Control Name Text
    Label Label   Value (x):
    TextBox ListBox txtValue  
    Button Button btnAdd Add
    ListBox Label lbxValues  
    Button Button btnClear Clear
    Label Label   Sum:
    TextBox Text Box txtSum  
    Label Label   Mean:
    TextBox Text Box txtMean  
    Label Label   Median:
    TextBox TextBox txtMedian  
    Label Label   Midrange:
    TextBox ListBox txtMidrange  
    Label Label lblCount Number of Values:
    Button 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]);
    
                lblCount.Text = values.Count.ToString();
            }
        }
    }
  11. Return to the form
  12. Double-click the Clear button
  13. Implement its event as follows:
    private void btnClear_Click(object sender, EventArgs e)
    {
        values.Clear();
        lbxValues.Items.Clear();
    
        txtValue.Text = "";
        txtSum.Text = "";
        txtMean.Text = "";
        txtMedian.Text = "";
        txtMidrange.Text = "";
        txtValue.Focus();
    }
  14. Return to the form

Mean

The mean is the average gotten from a series. To calculate it, you can first add the values to get their sum, then divide that sum by the number of values in either the population or the sample. A mean can be calculated with regards to a population or a sample.

The mean of a population is expressed as follows:

Mean

The mean of a sample is expressed as follows:

Mean

ApplicationApplication: Calculating the Mean

  1. Double-click the Add button
  2. Implement its 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 sum = 0.00, mean = 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.", "Measures of Center");
            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.",
                            "Probability Distribution");
        }
    
        // Calculate the total
        for (int i = 0; i < values.Count; i++)
            sum += values[i];
        // Calculate the mean
        mean = sum / values.Count;
    
        // Display the values
        txtSum.Text = sum.ToString("F");
        txtMean.Text = mean.ToString("F");
    }
  3. To execute, press F5
  4. Type each of the following values and click Add after each: 72604, 7592, 6314, 57086, 24885
     
    Measures of Center
     
    Measures of Center
  5. Close the form and return to your programming environment

Median

The median is the value in the middle of a collection but it follows these three rules:

  • First sort the values in the collection
  • If the number of values in the collection is odd, the median is the value in the middle
  • If the number of values in the collection is even, the median is the mean of the middle two values

ApplicationApplication: Getting the Median

  1. Change the code of the Add button 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 sum = 0.00, mean = 0.00;
        double median = 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.", "Measures of Center");
            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.",
                            "Probability Distribution");
        }
    
        // Calculate the total
        for (int i = 0; i < values.Count; i++)
            sum += values[i];
        // Calculate the mean
        mean = sum / values.Count;
    
        // Get ready to evaluate the median
        // First sort the list
        values.Sort();
    
        // Find out if the list is odd
        if ((values.Count % 2) == 0)
        {
            double midIndex = values.Count / 2;
            median = (values[(int)(midIndex - 0.5)] +
            	  values[(int)(midIndex + 0.5)]) / 2;
        }
        else
            median = values[values.Count / 2];
    
        // Display the values
        txtSum.Text = sum.ToString("F");
        txtMean.Text = mean.ToString("F");
        txtMedian.Text = median.ToString("F");
    }
  2. To execute, press F5
  3. Type each of the following values and click Add after each: 72604, 7592, 6314, 57086, 24885
     
    Measures of Center Measures of Center
    Measures of Center Measures of Center
    Measures of Center
  4. Close the form and return to your programming environment

The Midrange

The midrange is the mean of the minimum and the maximum values of a collection. Obviously, the collection should be sorted.

ApplicationApplication: Calculating the Midrange

  1. Change the code of the Add button 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 sum = 0.00, mean = 0.00;
        double median = 0.00, midrange = 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.", "Measures of Center");
            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.",
                            "Probability Distribution");
        }
    
        // Calculate the total
        for (int i = 0; i < values.Count; i++)
            sum += values[i];
        // Calculate the mean
        mean = sum / values.Count;
    
        // Get ready to evaluate the median
        // First sort the list
        values.Sort();
    
        // Find out if the list is odd
        if ((values.Count % 2) == 0)
        {
            double midIndex = values.Count / 2;
            median = (values[(int)(midIndex - 0.5)] +
            	values[(int)(midIndex + 0.5)]) / 2;
        }
        else
            median = values[values.Count / 2];
    
        // Calculate the midrange as the mean between the minimum and maximum
        midrange = (values[0] + values[values.Count-1]) / 2;
    
        // Display the values
        txtSum.Text = sum.ToString("F");
        txtMean.Text = mean.ToString("F");
        txtMedian.Text = median.ToString("F");
        txtMidrange.Text = midrange.ToString("F");
    }
  2. Return to the form
  3. Double-click the Close button
  4. Implement its event as follows:
    private void btnClose_Click(object sender, EventArgs e)
    {
        Close();
    }
  5. To execute, press F5
  6. Type each of the following values and click Add after each: 72604, 7592, 6314, 57086, 24885
     

    Measures of Center
  7. Close the form and return to your programming environment

Application

 

Home Copyright © 2010-2015 FunctionX