Write a function to do this ?

i try to write function to do this ....

first
if i Enter : 13
select 9 boxes around  [ box 13 ]

so get : 7 8 9 12 13 14 17 18 19
1
second
if i Enter : 34
select 9 boxes around  [ box 34 ]

so get : 27 28 29 33 34 35 3 4 5
2
third
if i Enter : 18
select 9 boxes around  [ box 18 ]

so get : 11 12 7 17 18 13 23 24 19
3

like that ....
mohammedokashaAsked:
Who is Participating?
 
TommySzalapskiConnect With a Mentor Commented:
In your first image, W is 5, H is 5, and you picked 13 for x
One thing I forgot to mention, the math works better if you start everything at 0. So we'll subtract 1 from x and add it back at the end.

So you picked 13, so x = 13-1 = 12

So the row is floor(12/5) = 2 (remember, the top row is 0) so r = 2
c = 12%5 = 2

So the top left is on row (r+(H-1))%H = (2 + (5-1))%5 = 1 and column (c+(W-1))%W =  (2 + (5-1))%5 = 1

So it's at Array[1][1] which would be 1*5+1 or 6 Then you add one since you started at 1 and get 7.

Note that if you are doing this on arrays, it is much simpler since all you need to do is the row/column stuff.
Doing (row+1)%height makes it shift down one row, but if it is at the end, it starts over from the top.

Same thing to shift up/go to the bottom except you use (row + height - 1)%height

Note that on some systems (row-1)%height would also work to shift up a row, but some systems calculate modulo incorrectly for negative numbers so doing (row + height - 1)%height guarantees that it will shift up correctly.

If you explain more precisely what you are doing, I can probably make it make more sense. All I can do right now is give you the theory behind how it would work in the general case.
0
 
Meir RivkinFull stack Software EngineerCommented:
The input is 2d array?
0
 
ozoCommented:
perl -e '$n=(shift)-1; $x=$n%6; $y=$n-$x; for $i( -6,0,6 ){ for$j( -1,0,1){ printf"%2d ",($y+$i)%30+($x+$j)%6+1} print "\n"}' 18
0
 
TommySzalapskiCommented:
It depends on the size of the table, if the width is W and the height is H, then assuming you choose x you will always select these numbers.
first, the row and column (with 0 as the top row) are
r = floor (x/W)
c = x%W

All the numbers on the 'left' are in the column (c+(W-1))%W
All the numbers in the middle are in column c
All the numbers on the 'right' are in column (c+1)%W

All the numbers on the 'top' are in the row (r+(H-1))%H
All the numbers in the middle are in row r
All the numbers on the 'bottom' are in row (r+1)%H

So the 'bottom right' number is on row (r+1)%H column (c+1)%W
Note that x is r*W+c and all the numbers follow that format so the 'bottom right' number is ((r+1)%H)*W +  (c+1)%W

Note that I put 'left' 'bottom' etc in quotes because if x is on the far left, the 'left' numbers will be on the far right of the table.
% is modulo so x%y is the remainder of x/y (i.e. 17%5 = 2)
0
 
mohammedokashaAuthor Commented:
TommySzalapski explain more using number plz
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.