Advertisement
Guest User

VB Asgn 1.2

a guest
Oct 18th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 9.77 KB | None | 0 0
  1. Public Class AnswerNumericalStatistic
  2.     'Clears the AnswerTextBox when the form is loaded'
  3.     Private Sub Me_Load(sender As Object, e As EventArgs) Handles Me.Load
  4.         AnswerTextBox.Text = ""
  5.     End Sub
  6.  
  7.     Private Sub LargestButton_Click(sender As Object, e As EventArgs) Handles LargestButton.Click
  8.         AnswerTextBox.Text = ""
  9.         Dim max As Double = NumericalStatistic.ValueTextBox.Lines(0)
  10.  
  11.         'Compare and find the largest number'
  12.         For i As Integer = 1 To NumericalStatistic.ValueTextBox.Lines.Length - 1
  13.             If NumericalStatistic.ValueTextBox.Lines(i) > max Then
  14.                 max = NumericalStatistic.ValueTextBox.Lines(i)
  15.             End If
  16.         Next
  17.  
  18.         AnswerTextBox.Text = max
  19.     End Sub
  20.  
  21.     Private Sub SmallestButton_Click(sender As Object, e As EventArgs) Handles SmallestButton.Click
  22.         AnswerTextBox.Text = ""
  23.         Dim min As Double = NumericalStatistic.ValueTextBox.Lines(0)
  24.  
  25.         'Compare and find the smallest number'
  26.         For i As Integer = 1 To NumericalStatistic.ValueTextBox.Lines.Length - 1
  27.             If NumericalStatistic.ValueTextBox.Lines(i) < min Then
  28.                 min = NumericalStatistic.ValueTextBox.Lines(i)
  29.             End If
  30.         Next
  31.  
  32.         AnswerTextBox.Text = min
  33.     End Sub
  34.  
  35.     Private Sub MeanButton_Click(sender As Object, e As EventArgs) Handles MeanButton.Click
  36.         AnswerTextBox.Text = ""
  37.         AnswerTextBox.Text = CalculateMean()
  38.     End Sub
  39.  
  40.     Private Sub ModeButton_Click(sender As Object, e As EventArgs) Handles ModeButton.Click
  41.         AnswerTextBox.Text = ""
  42.  
  43.         Dim modeArray(NumericalStatistic.ValueTextBox.Lines.Length, 1) As Double
  44.         Dim highestFrequency As Double = 0
  45.         Dim count As Integer = 0
  46.  
  47.         'Filter out repeating numbers (make array without repeating no.)'
  48.         For Each line As String In NumericalStatistic.ValueTextBox.Lines
  49.             Dim found As Boolean = False
  50.  
  51.             'check if there is any numbers in modeArray that is the same as the line'
  52.             For i As Integer = 0 To NumericalStatistic.ValueTextBox.Lines.Length - 1
  53.                 'the modeArray right index 0 is the Values'
  54.                 If modeArray(i, 0) = line Then
  55.                     found = True
  56.                 End If
  57.             Next
  58.  
  59.             If Not found Then
  60.                 modeArray(count, 0) = line
  61.                 count += 1
  62.             End If
  63.         Next
  64.  
  65.         'Count frequency of each number in the modeArray'
  66.         For Each line As String In NumericalStatistic.ValueTextBox.Lines
  67.             For i As Integer = 0 To NumericalStatistic.ValueTextBox.Lines.Length - 1
  68.                 If line = modeArray(i, 0) Then
  69.                     'the modeArray right index 1 is the Frequency'
  70.                     modeArray(i, 1) += 1
  71.                 End If
  72.             Next
  73.         Next
  74.  
  75.         'Find mode, similar to finding the largest'
  76.         Dim modes(count) As Double
  77.         Dim count2 As Integer = 0
  78.         For i As Integer = 0 To count
  79.             If modeArray(i, 1) > highestFrequency Then
  80.                 highestFrequency = modeArray(i, 1)
  81.                 modes(0) = modeArray(i, 0)
  82.             End If
  83.         Next
  84.  
  85.         'Check if got multiple mode'
  86.         For i As Integer = 0 To count
  87.             If modeArray(i, 1) = highestFrequency AndAlso modeArray(i, 0) <> modes(0) Then
  88.                 count2 += 1
  89.                 modes(count2) = modeArray(i, 0)
  90.             End If
  91.         Next
  92.  
  93.         AnswerTextBox.Text = modes(0)
  94.         'If there is more than 1 mode'
  95.         If count2 > 0 Then
  96.             For i As Integer = 1 To count2
  97.                 AnswerTextBox.Text = AnswerTextBox.Text & "," & modes(i)
  98.             Next
  99.         End If
  100.     End Sub
  101.  
  102.     Private Sub MedianButton_Click(sender As Object, e As EventArgs) Handles MedianButton.Click
  103.         AnswerTextBox.Text = ""
  104.         Dim sortedInput() As Double = SortArray()
  105.         Dim position As Integer
  106.         Dim num1, num2, median As Double
  107.  
  108.         'Even number of input'
  109.         If sortedInput.Length Mod 2 = 0 Then
  110.             'Find position of number closest to middle'
  111.             position = sortedInput.Length / 2
  112.             num1 = sortedInput(position)
  113.             num2 = sortedInput(position - 1)
  114.             median = (num1 + num2) / 2
  115.         'Odd'
  116.         Else
  117.             'Find position of median'
  118.             position = (sortedInput.Length - 1) / 2
  119.             median = sortedInput(position)
  120.         End If
  121.  
  122.         AnswerTextBox.Text = median
  123.     End Sub
  124.  
  125.     Private Sub FirstQuartileButton_Click(sender As Object, e As EventArgs) Handles FirstQuartileButton.Click
  126.         AnswerTextBox.Text = ""
  127.         AnswerTextBox.Text = CalculateFirstQuartile()
  128.     End Sub
  129.  
  130.     Private Sub ThirdQuartileButton_Click(sender As Object, e As EventArgs) Handles ThirdQuartileButton.Click
  131.         AnswerTextBox.Text = ""
  132.         AnswerTextBox.Text = CalculateThirdQuartile()
  133.     End Sub
  134.  
  135.     Private Sub InterquartileButton_Click(sender As Object, e As EventArgs) Handles InterquartileButton.Click
  136.         AnswerTextBox.Text = ""
  137.         'Calculate difference between 1st and 3rd Quartile'
  138.         Dim interQRange As Double = CalculateThirdQuartile() - CalculateFirstQuartile()
  139.         AnswerTextBox.Text = interQRange
  140.     End Sub
  141.  
  142.     Private Sub VarianceButton_Click(sender As Object, e As EventArgs) Handles VarianceButton.Click
  143.         AnswerTextBox.Text = ""
  144.         AnswerTextBox.Text = CalculateVariance()
  145.     End Sub
  146.  
  147.     Private Sub StandardDeviationButton_Click(sender As Object, e As EventArgs) Handles StandardDeviationButton.Click
  148.         AnswerTextBox.Text = ""
  149.         'Square root variance to get Standard Deviation'
  150.         Dim standardDeviation As Double = Math.Sqrt(CalculateVariance())
  151.         AnswerTextBox.Text = stDeviation
  152.     End Sub
  153.  
  154.     Function CalculateMean() As Double
  155.         'mean = average'
  156.         Dim sum As Double = 0.0
  157.         Dim mean As Double = 0.0
  158.  
  159.         For Each number As Double In NumericalStatistic.ValueTextBox.Lines
  160.             sum += number
  161.         Next
  162.  
  163.         mean = sum / (NumericalStatistic.ValueTextBox.Lines.Length)
  164.  
  165.         Return mean
  166.     End Function
  167.  
  168.     Function CalculateFirstQuartile() As Double
  169.         Dim sortedInput() As Double = SortArray()
  170.         Dim position As Integer
  171.         Dim num1, num2, firstQ As Double
  172.  
  173.         'Even number of input'
  174.         If sortedInput.Length Mod 2 = 0 Then
  175.             'Total number divided by 2 is even'
  176.             If ((sortedInput.Length) Mod 4 = 0) Then
  177.                 position = sortedInput.Length / 4
  178.                 num1 = sortedInput(position)
  179.                 num2 = sortedInput(position - 1)
  180.                 firstQ = (num1 + num2) / 2
  181.             'Total number divided by 2 is odd'
  182.             Else
  183.                 position = ((sortedInput.Length) / 4)
  184.                 firstQ = sortedInput(position)
  185.             End If
  186.         'Odd number of input'
  187.         Else
  188.             'Total number-1 divided by 2 is even'
  189.             If (sortedInput.Length - 1) Mod 4 = 0 Then
  190.                 position = (sortedInput.Length - 1) / 4
  191.                 num1 = sortedInput(position)
  192.                 num2 = sortedInput(position - 1)
  193.                 firstQ = (num1 + num2) / 2
  194.             'Total number-1 divided by 2 is odd'
  195.             Else
  196.                 position = ((sortedInput.Length + 1) / 4) - 1
  197.                 firstQ = sortedInput(position)
  198.             End If
  199.         End If
  200.  
  201.         Return firstQ
  202.     End Function
  203.  
  204.     Function CalculateThirdQuartile() As Double
  205.         Dim sortedInput() As Double = SortArray()
  206.         Dim position As Integer
  207.         Dim num1, num2, thirdQ As Double
  208.  
  209.         'Even number of input'
  210.         If sortedInput.Length Mod 2 = 0 Then
  211.             'Total number divided by 2 is even'
  212.             If ((sortedInput.Length) Mod 4 = 0) Then
  213.                 position = 3 * sortedInput.Length / 4
  214.                 num1 = sortedInput(position)
  215.                 num2 = sortedInput(position - 1)
  216.                 thirdQ = (num1 + num2) / 2
  217.             'Total number divided by 2 is odd'
  218.             Else
  219.                 position = (3 * (sortedInput.Length) / 4)
  220.                 thirdQ = sortedInput(position)
  221.             End If
  222.         'Odd number of input'
  223.         Else
  224.             'Total number-1 divided by 2 is even'
  225.             If (sortedInput.Length - 1) Mod 4 = 0 Then
  226.                 position = 3 * (sortedInput.Length - 1) / 4
  227.                 num1 = sortedInput(position)
  228.                 num2 = sortedInput(position + 1)
  229.                 thirdQ = (num1 + num2) / 2
  230.             'Total number-1 divided by 2 is odd'
  231.             Else
  232.                 position = (3 * (sortedInput.Length) / 4)
  233.                 thirdQ = sortedInput(position)
  234.             End If
  235.         End If
  236.  
  237.         Return thirdQ
  238.     End Function
  239.  
  240.     Function CalculateVariance() As Double
  241.         Dim sumOfDeviation As Double = 0.0
  242.         Dim variance As Double = 0.0
  243.  
  244.         'sum of (each number - mean)^2'
  245.         For i As Integer = 0 To NumericalStatistic.ValueTextBox.Lines.Length - 1
  246.             sumOfDeviation += (NumericalStatistic.ValueTextBox.Lines(i) - CalculateMean()) ^ 2
  247.         Next
  248.  
  249.         variance = sumOfDeviation / NumericalStatistic.ValueTextBox.Lines.Length
  250.  
  251.         Return variance
  252.     End Function
  253.  
  254.     Function SortArray() As Array
  255.         Dim sortedArray(NumericalStatistic.ValueTextBox.Lines.Length - 1) As Double
  256.         Dim num As Double
  257.         'put the numbers into a Double data type array'
  258.         For i As Integer = 0 To NumericalStatistic.ValueTextBox.Lines.Length - 1
  259.             num = NumericalStatistic.ValueTextBox.Lines(i)
  260.             sortedArray(i) = num
  261.         Next
  262.        
  263.         Array.Sort(sortedArray)
  264.  
  265.         Return sortedArray
  266.     End Function
  267. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement