Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class AnswerNumericalStatistic
- 'Clears the AnswerTextBox when the form is loaded'
- Private Sub Me_Load(sender As Object, e As EventArgs) Handles Me.Load
- AnswerTextBox.Text = ""
- End Sub
- Private Sub LargestButton_Click(sender As Object, e As EventArgs) Handles LargestButton.Click
- AnswerTextBox.Text = ""
- Dim max As Double = NumericalStatistic.ValueTextBox.Lines(0)
- 'Compare and find the largest number'
- For i As Integer = 1 To NumericalStatistic.ValueTextBox.Lines.Length - 1
- If NumericalStatistic.ValueTextBox.Lines(i) > max Then
- max = NumericalStatistic.ValueTextBox.Lines(i)
- End If
- Next
- AnswerTextBox.Text = max
- End Sub
- Private Sub SmallestButton_Click(sender As Object, e As EventArgs) Handles SmallestButton.Click
- AnswerTextBox.Text = ""
- Dim min As Double = NumericalStatistic.ValueTextBox.Lines(0)
- 'Compare and find the smallest number'
- For i As Integer = 1 To NumericalStatistic.ValueTextBox.Lines.Length - 1
- If NumericalStatistic.ValueTextBox.Lines(i) < min Then
- min = NumericalStatistic.ValueTextBox.Lines(i)
- End If
- Next
- AnswerTextBox.Text = min
- End Sub
- Private Sub MeanButton_Click(sender As Object, e As EventArgs) Handles MeanButton.Click
- AnswerTextBox.Text = ""
- AnswerTextBox.Text = CalculateMean()
- End Sub
- Private Sub ModeButton_Click(sender As Object, e As EventArgs) Handles ModeButton.Click
- AnswerTextBox.Text = ""
- Dim modeArray(NumericalStatistic.ValueTextBox.Lines.Length, 1) As Double
- Dim highestFrequency As Double = 0
- Dim count As Integer = 0
- 'Filter out repeating numbers (make array without repeating no.)'
- For Each line As String In NumericalStatistic.ValueTextBox.Lines
- Dim found As Boolean = False
- 'check if there is any numbers in modeArray that is the same as the line'
- For i As Integer = 0 To NumericalStatistic.ValueTextBox.Lines.Length - 1
- 'the modeArray right index 0 is the Values'
- If modeArray(i, 0) = line Then
- found = True
- End If
- Next
- If Not found Then
- modeArray(count, 0) = line
- count += 1
- End If
- Next
- 'Count frequency of each number in the modeArray'
- For Each line As String In NumericalStatistic.ValueTextBox.Lines
- For i As Integer = 0 To NumericalStatistic.ValueTextBox.Lines.Length - 1
- If line = modeArray(i, 0) Then
- 'the modeArray right index 1 is the Frequency'
- modeArray(i, 1) += 1
- End If
- Next
- Next
- 'Find mode, similar to finding the largest'
- Dim modes(count) As Double
- Dim count2 As Integer = 0
- For i As Integer = 0 To count
- If modeArray(i, 1) > highestFrequency Then
- highestFrequency = modeArray(i, 1)
- modes(0) = modeArray(i, 0)
- End If
- Next
- 'Check if got multiple mode'
- For i As Integer = 0 To count
- If modeArray(i, 1) = highestFrequency AndAlso modeArray(i, 0) <> modes(0) Then
- count2 += 1
- modes(count2) = modeArray(i, 0)
- End If
- Next
- AnswerTextBox.Text = modes(0)
- 'If there is more than 1 mode'
- If count2 > 0 Then
- For i As Integer = 1 To count2
- AnswerTextBox.Text = AnswerTextBox.Text & "," & modes(i)
- Next
- End If
- End Sub
- Private Sub MedianButton_Click(sender As Object, e As EventArgs) Handles MedianButton.Click
- AnswerTextBox.Text = ""
- Dim sortedInput() As Double = SortArray()
- Dim position As Integer
- Dim num1, num2, median As Double
- 'Even number of input'
- If sortedInput.Length Mod 2 = 0 Then
- 'Find position of number closest to middle'
- position = sortedInput.Length / 2
- num1 = sortedInput(position)
- num2 = sortedInput(position - 1)
- median = (num1 + num2) / 2
- 'Odd'
- Else
- 'Find position of median'
- position = (sortedInput.Length - 1) / 2
- median = sortedInput(position)
- End If
- AnswerTextBox.Text = median
- End Sub
- Private Sub FirstQuartileButton_Click(sender As Object, e As EventArgs) Handles FirstQuartileButton.Click
- AnswerTextBox.Text = ""
- AnswerTextBox.Text = CalculateFirstQuartile()
- End Sub
- Private Sub ThirdQuartileButton_Click(sender As Object, e As EventArgs) Handles ThirdQuartileButton.Click
- AnswerTextBox.Text = ""
- AnswerTextBox.Text = CalculateThirdQuartile()
- End Sub
- Private Sub InterquartileButton_Click(sender As Object, e As EventArgs) Handles InterquartileButton.Click
- AnswerTextBox.Text = ""
- 'Calculate difference between 1st and 3rd Quartile'
- Dim interQRange As Double = CalculateThirdQuartile() - CalculateFirstQuartile()
- AnswerTextBox.Text = interQRange
- End Sub
- Private Sub VarianceButton_Click(sender As Object, e As EventArgs) Handles VarianceButton.Click
- AnswerTextBox.Text = ""
- AnswerTextBox.Text = CalculateVariance()
- End Sub
- Private Sub StandardDeviationButton_Click(sender As Object, e As EventArgs) Handles StandardDeviationButton.Click
- AnswerTextBox.Text = ""
- 'Square root variance to get Standard Deviation'
- Dim standardDeviation As Double = Math.Sqrt(CalculateVariance())
- AnswerTextBox.Text = stDeviation
- End Sub
- Function CalculateMean() As Double
- 'mean = average'
- Dim sum As Double = 0.0
- Dim mean As Double = 0.0
- For Each number As Double In NumericalStatistic.ValueTextBox.Lines
- sum += number
- Next
- mean = sum / (NumericalStatistic.ValueTextBox.Lines.Length)
- Return mean
- End Function
- Function CalculateFirstQuartile() As Double
- Dim sortedInput() As Double = SortArray()
- Dim position As Integer
- Dim num1, num2, firstQ As Double
- 'Even number of input'
- If sortedInput.Length Mod 2 = 0 Then
- 'Total number divided by 2 is even'
- If ((sortedInput.Length) Mod 4 = 0) Then
- position = sortedInput.Length / 4
- num1 = sortedInput(position)
- num2 = sortedInput(position - 1)
- firstQ = (num1 + num2) / 2
- 'Total number divided by 2 is odd'
- Else
- position = ((sortedInput.Length) / 4)
- firstQ = sortedInput(position)
- End If
- 'Odd number of input'
- Else
- 'Total number-1 divided by 2 is even'
- If (sortedInput.Length - 1) Mod 4 = 0 Then
- position = (sortedInput.Length - 1) / 4
- num1 = sortedInput(position)
- num2 = sortedInput(position - 1)
- firstQ = (num1 + num2) / 2
- 'Total number-1 divided by 2 is odd'
- Else
- position = ((sortedInput.Length + 1) / 4) - 1
- firstQ = sortedInput(position)
- End If
- End If
- Return firstQ
- End Function
- Function CalculateThirdQuartile() As Double
- Dim sortedInput() As Double = SortArray()
- Dim position As Integer
- Dim num1, num2, thirdQ As Double
- 'Even number of input'
- If sortedInput.Length Mod 2 = 0 Then
- 'Total number divided by 2 is even'
- If ((sortedInput.Length) Mod 4 = 0) Then
- position = 3 * sortedInput.Length / 4
- num1 = sortedInput(position)
- num2 = sortedInput(position - 1)
- thirdQ = (num1 + num2) / 2
- 'Total number divided by 2 is odd'
- Else
- position = (3 * (sortedInput.Length) / 4)
- thirdQ = sortedInput(position)
- End If
- 'Odd number of input'
- Else
- 'Total number-1 divided by 2 is even'
- If (sortedInput.Length - 1) Mod 4 = 0 Then
- position = 3 * (sortedInput.Length - 1) / 4
- num1 = sortedInput(position)
- num2 = sortedInput(position + 1)
- thirdQ = (num1 + num2) / 2
- 'Total number-1 divided by 2 is odd'
- Else
- position = (3 * (sortedInput.Length) / 4)
- thirdQ = sortedInput(position)
- End If
- End If
- Return thirdQ
- End Function
- Function CalculateVariance() As Double
- Dim sumOfDeviation As Double = 0.0
- Dim variance As Double = 0.0
- 'sum of (each number - mean)^2'
- For i As Integer = 0 To NumericalStatistic.ValueTextBox.Lines.Length - 1
- sumOfDeviation += (NumericalStatistic.ValueTextBox.Lines(i) - CalculateMean()) ^ 2
- Next
- variance = sumOfDeviation / NumericalStatistic.ValueTextBox.Lines.Length
- Return variance
- End Function
- Function SortArray() As Array
- Dim sortedArray(NumericalStatistic.ValueTextBox.Lines.Length - 1) As Double
- Dim num As Double
- 'put the numbers into a Double data type array'
- For i As Integer = 0 To NumericalStatistic.ValueTextBox.Lines.Length - 1
- num = NumericalStatistic.ValueTextBox.Lines(i)
- sortedArray(i) = num
- Next
- Array.Sort(sortedArray)
- Return sortedArray
- End Function
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement