|
Microsoft Visual C# Example Application: Measures of Center |
|
|
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
Application:
Starting the Application
|
|
- Start Microsoft Visual Studio
- To create a new application, on the main menu, click File -> New
Project...
- In the middle list, click Windows Forms Application
- Change the Name to MeasuresOfCenter
- Click OK
- In the Solution Explorer, right-click Form1.cs and click Rename
- Type Measures.cs and press Enter
- Design the form as follows:
|
Control |
Name |
Text |
Label |
|
|
Value (x): |
TextBox |
|
txtValue |
|
Button |
|
btnAdd |
Add |
ListBox |
|
lbxValues |
|
Button |
|
btnClear |
Clear |
Label |
|
|
Sum: |
TextBox |
|
txtSum |
|
Label |
|
|
Mean: |
TextBox |
|
txtMean |
|
Label |
|
|
Median: |
TextBox |
|
txtMedian |
|
Label |
|
|
Midrange: |
TextBox |
|
txtMidrange |
|
Label |
|
lblCount |
Number of Values: |
Button |
|
btnClose |
Close |
|
- Double-click an unoccupied area of the form
- 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();
}
}
}
- Return to the form
- Double-click the Clear button
- 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();
}
- Return to the form
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:
The mean of a sample is expressed as follows:
|
|
Application:
Calculating the Mean
|
|
- Double-click the Add button
- 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");
}
- To execute, press F5
- Type each of the following values and click Add after each:
72604, 7592, 6314,
57086, 24885
- Close the form and return to your programming environment
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
Application:
Getting the Median
|
|
- 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");
}
- To execute, press F5
- Type each of the following values and click Add after each:
72604, 7592, 6314,
57086, 24885
- Close the form and return to your programming environment
The midrange is the mean of the minimum and the maximum
values of a collection. Obviously, the collection should be sorted.
Application:
Calculating the Midrange
|
|
- 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");
}
- Return to the form
- Double-click the Close button
- Implement its event as follows:
private void btnClose_Click(object sender, EventArgs e)
{
Close();
}
- To execute, press F5
- Type each of the following values and click Add after each:
72604, 7592, 6314,
57086, 24885
- Close the form and return to your programming environment
Application
|
|