- #1
doktorwho
- 181
- 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.
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]
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;