Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to sort each row from a two-dimensional array in Pascal

1 Answer

0 votes
program SortRows;

const
  ROWS = 3;
  COLS = 4;

type
  TArray2D = array[1..ROWS, 1..COLS] of Integer;

var
  arr: TArray2D;
  i, j, k, temp: Integer;

procedure SortRow(var row: array of Integer);
var
  i, j, temp: Integer;
begin
  for i := Low(row) to High(row) - 1 do
    for j := Low(row) to High(row) - 1 - i do
      if row[j] > row[j + 1] then
      begin
        temp := row[j];
        row[j] := row[j + 1];
        row[j + 1] := temp;
      end;
end;

begin
  // Initialize the array with some values
  arr[1, 1] := 4; arr[1, 2] := 2; arr[1, 3] := 1; arr[1, 4] := 3;
  arr[2, 1] := 8; arr[2, 2] := 6; arr[2, 3] := 5; arr[2, 4] := 7;
  arr[3, 1] := 12; arr[3, 2] := 9; arr[3, 3] := 11; arr[3, 4] := 10;

  // Sort each row
  for i := 1 to ROWS do
  begin
    SortRow(arr[i]);
  end;

  for i := 1 to ROWS do
  begin
    for j := 1 to COLS do
    begin
      Write(arr[i, j]:4);
    end;
    Writeln;
  end;
end.



(*
run:

   1   2   3   4
   5   6   7   8
   9  10  11  12

*)

 



answered Mar 16, 2025 by avibootz
...