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!

How to understand this code written in Pascal?

  1. Dec 15, 2016 #1
    1. The problem statement, all variables and given/known data
    I just started learning pascal (school curricuulum) and am finding it quite boring compared to python. After skipping a few lessons i came today and we had this code shown. I know it creates a matrix and then transposes it but am misunderstanding the details of the code and would like help on certain things.
    Code (Pascal):

    program matrices;

    const MAX = 50;

    type matrixx = array[1..MAX,1..MAX] of integer;

    var
       a:matrixx;
       n:integer;

    procedure upis(n:integer; var a:matrixx);
    var i,j:integer;
    begin
         for i:=1 to n do
             for j:=1 to n do
                 read(a[i,j]);
    end;

    procedure ispis(n:integer; a:matrixx);
    var i,j:integer;
    begin
         for i:=1 to n do
         begin
             for j:=1 to n do
                 write(a[i,j],' ');
             writeln();
         end;
         writeln();
    end;

    function transpose(n:integer; a:matrixx):matrixx;
    var t,i,j:integer;
    begin
         for i:=1 to n do
             for j:=i+1 to n do
                 begin
                      t:=a[i,j];
                      a[i,j]:=a[j,i];
                      a[j,i]:=t;
                 end;
         transpose:=a;
    end;

    begin
         writeln('Enter the Matrix number: ');
         readln(n);
         if (n > MAX) or (n <= 0) then
            exit;

         upis(n,a);
         ispis(n,a);

         a:=transpose(n,a);
         ispis(n,a);

         readln(n);
    end.
     
    And one other thing, do you know why i cant get this code to actually run in the Lazarus Editor for pascal?
    Here is the screenshot:
    Capture.JPG
    2. Relevant equations
    3. The attempt at a solution

    So for Pascal we have the constants and variables predefined before we write the program and the main program begins with ##begin## and ends with ##end##. Here we have more programs than one but they are not the main one then. The last one seems like its the main one and the upper ones seem like some sort of subprograms so we would only have to call them in the real program to do the calculation right?
    Now:
    Code (Pascal):

    procedure upis(n:integer; var a:matrixx);
    var i,j:integer;
    begin
         for i:=1 to n do
             for j:=1 to n do
                 read(a[i,j]);
    [/end]
     
    I understand the function of i and j but what is this last part? The read(a[i,j]) part? It read each element of the matrix? Like it has to have a function to tell it to actually read the inputs?
    And the part:
    Code (Pascal):

    begin
         for i:=1 to n do
             for j:=i+1 to n do
                 begin
                      t:=a[i,j];
                      a[i,j]:=a[j,i];
                      a[j,i]:=t;
                 end;
         transpose:=a;
     
    What is the function of t?
     
  2. jcsd
  3. Dec 15, 2016 #2

    jedishrfu

    Staff: Mentor

    I think its expecting a unit statement before the program statement.

    This is akin the java requirement that all classes be in packages ie a package statement followed sometime later by a class statement.

    More here:

    http://wiki.freepascal.org/Unit

    With respect to the read and write statements, the read reads one value from the keyboard and the write writes out one value to the screen followed by a space.

    Now, what does upis() do? what does ispis() do? and what does transpose() do?

    If you read the main method you'll see each one is called and can see the actual purpose of the program.

    The function of t is to hold the value so as not to erase it. This is a classic test question even on programming interviews where they ask you to swap two numbers:

    - newbies will write y=x; and then x=y
    - programmers will write t=x; and x=y and y=t to do the swap without losing the variable's value

    A realworld example is you and your friend get two cups of soda with your names on them but the sodas are swapped. You like Pepsi and your friend likes Dr Pepper. So now how do you swap the soda so the soda and name match up?

    You can't pour your cup into your friends cup right?
     
    Last edited: Dec 15, 2016
  4. Dec 15, 2016 #3

    Mark44

    Staff: Mentor

    I don't think that's it. In the link you provided, it was talking about interfaces, which the OP is not using.

    I'll take a look at the code that was posted.
     
  5. Dec 15, 2016 #4

    NascentOxygen

    User Avatar

    Staff: Mentor

    To read and assign the data to the desired variable (here, an element of the array).
     
  6. Dec 15, 2016 #5

    Mark44

    Staff: Mentor

    read(a[i, j]) takes input from the keyboard and stores it in the (i, j) element of the matrix.

    The purpose of t in the code snippet is temperary storage. The code is swapping the (i, j) and (j, i) elements of the matrix. To do this, you need another variable.
    If you try to do a swap like this:

    Code (Text):
    x = y;
    y = x;
    you end up with two variables with the same value. A third variable is needed to do a swap.
     
  7. Dec 15, 2016 #6

    Mark44

    Staff: Mentor

    The screen shot you show is an editor -- you need a Pascal compiler to compile the code and run it.
     
  8. Dec 15, 2016 #7
    yeah i got it, the problem was that i was creating a new unit rather than a new program in lazarus.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: How to understand this code written in Pascal?
Loading...