-1

I need to create a function that finds the diagonal opposite of a coordinate in a 10x10 grid blocks.

ーーーーーーーーーーーーーーーーー
9|19|29|39|49|59|69|79|89|99|
8|18|28|38|48|58|68|78|88|98|
7|17|27|37|47|57|67|77|87|97|
6|16|26|36|46|56|66|76|86|96|
5|15|25|35|45|55|65|75|85|95|
4|14|24|34|44|54|64|74|84|94|
3|13|23|33|43|53|63|73|83|93|
2|12|22|32|42|52|62|72|82|92|
1|11|21|31|41|51|61|71|81|91|
0|10|20|30|40|50|60|70|80|90|
ーーーーーーーーーーーーーーーーー

For example:

  • input 3 → output 30
  • input 11 → output 11
  • input 13 → output 31

Thank you in advance!

bbnn
  • 3,505
  • 10
  • 50
  • 68
  • I'm a little unsure of what you are asking. Do you want something like "find the indices (i,j) of the element in the array which has value 13, then find the opposite value of the element (j,i)". In other words are all your grids filled with this regular pattern of numbers. – Salix alba Sep 14 '16 at 08:05

2 Answers2

3

Do you need a simple function that swaps the two digits?

Like:

output = (input / 10) + (input % 10) * 10

"/" is integer division here and "%" is modulo operator. The function only works with 0..99.

Martin Sugioarto
  • 340
  • 3
  • 15
0

Lets have a look at coordinates (x, y):

3 is at (1,4) ... 30 is at (4,1) 11 is at (2,2) 13 is at (2,4) ... 31 is at (4,2)

Thus, the conclusion on your input is: you simply swap x,y coordinates and you have the coordinates for the value you are looking for!

You see, the wording of your questions gives it away - it is about drawing a diagonal from (1,1),(2,2) to (10,10); to then "reflect" along that diagonal line.

GhostCat
  • 137,827
  • 25
  • 176
  • 248