Answer the question
In order to leave comments, you need to log in
How to fill an NxM array in a spiral?
Hello everyone, I need help. For a day now I have been trying to write a program that would fill an NxM (not square) array with random numbers in a clockwise spiral. Those. from the first element of the matrix to the element that will be located in the center or slightly left of the center. Plzzz, I really need help.
Answer the question
In order to leave comments, you need to log in
//PascalABC.NET
begin
var (n,m) := (3,6);
var a := new integer[n,m]; //array[0..n-1, 0..m-1] of integer
for var i := 0 to n-1 do
for var j := 0 to m-1 do begin
var x := min(i ,j,n-1 - i, m-1 - j); //number of the spiral, the minimum of 4 numbers
a[i,j] := n*m+1 - if (i>x) and (m-1-j>x) then
(n-2*x-2 )*(m-2*x-2) + (ix) + (jx)
else (n-2*x)*(m-2*x) - (ix) - (jx)
end;
a.Println
end.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question