Solved

Write a function to do this ?

Posted on 2013-01-27
5
289 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

830 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