Written some cool source code? Upload it to Programmer's Heaven.

View Standard Deviation\grey.frm

Standard Deviation 1.0

Submitted By: graceson
Rating: starstarstarhalf star (Rate It)


VERSION 5.00
Begin VB.Form frmStandardDeviation
   BorderStyle     =   1  'Fixed Single
   Caption         =   "Form2"
   ClientHeight    =   6465
   ClientLeft      =   5160
   ClientTop       =   3105
   ClientWidth     =   4935
   LinkTopic       =   "Form2"
   MaxButton       =   0   'False
   MinButton       =   0   'False
   ScaleHeight     =   6465
   ScaleWidth      =   4935
   Begin VB.CommandButton CmdClose
      Caption         =   "Close"
      Height          =   375
      Left            =   3600
      TabIndex        =   9
      Top             =   6000
      Width           =   1215
   End
   Begin VB.TextBox TxtMode
      Height          =   375
      Left            =   360
      TabIndex        =   4
      Top             =   4440
      Width           =   1815
   End
   Begin VB.TextBox TxtMedian
      Height          =   375
      Left            =   360
      TabIndex        =   3
      Top             =   3600
      Width           =   4335
   End
   Begin VB.TextBox TxtMean
      Height          =   375
      Left            =   360
      TabIndex        =   2
      Top             =   5280
      Width           =   4335
   End
   Begin VB.Frame Frame1
      Caption         =   "Sorted List"
      Height          =   2535
      Left            =   120
      TabIndex        =   0
      Top             =   120
      Width           =   4695
      Begin VB.ListBox LstStorageSorted
         Height          =   2010
         Left            =   120
         TabIndex        =   1
         Top             =   360
         Width           =   4455
      End
   End
   Begin VB.Frame Frame2
      Caption         =   "Standard Deviations"
      Height          =   3015
      Left            =   120
      TabIndex        =   5
      Top             =   2880
      Width           =   4695
      Begin VB.TextBox txtModeFrequency
         Height          =   405
         Left            =   3000
         TabIndex        =   10
         Top             =   1560
         Width           =   1575
      End
      Begin VB.Label Label4
         Caption         =   "Freqency :"
         Height          =   255
         Left            =   2160
         TabIndex        =   11
         Top             =   1200
         Width           =   975
      End
      Begin VB.Label Label3
         Caption         =   "Mean :"
         Height          =   255
         Left            =   120
         TabIndex        =   8
         Top             =   2040
         Width           =   615
      End
      Begin VB.Label Label2
         Caption         =   "Mode :"
         Height          =   255
         Left            =   120
         TabIndex        =   7
         Top             =   1200
         Width           =   495
      End
      Begin VB.Label Label1
         Caption         =   "Median :"
         Height          =   255
         Left            =   120
         TabIndex        =   6
         Top             =   360
         Width           =   735
      End
   End
End
Attribute VB_Name = "frmStandardDeviation"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'*************************
'* Global Variables Here *
'*************************

Dim arrStorage() As Double
'*******************************
'* User defined functions here *
'*******************************

'Copy to Array (ArrStorage)
'Copies the value of listbox(LstStorageSorted) to array(arrStorage)
Private Sub CopyToArray()
    Dim i As Integer
    ReDim arrStorage(LstStorageSorted.ListCount)
    For i = 0 To LstStorageSorted.ListCount - 1 Step 1
        arrStorage(i) = Val(LstStorageSorted.List(i))
    Next
End Sub

'Here comes the most important functions
'Calculates The "Mean"
Private Function CalculateMean() As Double
    Dim sum As Double
    Dim i
    sum = 0
    For i = 0 To LstStorageSorted.ListCount - 1 Step 1
        sum = sum + arrStorage(i)
    Next
    CalculateMean = sum / LstStorageSorted.ListCount
End Function
'Calculate Median
Private Function CalculateMedian() As Double
    Dim n As Integer
    n = Abs(LstStorageSorted.ListCount / 2)
    Print n
    If LstStorageSorted.ListCount Mod 2 = 1 Then
        CalculateMedian = arrStorage(n)
    Else
        If arrStorage(n - 1) = arrStorage(n) Then
            CalculateMedian = arrStorage(n - 1) + 0.5
        Else
            CalculateMedian = (arrStorage(n - 1) + arrStorage(n)) / 2
        End If
    End If
End Function

'Calculate Mode
Private Function CalculateMode(ByRef xxx) As Double
    Dim FrequencyTable(), temp, temp2 As Double
    Dim ElementCount, Frequency As Integer
    Dim i, j As Integer
   
    'count how many kind of numbers are there
    ElementCount = 0
    temp = arrStorage(0)
    For i = 1 To LstStorageSorted.ListCount Step 1
        If temp <> arrStorage(i) Then
            temp = arrStorage(i)
            ElementCount = ElementCount + 1
        End If
    Next

    'redim FrequencyTable to accomodate the elements
    ReDim FrequencyTable(ElementCount, 2)
   
    'store each element and frequency to the FrequencyTable
    Frequency = 1
    temp = arrStorage(0)
    j = 0
    For i = 1 To LstStorageSorted.ListCount Step 1
        If temp <> arrStorage(i) Then
            FrequencyTable(j, 0) = temp       'store the number
            FrequencyTable(j, 1) = Frequency  'store its frequency
            temp = arrStorage(i)
            j = j + 1
            Frequency = 0
        End If
        Frequency = Frequency + 1
    Next

    'sort the frequency in ascending order
    For i = 0 To ElementCount - 1 Step 1
        For j = 0 To ElementCount - 1 Step 1
            If FrequencyTable(i, 1) < FrequencyTable(j, 1) Then
                temp = FrequencyTable(i, 1)
                temp2 = FrequencyTable(i, 0)
                FrequencyTable(i, 1) = FrequencyTable(j, 1)
                FrequencyTable(i, 0) = FrequencyTable(j, 0)
                FrequencyTable(j, 1) = temp
                FrequencyTable(j, 0) = temp2
            End If
        Next
    Next
   
    If FrequencyTable(ElementCount - 1, 1) = FrequencyTable(ElementCount - 2, 1) Then
        CalculateMode = 0
        xxx = 0
    Else
        CalculateMode = FrequencyTable(ElementCount - 1, 0)
        xxx = FrequencyTable(ElementCount - 1, 1)
    End If
End Function

'********************************************
'* Function definition of the objects added *
'********************************************
'Forms
Private Sub Form_Load()
    Dim i As Integer
    If frmMain.LstStorage.ListCount > 0 Then
        For i = 0 To frmMain.LstStorage.ListCount - 1 Step 1
            LstStorageSorted.AddItem frmMain.LstStorage.List(i)
        Next
        CopyToArray
        TxtMean = CalculateMean
        TxtMedian = CalculateMedian
        TxtMode = CalculateMode(i)
        txtModeFrequency = i
    Else
        TxtMean = 0
        TxtMode = 0
        TxtMedian = 0
        txtModeFrequency = 0
    End If
End Sub
' Command Buttons
Private Sub CmdClose_Click()
    Unload Me
    frmMain.Show
End Sub

corner
© 1996-2008. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Publisher: Lars Hagelin.
bootstrapLabs Logo A bootstrapLabs project.