Welcome to my blog. Here in my blog, we will be converting mathematical equations to computer programs, specifically into visual basic for applications codes, even more specifically, using only few statements like loop, if, and operational statements. Note that the codes in the blog is just one of the possible codes we can use.
Questions, suggestions and comments are highly appreciated. You may want to post any questions regarding anything related to the topic.
Well, let’s start from statistics. In statistics, we have a term called median. And by definition, median is the middle number (in a sorted list of numbers). To find the Median, place the numbers you are given in value order and find the middle number. We will be doing a program where it will generate the median of a given group of data.
First, let’s start from the usual steps we do when doing a program using visual basic for applications. Let’s declare every variable we will be using. I will be discussing the variables as we go along. For now lets start from declaring the following variables:
Dim data() As Single
Dim sizedata As Integer
The variable ‘data’ represents the array of data and ‘sizedata’ represents the size. As you can see, ‘data’ still don’t have an exact array.
Next, we will input the size of the data. We won’t have too much problem if the size is known. But not all data groups have the same size. So we will be making a computer program where the user will input the size. In order for us to do that, we will be using the code below:
Do
sizedata = InputBox(prompt:="enter the size", Default:=0)
Loop While sizedata = 0
I added a loop statement in the code to minimize bugs. Also, I have set the default value to zero (0).
Here’s what the code will show you:
After entering the data size. We will need to redimension the variable ‘data’. So we will be using the following statement:
ReDim data(1 To sizedata) As Single
Notice that we have set the dimension of the variable ‘data’. Example if ‘sizedata=3’, then we will have an array of data from 1 to 3.
data(1)
data(2)
data(3)
Next, we need to input the data in order for us to solve the median. So I’ll be using the following codes:
For indexdata = 1 To sizedata
data(indexdata) = InputBox("enter data " & indexdata)
Next
Here’s what will be shown on the screen:
Next, we need to determine the data with the lowest value. Now we will be using conditional statements.
min = data(1)
For indexdata = 1 To sizedata
If min > data(indexdata) Then
min = data(indexdata)
End If
Next
Then we will be determine the element with the highest value. Here’s the code:
max = data(1)
For indexdata = 1 To sizedata
If max < data(indexdata) Then
max = data(indexdata)
End If
Next
Next we determin the range:
range = max - min
Remember to declare the variables ‘min’, ‘max’, and ‘range’.
Dim min as single
Dim max as single
Dim range as single
Now, we will be introducing more variables. The next step that we need to do is to count the number of elements with the lowest value. And in order for us to do that, we need to enter this code:
countmin = 0
For indexdata = 1 To sizedata
If min = data(indexdata) Then
countmin = countmin + 1
End If
Next
Now we need to sort the data. Let’s start from the lowest. Then sort all the other elements using this code:
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
Now we are done sorting the data. Just don’t forget to declare all the variables we have used. Now we can get the median already. But first, we need to settle a problem. We can have the median immediately if the data size is an odd number. But what if it’s an even? We just have to determine if the size is even or odd using this code:
halfint = sizedata / 2
halfsin = sizedata / 2
The variables represent the half of the value of the size. Basically, there would be no difference between the 2 except their data type. The first will be an integer and the other will be single. By doing so, there would be a difference in the 2 if the size is an odd number. Example:
sizedata= 5
halfint=3 (5/2)
halfsin=2.5 (5/2)
If halfsin is not equal to halfint, then the size is an odd number. Now just declare the previous variables.
Dim halfint As Integer
Dim halfsin As Single
We can now determine the median. I’ve decided to use a function for that. Just follow the code below:
median = getmedian(sorteddata(), sizedata, halfint, halfsin)
And for the function:
Function getmedian(sorteddata() As Single, sizedata As Integer, halfint As Integer, halfsin As Single) As Single
'logical test: if true then sizedata is an even number else sizedata is odd
If halfint = halfsin Then
getmedian = (sorteddata(sizedata / 2) + sorteddata(sizedata / 2 + 1)) / 2
Else
getmedian = sorteddata(sizedata / 2 + 0.5)
End If
End Function
There you go. We now have a program that will automatically solve the median of a group of data. The last thing to do is to show the resuls, use the code below:
response = MsgBox("the median is " & median)
No comments:
Post a Comment