## Enhance code in this function

General FreeBASIC programming questions.
DrPimperl
Posts: 3
Joined: Aug 13, 2019 15:11
Location: Austria

### Enhance code in this function

Hello everbody,

this is my first post in this forum, so please allow me some short introduction:
I haven't programmed in BASIC for decades (yes, I'm an oldie) but recently gave free BASIC a try, because it can produce 64-bit-code.

This function calculates the Index of coincidence for a string. The string is in lower case containing only "a" to "z" - all other charcters are removed.

Code: Select all

`Function CI(Text As String) As Double   Dim As Integer i,j,l   Dim As Double su,ah (1 To 26),tmp   For i=1 To 26   ' array ah() counts the frequency of the letters in Text      ah(i)=0     ' ah(0)=number of "a"'s, ah(1)=number of "b"'s, and so on   Next         ' as it is declared local, its not necessary to initialize it, right? meaning i could skip this entirely?      l=Len(Text)   For i=1 To l      j=Asc(Mid(Text,i,1))-96   '  j=Text[i-1]-96 would be same, but faster?         ah(j)+=1   Next   For i=1 To 26      tmp=ah(i)*(ah(i)-1)      su=su+tmp   Next   tmp=l*(l-1)   su=su/tmp   CI=su      ; return su would be same? Which is better?      End Function`

Thanks for reading and awaiting your comments.
badidea
Posts: 1614
Joined: May 24, 2007 22:10
Location: The Netherlands

### Re: Enhance code in this function

Hi, about the first question. The array is initialized by default. You can disable this by array(..) = any, see:

Code: Select all

`sub test1   dim as integer x(0 to 9)   for i as integer = 0 to 9      print x(i)   nextend subsub test2   dim as integer x(0 to 9) = any   for i as integer = 0 to 9      print x(i)   nextend subprint "test1 - default initialisation (0)"test1print "test2 - no initialisation (any)"test2`
fxm
Posts: 9309
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

### Re: Enhance code in this function

Welcome to the forum!

My version:

Code: Select all

`Function CI(Text As String) As Double   Dim As Integer l = Len(Text)   Dim As Double su   Dim As Integer ah(Asc("a") To Asc("z"))      '  by default, declared numeric arrays are cleared      For i As Integer = 0 To l - 1                '  local iterator      Dim As Integer j = Text[i]                '  local variable in the For...Next loop,   j = Text[i] is faster      If j >= Asc("a") and j <= Asc("z") Then   '  array bounds checking, otherwise bad memory access (out of array elements)         ah(j) += 1      End If   Next   For i As Integer = Asc("a") To Asc("z")      '  local iterator      su += ah(i) * (ah(i) - 1)   Next   CI = su / (l * (l-1))                        '  for this case, "return" or "CI="/"Function=" are equivalent      End Function`
DrPimperl
Posts: 3
Joined: Aug 13, 2019 15:11
Location: Austria

### Re: Enhance code in this function

Thanks for your answers.
With this little function, I can "guess" the language of a string quite good:
(provided the string is long enough)

Language Index of Coincidence
English 1.73
French 2.02
German 2.05
Italian 1.94
Portuguese 1.94
Russian 1.76
Spanish 1.94

Random 3.84 (1/26)

f.ex., I have a ciphertext:
IC (IndexofCoincidence) between
>1.7 and <2.1 = natural language monoalphabetic
else polyalphabetic or "random"
Knatterton
Posts: 165
Joined: Apr 19, 2019 19:03

### Re: Enhance code in this function

DrPimperl wrote:With this little function, I can "guess" the language of a string quite good:
(provided the string is long enough)

Language Index of Coincidence
English 1.73
French 2.02
German 2.05
Italian 1.94
Portuguese 1.94
Russian 1.76
Spanish 1.94

Random 3.84 (1/26)

Ha,ha, that's good. I think nobody guessed for what this information is good. Honestly, who suspected this?

Here is the right forum for you, all are almost as bright as you! :-)
DrPimperl
Posts: 3
Joined: Aug 13, 2019 15:11
Location: Austria

### Re: Enhance code in this function

When I started playing chess, i found out I'm not clever at all - learning from other people is an endless ocean!

Return to “General”

### Who is online

Users browsing this forum: No registered users and 1 guest