Im trying to create a 6 by 6 grid of a playfair cipher code. This is how it works...I enter a keyphrase (for example: "Hi, I like to code")
and that keyphrase is put into a 6 by 6 grid ignoring all repeated letters and numbers. Once the keyphrase has been exhausted, numbers 0-9 are added to the grid if they do not already exist; then letters (if they do not already exist) are added if the grid is still not filled up. This assignment is past due but i still want to learn how to do this. Any ideas? I get a segmentation error when I run this. It could be because fr and fc go beyond 6 by 6. There may be another way of doing this...any idea is greatly appreciated. Thanks!
// Generate a 6 by 6 grid
void createGrid(string keyPhrase, char grid[6][6])
{
// add key
int phraseCount = 0;
int fr, fc;
bool first = true;
for (int r = 0; r < 6; r++)
{
for (int c = 0; c < 6; c++)
{
grid[r][c] = keyPhrase[phraseCount];
phraseCount++;
if(first)
{
fr = r;
fc = c;
first = false;
}
grid[r][c] = '.';
}
}
bool added = false;
// Add extra numbers, then letters
for(int i = 48; i < 58; i++) // pick a number
{
for (int r = 0; r < 6; r++)
{
for (int c = 0; c < 6; c++)
{
if((char) i == grid[r][c])
{
added = true;
break;
}
}
if(!added)
{
grid[fr][fc] = (char) i;
fc++;
if (fc == 6)
{
fr++;
fc = 0;
}
}
added = false;
}
}
for(int i = 65; i < 91; i++) // pick a letter
{
for (int r = 0; r < 6; r++)
{
for (int c = 0; c < 6; c++)
{
if((char) i == grid[r][c])
{
added = true;
break;
}
}
if(!added)
{
grid[fr][fc] = (char) i;
fc++;
if (fc == 6)
{
fr++;
fc = 0;
}
}
added = false;
}
}
for (int r = 0; r < 6; r++)
{
for (int c = 0; c < 6; c++)
{
cout << grid[r][c];
}
cout << endl;
}
return;
}