# How to understand this code written in Pascal?

• doktorwho
In summary, The program tries to solve a matrix equation and it has a function to keep track of the values.
doktorwho

## Homework Statement

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:
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 can't get this code to actually run in the Lazarus Editor for pascal?
Here is the screenshot:

## Homework Equations

3. The Attempt at a Solution [/B]
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:
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:
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?

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:
doktorwho
jedishrfu said:
I think its expecting a unit statement before the program statement.
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.

doktorwho said:
Like it has to have a function to tell it to actually read the inputs?
To read and assign the data to the desired variable (here, an element of the array).

doktorwho
doktorwho said:
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:
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?
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:
x = y;
y = x;
you end up with two variables with the same value. A third variable is needed to do a swap.

doktorwho
doktorwho said:
And one other thing, do you know why i can't get this code to actually run in the Lazarus Editor for pascal?
Here is the screenshot:
View attachment 110446
The screen shot you show is an editor -- you need a Pascal compiler to compile the code and run it.

doktorwho
Mark44 said:
The screen shot you show is an editor -- you need a Pascal compiler to compile the code and run it.
yeah i got it, the problem was that i was creating a new unit rather than a new program in lazarus.

## What is Pascal and why is it used for coding?

Pascal is a high-level programming language that was developed in the 1970s by Niklaus Wirth. It was designed to be easy to read and write, making it a popular choice for beginners and educational purposes. It is also used in the development of scientific and mathematical applications.

## How do I read and understand code written in Pascal?

To understand code written in Pascal, it is important to first familiarize yourself with the language's syntax and keywords. Then, start by breaking the code into smaller parts and analyzing each section to understand its purpose and how it contributes to the overall functionality of the program. It may also be helpful to refer to documentation or seek assistance from other programmers.

## What are the common data types used in Pascal?

Some of the common data types used in Pascal include integers, real numbers, strings, booleans, and characters. These data types are used to store different types of values and can be manipulated using various operations and functions.

## What are the key control structures in Pascal?

The key control structures in Pascal include conditional statements (if/else), loops (for/while), and procedures/functions. These structures allow for decision-making, repetition, and modularization of code, making it more organized and efficient.

## How can I improve my understanding of Pascal code?

One way to improve your understanding of Pascal code is to practice writing your own programs and experimenting with different functionalities. Additionally, reading and analyzing code written by others, as well as seeking guidance from experienced programmers, can also help improve your understanding of the language.

### Similar threads

• Engineering and Comp Sci Homework Help
Replies
7
Views
2K
• Engineering and Comp Sci Homework Help
Replies
10
Views
2K
• Engineering and Comp Sci Homework Help
Replies
3
Views
2K
• Engineering and Comp Sci Homework Help
Replies
2
Views
2K
• Engineering and Comp Sci Homework Help
Replies
7
Views
2K
• Engineering and Comp Sci Homework Help
Replies
1
Views
1K
• Programming and Computer Science
Replies
1
Views
980
• Engineering and Comp Sci Homework Help
Replies
2
Views
2K
• Engineering and Comp Sci Homework Help
Replies
2
Views
3K
• Engineering and Comp Sci Homework Help
Replies
7
Views
1K