Recent

Author Topic: Converting 2D array into a 1D array  (Read 2878 times)

yalld

  • Newbie
  • Posts: 1
Converting 2D array into a 1D array
« on: October 14, 2017, 12:03:23 am »
Hello everyone, I am quite new to programming in general. One thing that I wanted to do was to sorty my 2D array to a descending order, one way i found how to do it (on the internet in another programming language) was converting 2D array into a 1D array, sorting that and then converting it back to a 2D array. I can't seem to figure out how to convert the arrays. I have no idea on where to really start, I've gotten as far as
Code: Pascal  [Select]
  1. array1d:=(m)*(n);  
but even that gives me an error. Any help would be appreciated. Also n = rows, m = collums.

jamie

  • Hero Member
  • *****
  • Posts: 2252
Re: Converting 2D array into a 1D array
« Reply #1 on: October 14, 2017, 02:59:57 am »
Please have a look here.

https://www.tutorialspoint.com/pascal/pascal_multi_dimensional_arrays.htm

You can use double loop to scan through a 2D array..

Var
 X,Y,Z:Integer;
 A :Array[1..10, 1..10] of Integer;
 B :Array[1..100] of integer;
Begin
 Z := 1;
 For X := 1 to 10 do For Y := 1 to 10 do
  Begin
    B[Z] := A[X,Y];
    Inc(Z);
  End;

-- That's off the top of my head but that will copy the 2D content to the 1D array.
Number 1 at blue screen app creations!

Eugene Loza

  • Hero Member
  • *****
  • Posts: 563
    • My "almost daily" development blog
Re: Converting 2D array into a 1D array
« Reply #2 on: October 14, 2017, 07:14:43 am »
You can easily do this through pointers.
Pseudocode (I haven't tested it)
Code: Pascal  [Select]
  1. var M: array[0..10,0..10] of integer;
  2. P: ^integer;
  3. ...
  4. P := @M[0,0]; //get the address of first array element
  5. for i := 0 to (10+1)*(10+1)-1 do begin
  6.   WriteLn(P^); //use current value, ^ denotes access to the value which pointer is pointing to
  7.   inc(P); //get the next value
  8. end;
You may easily use it as P+17 is the 17th element of the array, i.e. if you need unordered access to the array:
Code: Pascal  [Select]
  1. WriteLn((P+Random((10+1)*(10+1)-1))^);
« Last Edit: October 14, 2017, 07:19:04 am by Eugene Loza »
Lazarus 1.9 + FPC 3.1.1 Debian Jessie 64 bit.

My Free and Open Source games in Lazarus/FreePascal/CastleGameEngine:
https://decoherence.itch.io/
(and some ancient games in Turbo Pascal too)
Sources are here: https://github.com/eugeneloza?tab=repositories

schuler

  • Full Member
  • ***
  • Posts: 150
Re: Converting 2D array into a 1D array
« Reply #3 on: October 15, 2017, 06:36:13 am »
I have a similar problem in my project. In my case, I need both 3D and 1D representations at the same time. In the case that you wish to have a look, here we go:

https://sourceforge.net/p/cai/svncode/HEAD/tree/trunk/lazarus/libs/uvolume.pas

Thaddy

  • Hero Member
  • *****
  • Posts: 9399
Re: Converting 2D array into a 1D array
« Reply #4 on: October 15, 2017, 11:39:39 am »
I have a similar problem in my project. In my case, I need both 3D and 1D representations at the same time. In the case that you wish to have a look, here we go:

https://sourceforge.net/p/cai/svncode/HEAD/tree/trunk/lazarus/libs/uvolume.pas

Declare the 1D as absolute to the 2D or the other way around e.g:
Code: Pascal  [Select]
  1. program program1;
  2. {$ifdef fpc}{$mode delphi}{$H+}{$endif}
  3. var
  4.   a:array[0..15] of integer =(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
  5.   b:array[0..3,0..3] of integer absolute a;
  6.   i,j:integer;
  7. begin
  8.   for i := 0 to 3 do
  9.     for j := 0 to 3 do
  10.       writeln(b[i,j]);
  11. end.
Be careful with dynamic arrays.
« Last Edit: October 15, 2017, 11:50:53 am by Thaddy »
also related to equus asinus.