Search code examples
algorithmmathgridgeometrydiagonal

How do you find the diagonal (left to right) opposite of a coordinate in a 10x10 grid blocks


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!


Solution

  • 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.