Playfair Cipher

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;
}
You are having a hard time because you are trying to deal with two separate issues at the same time.

If you draw your grid on a piece of paper and fill it out, it is easy enough for you, as a human, to fill in the grid starting in the upper-left corner and writing left to right, top to bottom.

+---+---+---+---+
| H | I | L | K |  "Hi, I like to code" 
+---+---+---+---+               ^
| E | T |...|   |
+---+---+---+---+
|   |   |   |   |
+---+---+---+---+
Example grid made smaller for illustration purposes

But in the computer, that's a lot to do in addition.

So what if you move the rows so that they are all in one line?

+---+---+---+---+  +---+---+---+---+  +---+---+---+---+
| H | I | L | K |  | E | T |...|   |  |   |   |   |   |  "Hi, I like to code" 
+---+---+---+---+  +---+---+---+---+  +---+---+---+---+               ^

That makes it a lot simpler.

There are several ways to do this in the computer. The simplest might be to just remember that a two-dimensional array is really just a fancy way of looking at a one dimensional array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

void fill( char* a )
  {
  // Hey, a[] is actually a 2D array, but I'm treating it like a 1D array...
  for (int n = 0; n < 12; n++)
    a[ n ] = 'A' + n;
  }

int main()
  {
  // Here's a[3][4] -- a 2D array.
  char a[ 3 ][ 4 ];
  fill( (char*)a );
  for (int row = 0; row < 3; row++)
    {
    for (int col = 0; col < 4; col++)
      cout << a[ row ][ col ] << " ";
    cout << "\n";
    }
  return 0;
  }
A B C D
E F G H
I J K L

If that messes with your brain too much, you can just use a one-dimensional array (or even a string) to fill things without repeats, etc.

Then use that 1D array / string / whatever to fill your grid.

Hope this helps.
Topic archived. No new replies allowed.