Solved

Write a function to do this ?

Posted on 2013-01-27
5
288 Views
Last Modified: 2013-01-28
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 ....
0
Comment
Question by:mohammedokasha
5 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 38824546
The input is 2d array?
0
 
LVL 84

Expert Comment

by:ozo
ID: 38824779
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
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 38824782
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
 

Author Comment

by:mohammedokasha
ID: 38825062
TommySzalapski explain more using number plz
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
ID: 38825264
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

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
This video teaches viewers about errors in exception handling.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question