Join for FREE | Take the Tour Lost Password?
Shop deviantART for the
holidays and save BIG!
Click here! :holly:
[x]

deviantART

:omfg:
 
©2007-2009 ~Graven
:icongraven:

Artist's Comments

This code stores all the different Caesar cipher's in a two dimensional array called table.

This actually took me a surprisingly long time, in particular line 17, which also happens to be the favourite part of the code for me.

This will eventually be a vigenereencoder/decoder.

There were about 4 redrafts of this, crazy I know. Because at first I was probably trying to do this the most difficult way possible.

I knew I had to store the alphabet in an array (that's the easy part) and so I did that first.

I also knew I needed to store the different "shifts" in an array. 26 different shifts, all 26 characters long.

The first 2 for loops basically loop through the [26][26] array.

In my first few versions I had excess variables and realised I Could get away with just using Cols and n.

Cols represents the Column that is currently active, and therefore can be used to represent the number of places each character needs to be shifted. I.e, Column 4, means that each character is shifted 4 characters to the left.

N represents the numerical value of the position of each character within the array. E.g, a = 0, b = 1, c = 2 and so on.

So after looping through each element in the array, the next part I coded was getting the new "shifted" character.

I did this by getting the numerical position of the current character, 'n', and adding the number of places to shift to it 'cols'. So the new shifted character = n + Cols. I then used the following to store the new character in the multidimensional array:

Table[Cols][n] = Alphabet[Cols + n];

This worked great until I realised that some combinations of Cols + n were going to be greater than the length of the array, and if this happened I would try and write/read from memory that wasn't assigned to the array.

So I inserted the check: if(Cols +n >25).

It was here that I hit a brick wall, because I realised that if the combination was greater than the end of the array I would need to loop through the alphabet again to get the "shifted" character.

After about 30 minutes of tried, tested and quite frankly crazy ideas of how to get this character I decided to write down what I need to do on paper. It was after writing something like the following that I noticed a pattern:

Position: 0 1 2 3 4
Letter: a b c d e

I decided that I would shift the letters all by 3.

Letter to shift: c.
Numerical position: 2.
Numerical position + shift =
2 + 3 = 5.
Letter it should be AFTER shift: a.
Numerical value of letter after shift: 0


Letter to shift: d.
Numerical position: 3.
Numerical position + shift =
3 + 3 = 6.
Letter it should be AFTER shift: b.
Numerical value of the letter after shift: 1

It was then I noticed that the numerical value of the letter it should be after it was shifted was:

Beginning of the array + (Numerical position + Shift) - (length of the array + 1);

In other words:

shifted character = 0 + (n + Cols) - (lengthOfArray + 1);


If the combination of Cols + n wasn't greater than the length of the array, the new character was simply the numerical position of the character + shift.

I hope that explanation made some sense, and sorry if it was blatantly obvious to you! But I am not the most mathematical person; so it took me some time!

Comments


love 0 0 joy 0 0 wow 0 0 mad 0 0 sad 0 0 fear 0 0 neutral 0 0
:iconcodeisart:
Really great description :clap:
Could this not have been achieved using a modulus operator and/or abs ()? Modulus would allow you to 'wrap' around after going over a set maximum number, allowing you to quickly select array positions. Also is there a way to specify the alphabet as a full string, and then explode/split it into an array. You could also build the array using a loop and the ASCII values (a = 65).

Thanks for the time taken to select, prepare and upload your code :)

Details

November 4, 2007
30.1 KB
30.1 KB
1172×395

Statistics

1
0
234 (0 today)
22 (0 today)

Share

Link
Embed
Thumb

Site Map