Solved

Write a function to do this ?

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

739 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