Microsoft Visual C# Example: The z Score

 Introduction
 The z score, also called a standardized value, is a value a member of a (numeric) collection is above or below the mean. Obviously before calculating it, you must have a series of values. You must also calculate the mean of all those values. You must also have the standard deviation.

The formula to calculate the z score of an element of a sample is:

The factors in this equation are:

 Factor Description x An element of the series x The mean of the sample s The standard deviation of the sample

The formula to calculate the z score of an element of a population is:

The factors in this equation are:

 Factor Description x An element of the series µ The mean of the population σ The standard deviation of the population
 Application: 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 ZScore1
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 (x):
TextBox txtValue
ListView lvwValues FullRowSelect: True
GridLines: True
View: Details
Columns
 (Name) Text TextAlign Width colIndex # 30 colX x 40 colZScore z Score Right 120
Button btnCalculate Calculate
Label   Number of Values:
TextBox txtCount
Label   Sum of Values:
TextBox txtSum
Label   Mean:
TextBox txtMean
Label   Standard Deviation
TextBox txtStandardDeviation
Button btnClear Clear/Reset
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 ZScore1
{
public partial class Exercise : Form
{
List<double> values;

public Exercise()
{
InitializeComponent();
}

private void Exercise_Load(object sender, EventArgs e)
{
values = new List<double>();
}

void ShowValues()
{
int i = 0;
lvwValues.Items.Clear();

foreach (double value in values)
{
ListViewItem lviValue = new ListViewItem(i.ToString());
}
}
}
}```
12. Implement the event as follows:
```private void btnAdd_Click(object sender, EventArgs e)
{
double x = 0.00D;
double sum = 0.00D, mean = 0.00D;
double bigSum = 0.00, stdDev;

// Check that the user entered a value for x
if (txtValue.Text.Length == 0)
{
MessageBox.Show("You must enter the x value.",
"Statistics");
return;
}

// Get the value for x
try
{
x = double.Parse(txtValue.Text);
}
catch (FormatException)
{
MessageBox.Show("The value you entered is invalid.",
"Statistics");
}

// Add the value to the list

ShowValues();

// Calculate the sum of the values
foreach (double value in values)
sum += value;

mean = sum / 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));

txtCount.Text = values.Count.ToString();
txtSum.Text = sum.ToString();
txtMean.Text = mean.ToString();
txtStandardDeviation.Text = stdDev.ToString();

txtValue.Text = "";
txtValue.Focus();
}```
14. Double-click the Calculate button
15. Implement the event as follows:
```private void btnCalculate_Click(object sender, EventArgs e)
{
double sum = 0.00, mean = 0.00;
double bigSum = 0.00, stdDev;

lvwValues.Items.Clear();

// Example Values: To use these values, remove comment
/*

foreach (double value in values)
sum += value;
mean = sum / 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));

int j = 1;
foreach (double value in values)
{
ListViewItem lviValue = new ListViewItem(j.ToString());
j++;
}

txtCount.Text = values.Count.ToString();
txtSum.Text = sum.ToString();
txtMean.Text = mean.ToString();
txtStandardDeviation.Text = stdDev.ToString();
}```
17. Double-click the Clear/Reset button
18. Implement the event as follows:
```private void btnClear_Click(object sender, EventArgs e)
{
txtValue.Text = "";
lvwValues.Items.Clear();
txtCount.Text = "";
txtSum.Text = "";
txtMean.Text = "";
txtStandardDeviation.Text = "";

txtValue.Focus();
}```
20. Double-click the Close button
21. Implement the event as follows:
```private void btnClose_Click(object sender, EventArgs e)
{
Close();
}```
22. To execute, press F5
23. Enter values and click Add each time
24. After adding the values, click Calculate