# 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 ?

Nick
LVL 9
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Author Commented:
Edited text of question.
0
Commented:
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
Author 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
Commented:
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
Commented:
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
Author 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
Commented:
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
Author Commented:
Neil , I think that you wish to take all my points :)

Nick
0
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

Experts Exchange Solution brought to you by