Help Writing a Program: Solving Encryption with Matrix and Key Sequence Method

Click For Summary
The discussion revolves around writing a program to solve an encryption problem using a matrix and a key sequence. The proposed method involves reading a key sequence to rearrange the columns of a matrix based on the key values. The user seeks assistance in inputting characters into the matrix from a single string and transposing it correctly according to the key. Suggestions include keeping the text as a string and calculating character positions dynamically as they are read, rather than using a 2-D array. The goal is to efficiently encrypt the text while adhering to the constraints of the assignment.
doktorwho
Messages
181
Reaction score
6

Homework Statement


For our final test preparation we were given a few problems to write and this is one of them:
Matrix.JPG

I would be very grateful if you could help me find the easiest way to solve it.

Homework Equations


3. The Attempt at a Solution [/B]
First let me tell you my plan for writing this program so you can see if my idea is good. So let's say everything was nicely inputted and i have to do the encription now. My idea is this: I read the first element of the key sequence. Let's say its 3. Then since i know its 3 my first coloumn of the matrix (since its first element) goes to the third place (swap the third and the first). So now my first coloumn is read third. Wherever i find the element 1 in the key that matrix coloumn is my first: ( let's say its on the last position of the key, then my last coloumn is the first). When i read it i read it in the standard way. Does this seem good to you?
Here is the code till now and the place i got stuck at:
Code:
program firstprogram;
const
  MAX=10;
type
  matrix=array[1..MAX,1..MAX] of char;
  key=array[1..MAX] of integer;
var
  a:matrix;
  b:key;
  c:char;
  setchar:set of char;
  n,i:integer;

begin
  writeln('Enter the number of key elements');
  readln(n);
  if (n<5) or (n>MAX) then
     exit;
  writeln('Enter the key elements');
  for i:=1 to n do
      begin
           readln(b[i]);
           if (b[i]<1) or (b[i]>n) then
              exit;
      end;
  setchar:=['a'..'z']; {i need this to check if the matrix input is a charachter and not something else}

  {This is the place i need to ask the user to input the matrix with the original text, 
  it should type it in one line and i should divide it's charachters into places in the matrix..
  i don't know how to do this, could you help me? And possible without me having to
    take a long time to come up with it couse its kinda averagly urgent :), thanks}
end.
 
Physics news on Phys.org
First, are there any other constraints that you need to apply to the key?

It's not quite clear, but I think you are saying that the original text comes one row at a time, but is transposed by columns and read out in columns.
You do not need to store the text as a Pascal 2-D array. You can just keep it as a string, knowing in your code where the column boundaries are.

To avoid excessive processing, I would apply the transposition as the text is read in. I.e., calculate where in the string each character is to go as you read it. If you do it right, as soon as you have finished reading it in you have the encrypted text ready as a string.
 
haruspex said:
First, are there any other constraints that you need to apply to the key?

It's not quite clear, but I think you are saying that the original text comes one row at a time, but is transposed by columns and read out in columns.
You do not need to store the text as a Pascal 2-D array. You can just keep it as a string, knowing in your code where the column boundaries are.

To avoid excessive processing, I would apply the transposition as the text is read in. I.e., calculate where in the string each character is to go as you read it. If you do it right, as soon as you have finished reading it in you have the encrypted text ready as a string.
It is necessary to keep it in the 2-d array as that's their way to see if we can manipulate the matrices. Let me give you an example so you can understand better.
Lets say your key has 5 elements (1,2,3,5,4), you matrix then has 5 coloumns and let's suppose you write on paper you matrix with blank cells. Now you insert the text with each letter in one block. If the text is for example "youdotalkaboutfightclun" it would be like this (cant draw matrix really but here is what it would look like,
y o u d o
t a l k a
b o u t f
i g h t c
l u b
And now if the key is just (1,2,3,4,5) it would read out by coloumns like
ytbiloaoguuluhbdkttoafc but since its (1,2,3,5,4) you read almost the same way but when you gets to the forth coloumn you read the fifth couse that's where the element says 5 ( so at forth place read coloumn 5). I am suppose to permute them by the key so every coloumn would get whete it belongs but having trouble importing charachters in matrix from one long string. Ideas?
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
Replies
3
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
9
Views
2K