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
LVL 9
ginsonicAsked:
Who is Participating?
 
TheNeilConnect With a Mentor Commented:
Nick,

What can I say, you just ask the questions that I know the answers to.

I take it that this is what you what you wanted

The Neil
0
 
ginsonicAuthor Commented:
Edited text of question.
0
 
PeterLarsenCommented:
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
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
ginsonicAuthor Commented:
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 .
0
 
TheNeilCommented:
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
0
 
geobulCommented:
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
0
 
ginsonicAuthor Commented:
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 .
0
 
TheNeilCommented:
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
0
 
ginsonicAuthor Commented:
Neil , I think that you wish to take all my points :)

Put an answer .
Nick
0
 
ginsonicAuthor Commented:
Thanks
0
All Courses

From novice to tech pro — start learning today.