-
March 4th, 2012, 04:03 PM
#1
Vigenere table in 2d array
So I've been trying to make a vigenere 2d array my first try was using this:
Code:
char[][] table = new char [25][];
int n = 0;
int m = 0;
int i;
int diff;
for (i = 65; i == 91; i++)
{
if (n != 0)
{
i = i + n;
if (i > 90)
{
diff = i - 90;
i = 64 + diff;
}
}
table[n][m] = Convert.ToChar(i);
m++;
if (m == 26 && n < 26)
{
n++;
i = 65;
}
}
but the array still had null values after this
so I decided to try a more simple method:
Code:
char [] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char[][] table = new char [25][];
int n = 0;
int m = 0;
int i = 0 ;
int diff;
while (m != 26)
{
if (m > 0)
{
i = i + m;
if (i > 25)
{
diff = i - 25;
i = 0 + diff;
}
}
table[m][n] = alphabet[i];
n++;
i++;
if (n == 25)
{
n = 0;
m++;
i = 0;
}
}
but it still did not work.
If you can help me fix any of these it would be great!
-
March 4th, 2012, 04:32 PM
#2
Re: Vigenere table in 2d array
How about:
Code:
char [] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char[][] table = new char [alphabet.Length][];
for(int r = 0; r < alphabet.Length; r++)
{
table[r] = new char[alphabet.Length];
for(int c = 0; c < alphabet.Length; c++)
{
table[r][c] = alphabet[(r+c) % alphabet.Length];
}
}
Last edited by BioPhysEngr; March 4th, 2012 at 04:32 PM.
Reason: replaced tabs with four spaces
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
-
March 6th, 2012, 01:56 PM
#3
Re: Vigenere table in 2d array
wow, thank you!
is there any good tutorial or book I could get for more info on how to work with different dimension arrays?
because I encounter problems further in my work.
here I'm trying to cipher the entered text which is in textBox1 (text1) by using letters entered for the key (key).
Code:
string derp1 = textBox1.Text;
int k = 0;
int len = derp1.Length;
char[] text1 = new char[len];
text1 = derp1.ToCharArray(); // user text
string derp2 = textBox2.Text;
int len2 = derp2.Length;
char[] key = new char[len2];
key = derp2.ToCharArray(); // user key
char[] code = new char[len];
i = 0;
while (i != len)
{
if (table[n][m] == key[k]) //looking for the key letter in the vigenere table
{
if (table[n][m] == text1[i]) //looking for the according letter in the row where the key was found
{
code[i] = table[m][n]; //saving the ciphered text into code[i]
k++;
i++;
n = 0;
m = 0;
if (k > len2) //checking if we got to the last letter of the entered key to restart it
{
k = 0;
}
}
else
n++;
}
m++;
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|