# How to understand this code written in Pascal?

Tags:
1. Dec 15, 2016

### doktorwho

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
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: ');
if (n > MAX) or (n <= 0) then
exit;

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

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

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:

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
[/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. Dec 15, 2016

### 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?

Last edited: Dec 15, 2016
3. Dec 15, 2016

### 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.

4. Dec 15, 2016

### Staff: Mentor

To read and assign the data to the desired variable (here, an element of the array).

5. Dec 15, 2016

### 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.

6. Dec 15, 2016

### Staff: Mentor

The screen shot you show is an editor -- you need a Pascal compiler to compile the code and run it.

7. Dec 15, 2016

### doktorwho

yeah i got it, the problem was that i was creating a new unit rather than a new program in lazarus.