# Arrays and nested for loops

Page 1 of 1

## 1 Replies - 583 Views - Last Post: 20 March 2010 - 09:17 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=163149&amp;s=bcc543271cc0c1f464ec394bcbd1b198&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Jahmbo

Reputation: 2
• Posts: 35
• Joined: 17-March 10

# Arrays and nested for loops

Posted 20 March 2010 - 07:28 AM

Vigenere codebreaking time again.

I have calculated my key length (n) using coincidence indexing.
I have broken my ciphertext into (n) strings and generated arrays of frequency distribution for each (n).

Now I have to compare the generated arrays to an array holding the standard distribution to determine what offset will provide the correct letter for key position(n) by calculating the lowest standard deviation. It's basically become a Caesar cipher cracker at this point.

Here is what I have so far.

```//standard frequency dist courtesy of the Math Explorers Club at Cornell

float std_dist[] ={7.81, 1.28, 2.93, 4.11, 13.05, 2.88, 1.39, 5.85, 6.77, 0.23,
0.42, 3.60, 2.62, 7.28, 8.21, 2.15, 0.14, 6.64, 6.46, 9.02, 2.77,
1.00, 1.49, 0.30, 1.51, 0.09};

//generated by code, included here for comparison

float my_dist[] ={4.54545, 4.82955, 2.17803, 1.98864, 2.84091, 4.73485, 4.92424, 5.39773, 2.46212, 3.125, 6.06061, 6.62879, 3.59848, 1.51515, 3.0303, 1.98864, 0.568182, 3.69318, 5.39773, 3.88258, 2.74621, 5.77652, 6.3447	3.2197	3.0303	5.49242};

//run through this for each value of key_length(n)

float lowest[26];
float dev;
int i,j,min=0;

for(i=0;i<26;i++){
lowest[i]=0;
for(j=0;j<26;j++){
dev = (std_dist[(26 + j - i) % 26]) - (my_dist[j]);
lowest[i] += (dev * dev);
if (lowest[i] < lowest[min]){min=i;}
}
}

char candidate='A'+min;

cout << candidate;

```

Needless to say it doesnt work, I have messed around with it the closest I can get is to look in lowest[] and see that the array is getting data, but I can't seem to figure out how to make sure that min is the lowest possible after the routine is finished.

Any ideas??

This post has been edited by Jahmbo: 20 March 2010 - 08:16 AM

Is This A Good Question/Topic? 0

## Replies To: Arrays and nested for loops

### #2 IngeniousHax

• |>|20-514<|{3|2

Reputation: 84
• Posts: 1,385
• Joined: 28-March 09

## Re: Arrays and nested for loops

Posted 20 March 2010 - 09:17 AM

You could always sort the values using a sorting algorithm and make sure that min is lowest in this manner.
just build a function to accept both the values and do a bubble sort or insertion sort.