Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am making a decoder for a substitution cipher using frequency analysis (the num of times a letter pops up in a sentence, for example, 'E' is the most used letter, followed by 'T'). Anyways, I've got some of it working but I'm stuck on one part. I need to generate a new key based on the frequency analysis of the encoded sample.
Here's what I have:
//This method creates a new array of characters beginning with the most used letters from the sample.
public static char[] MakeAlphaKey(char[]Alphabet, int[]FrequencyKey)
{
char[] AlphaKey = new char[26];
for(int a = 0; a < 26; a++)
{
AlphaKey[a] = Alphabet[Max(FrequencyKey)];
}
return AlphaKey;
}//Method to find most common letter
//I think the problem is somewhere in this method with maxindex, but I cant figure out what.
//FrequencyKey is a list of ints represented in Alphabetical order, so for example, FrequencyKey[0] = 5, which is the number of 'A's in the encoded text. FrequencyKey[1] is the number of B's.public static int Max(int FrequencyKey[])
{
int max = 0;
int maxindex = 0;for (int i = 0; i < 26; i++)
{
if(FrequencyKey[i] > max)
{
max = FrequencyKey[i];
maxindex = i;
FrequencyKey[i] = -1;
}
//Maybe add something here???
}
return maxindex;
}Any help would be appreciated. I know how to do the rest of the program already. It's just this one part is really frustrating.

Why do u set FrequencyKey[i] to -1 when u find a max candidate??
If it is so that it doesnt get picked up as the Max freq next time, then don't set it to -1 inside the loop, move it to just b4 u return the maxindex value.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |