Recently found an interesting task. I really want to solve it, but there are no ideas (except for conditions). Tell me please.
The user enters one (wrong) index of a (two-dimensional) 3x3 array (one of these):
a[0] a[1] a[2]
a[3] a[4] a[5]
a[6] a[7] a[8]
It is necessary to display the correct corresponding index.
a[0][0] a[0][1] a[0][2]
a[1][0] a[1][1] a[1][2]
a[2][0] a[2][1] a[2][2]
Example
Input: a[7]
Conclusion: a[2][1]

  • The usual division with the remainder ... - Akina

1 answer 1

The first index is x / 3 .
The second index is x % 3 .

In general, instead of 3 - the width of the array.