I am trying to add up all the Shannon entropy of all the alphabetical characters in a word document. Instead of it adding the characters it gives me what I put for character(27) as an answer.
Dim characters(1 To 27) As Double
Dim x As Integer 'for looping
Dim tot As Double 'The final value
characters(1) = 0.1859 'Space
characters(2) = 0.0856 'A
characters(3) = 0.0139 'B
characters(4) = 0.0279 'C
characters(5) = 0.0378 'D
characters(6) = 0.1304 'E
characters(7) = 0.0289 'F
characters(8) = 0.0199 'G
characters(9) = 0.0528 'H
characters(10) = 0.0627 'I
characters(11) = 0.0013 'J
characters(12) = 0.0042 'K
characters(13) = 0.0339 'L
characters(14) = 0.0249 'M
characters(15) = 0.0707 'N
characters(16) = 0.0797 'O
characters(17) = 0.0199 'P
characters(18) = 0.0012 'Q
characters(19) = 0.0677 'R
characters(20) = 0.0607 'S
characters(21) = 0.1045 'T
characters(22) = 0.0249 'U
characters(23) = 0.0092 'V
characters(24) = 0.0149 'W
characters(25) = 0.0017 'X
characters(26) = 0.0199 'Y
characters(27) = 0.0008 'Z
For x = 1 To 27
tot = tol + characters(x)
Next
MsgBox "The Shannon entropy of the alphabetic characters in this document is " & tot
What I am getting
Today was a good day
The Shannon entropy of the characters in this document is 0.0008
What I am trying to get
Today was a good day
The Shannon entropy of the characters in this document is 1.2798542258337
I don't know if you have noticed that you wrote this:
...while you might have wanted to write this:
In fact, what you want is that tot is iteratively summing to itself a new value. But if you write
...what happens is that
tol
is always = 0 (because it doesn't preserve it's value and gets its 0 value by default) and sotot
will be obviously equal to 0 + the last element because it does not keep its changes either. It's a typo, just changetot = tol + characters(x)
withtot = tot + characters(x)
and the code will work.