The next thing we would be doing is to
create a program that will generate the mode of a given data. By definition,
mode is the number which appears most often in a set of numbers.. We would be
using the code for arranging data from the previous topic. I’ll put the code
below:
countmin = 0
For indexdata = 1 To
sizedata
If min =
data(indexdata) Then
countmin =
countmin + 1
End If
Next
For indexsort = 1 To
countmin
For indexdata = 1 To
sizedata
If min =
data(indexdata) Then
sorteddata(indexsort) = data(indexdata)
End If
Next
Next
datanum = countmin
For indexrange = 1 To
range
For indexdata = 1 To
(sizedata)
If data(indexdata)
= min + indexrange Then
sorteddata(datanum + 1) = data(indexdata)
datanum =
datanum + 1
End If
Next
Next
The code above will be be sorting the
data. Don’t forget to declare every variables. Next is to count every
repeatitions in the data. All we have to do is to input the code below:
For indexcount = 1 To sizedata
If (indexcount + 1) <= sizedata Then
If sorteddata(indexcount) = sorteddata(indexcount + 1) Then
Else
For indexdata = 1 To sizedata
If sorteddata(indexcount) =
sorteddata(indexdata) Then
count(indexcount) =
count(indexcount) + 1
End If
Next
End If
Else
For indexdata = 1 To sizedata
If sorteddata(indexcount) =
sorteddata(indexdata) Then
count(indexcount) =
count(indexcount) + 1
End If
Next
End If
Next
After getting the count of every value in
the data, we would be getting the most count. We would be using the code below:
For indexcount = 1 To sizedata
If (indexcount + 1) <= sizedata Then
If sorteddata(indexcount) = sorteddata(indexcount + 1) Then
Else
For indexdata = 1 To sizedata
If sorteddata(indexcount) =
sorteddata(indexdata) Then
count(indexcount) =
count(indexcount) + 1
End If
Next
End If
Else
For indexdata = 1 To sizedata
If sorteddata(indexcount) =
sorteddata(indexdata) Then
count(indexcount) =
count(indexcount) + 1
End If
Next
End If
Next
There would be a problem if we have
multiple modes. Because the codes given will only show only one mode. To fix
the problem, we will be inserting the code below:
If maxcount > 1 Then
countmode = 0
For indexdata = 1 To sizedata
If maxcount = count(indexdata) Then
countmode = countmode + 1
End If
Next
ReDim mode(1 To countmode) As Single
modenum = 0
For indexdata = 1 To sizedata
If maxcount = count(indexdata) Then
modenum = modenum + 1
mode(modenum) =
sorteddata(indexdata)
End If
Next
End If
Next, we would be applying the appropriate
words to our output. We just have to put the code below:
If countmode = 1 Then
isare = "is"
modesmode = "mode"
Else
isare = "are"
modesmode = "modes"
End If
The last step is to show our the results.
The code below also is flexible for bimodal and trimodal problems.
response = MsgBox("there " & isare & " "
& countmode & " " & modesmode)
For indexmode = 1 To countmode
response = MsgBox("the " & modesmode & " "
& isare & ": " & mode(indexmode))
Next
No comments:
Post a Comment