1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help writing a program

  1. Jan 16, 2017 #1
    1. The problem statement, all variables and given/known data
    For our final test preparation we were given a few problems to write and this is one of them:
    I would be very grateful if you could help me find the easiest way to solve it.
    2. Relevant equations
    3. The attempt at a solution

    First let me tell you my plan for writing this program so you can see if my idea is good. So lets 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. Lets 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: ( lets 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 (Pascal):

    program firstprogram;
      matrix=array[1..MAX,1..MAX] of char;
      key=array[1..MAX] of integer;
      setchar:set of char;

      writeln('Enter the number of key elements');
      if (n<5) or (n>MAX) then
      writeln('Enter the key elements');
      for i:=1 to n do
               if (b[i]<1) or (b[i]>n) then
      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 dont 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}

  2. jcsd
  3. Jan 16, 2017 #2


    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    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.
  4. Jan 17, 2017 #3
    It is necessary to keep it in the 2-d array as thats 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 lets 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 thats where the element says 5 ( so at forth place read coloumn 5). Im 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?
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted