How to understand this code written in Pascal?

Click For Summary

Discussion Overview

The discussion revolves around understanding a Pascal program that creates and transposes a matrix. Participants seek clarification on specific code details, including the purpose of certain statements and the overall structure of the program.

Discussion Character

  • Homework-related, Technical explanation, Conceptual clarification

Main Points Raised

  • One participant expresses confusion about the code structure, noting that it includes multiple procedures and asking for clarification on their roles.
  • Another participant explains that the `read(a[i,j])` statement reads input values into the matrix elements, emphasizing the need for a function to handle input.
  • There is a discussion about the purpose of the variable `t` in the transpose function, with one participant explaining that it serves as temporary storage for swapping matrix elements.
  • Some participants discuss the requirement for a unit statement in Pascal, comparing it to Java's package requirements, while others challenge this assertion.
  • One participant notes that the program needs a compiler to run, clarifying that an editor alone is insufficient.

Areas of Agreement / Disagreement

Participants generally agree on the functionality of the `read` and `write` statements, as well as the purpose of the variable `t` in the context of swapping values. However, there is disagreement regarding the necessity of a unit statement before the program statement, with conflicting views presented.

Contextual Notes

Some participants mention limitations in understanding the code due to missing assumptions or knowledge about Pascal's structure and requirements, particularly regarding units and program organization.

Who May Find This Useful

Readers interested in learning Pascal programming, particularly those encountering matrix operations and procedural programming concepts, may find this discussion beneficial.

doktorwho
Messages
181
Reaction score
6

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:
Capture.JPG

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?
 
Physics news on Phys.org
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:
  • Like
Likes   Reactions: 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).
 
  • Like
Likes   Reactions: 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.
 
  • Like
Likes   Reactions: 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.
 
  • Like
Likes   Reactions: 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.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
2K
Replies
3
Views
3K
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
Replies
3
Views
3K