Solved

Write a function to do this ?

Posted on 2013-01-27
5
292 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Part One of the two-part Q&A series with MalwareTech.
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

617 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