Link to home
Start Free TrialLog in
Avatar of ginsonic
ginsonicFlag for Romania

asked on

Spiral array algorithm

I need to create an array .

THE IMPORTANT PROBLEM :
This array must respect the algorithm :
Example for an array with 4 rows and 6 columns .

4x6=24 ;

01 02 03 04 05 06
16 17 18 19 20 07
15 24 23 22 21 08
14 13 12 11 10 09 .

The elements are arranged in vortex .

How can I give two values ( one for rows and one for columns ) and the program o make self this array ?

Thanks in advance,
Nick
Avatar of ginsonic
ginsonic
Flag of Romania image

ASKER

Edited text of question.
Avatar of PeterLarsen
PeterLarsen

I think this is what you'r looking for.

var
 MyArray : array[1..4, 1..6] of integer;
begin
 MyArray[2,29:=17;

Regards
Peter
I wish an automation method .
I don't wish to type the value for all elements .
The user will type only number of columns and rows .
Why not dcelare a BIG array (say [1..100, 1..100]) and then have two variables which simply store how much of the array you want to use.

e.g.

DataArray : ARRAY [1..100, 1..100] OF INTEGER;
Array_W : INTEGER;
Array_H : INTEGER;

This way you can change the 'size' of the array while your app is running.

The Neil
Hi,

Maybe next code will help.

var
  MyArray : Variant;
......

procedure MakeArray(Rows,Cols:Integer);
var
  i,j : integer;
begin
  { define array }
  MyArray := VarArrayCreate([1,Rows,1,Cols], varInteger);
  { initialize }
  for i := 1 to Rows do
    for j := 1 to Cols do MyArray[i,j] := 0;
end;

Regards, Geo
I think that you don't understand me .
I must have these value 1,2,3,4.....n in array .
I don't need to make all 0 ( comment of geobul ) ;

Look at next example :

for row:=4 and column:=4

01 02 03 04
12 13 14 05
11 16 15 06
10 09 08 07

I need an array alike this.
with these values for elements 1-16 and in this order .
If you read the elements in order you can see that all are order alike a vortex .
Ahh, I get it. Try this:

Array_Width := 6;
Array_Height := 6;
State := 1;
Level := 0;

//Clear the data
FOR x := 1 TO Array_Width
DO
  FOR y := 1 TO Array_Height
  DO
    Data[x, y] := 0;

x := 1;
y := 1;
n := 0;
Complete := FALSE;
REPEAT
  n := n + 1;

  //Have we already written to this 'cell' (this is our get out clause)?
  IF Data[x, y] <> 0
  THEN
    Complete := TRUE
  ELSE
  BEGIN
    Data[x, y] := n;
    CASE State OF
      1 : BEGIN
            x := x + 1;
            IF x > (Array_Width - Level)
            THEN
            BEGIN
              State := 2;
              x := Array_Width - Level;
              y := y + 1;
            END;
          END;
      2 : BEGIN
            y := y + 1;
            IF y > (Array_Height - Level)
            THEN
            BEGIN
              State := 3;
              y := Array_Height - Level;
              x := x - 1;
            END;
          END;
      3 : BEGIN
            x := x - 1;
            IF x <= Level
            THEN
            BEGIN
              State := 4;
              x := Level + 1;
              y := y - 1;
            END;
          END;
      4 : BEGIN
            y := y - 1;
            IF y <= (Level + 1)
            THEN
            BEGIN
              State := 1;
              Level := Level + 1;
              y := Level + 1;
              x := Level + 1;
            END;
          END;
    END;
  END;
UNTIL Complete;

Just set the Array_Width and Array_Height variables to the size of your array and off you go. It DOES assume that your array is defined from 1 rather than 0 (e.g. Data : ARRAY [1..6, 1..6] OF INTEGER; instead of Data : ARRAY[0..5, 0..5] OF INTEGER;). This routine won't work on anything smaller than 2x2

The Neil
Neil , I think that you wish to take all my points :)

Put an answer .
Nick
ASKER CERTIFIED SOLUTION
Avatar of TheNeil
TheNeil

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks