I have a matrix (5×5) with values in them for example:
Matrix (1,1) Value: ‘a’
Matrix (1, 2) Value: ‘b’
Matrix (2, 1) Value: ‘c’
how would I be able to find the letter ‘a’ in that matrix and have it output the coordinates?
ie
user inputs ‘b’
[searches for ‘b’ in table]
output (1,2)
thanks in advance
It’s as simple as:
Asuming that you declared Matrix as:
And LengthOfMatrix And HeightOfMatrix should be the dimentions of your matrix. They could be switched to something more dynamic like:
In a short description, all that this loop does is it goes through all of the elements of the matrix and outputs the coordinates of every element that matches a certain criteria (In this case – equals to ‘a’).
Note: In most programming languages array’s indexes begin from 0, so the first element in your matrix will be at coords (0,0).