2D array sort using function - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# 2D array sort using function

Posts: 5Member
Hi. I've started programming recently so I'm a newbie. I checked how functions work, and tried to sort 2D array using function. Here's the code:
[code]

program functionSort;
uses crt;
var a:array of integer;
i,j:integer;

function sort(a:array of integer):array of integer;
var i,j,k,l,tmp:integer;
begin
for i:=1 to 3 do
for j:=1 to 2 do
for k:=1 to 3 do
for l:=1 to 2 do
begin
if a[i,j]>a[k,l] then
begin
tmp:=a[i,j];
a[i,j]:=a[k,l];
a[k,l]:=tmp;
end;
end;
sort:=a;
end;

begin
for i:=1 to 3 do
for j:=1 to 2 do
begin
end;
write('Press anykey');
for i:=1 to 3 do
for j:=1 to 2 do
begin
writeln(sort(a));
end;
end.
[/code]
Another question, what's this sort called? Bubblesort or Selection sort? Or something third?

• Posts: 5Member
Sorry for the double post, the forum got bugged.
In 1st var line i for got to add array length, and in function line.
Also at the last writeln i added sort(a[i,j])
Here's how it looks now:
[code]

program functionSort;
uses crt;
var a:array[1..3,1..2] of integer;
i,j:integer;

function sort(a:array[1..3,1..2] of integer):array[1..3,1..2] of integer;
var i,j,k,l,tmp:integer;
begin
for i:=1 to 3 do
for j:=1 to 2 do
for k:=1 to 3 do
for l:=1 to 2 do
begin
if a[i,j]>a[k,l] then
begin
tmp:=a[i,j];
a[i,j]:=a[k,l];
a[k,l]:=tmp;
end;
end;
sort:=a;
end;

begin
for i:=1 to 3 do
for j:=1 to 2 do
begin