I have a problem to manage a two-dimensional matrix nxn in C++. My problem is create a function that control if exists any diagonal, parallel line at the principal diagonal, that is reverse to other. I controlled the two index, necessary for the rows and columns, if are different and maybe I could help me with support arrays, which reverse the elements. Perhaps it's not a good idea with a huge matrix(such as 8x8, 14 arrays) so, I am asking your help.
Thanks
This is my code:
bool funct(short **M, int rows, int columns){
bool found = false;
for(int i = 0; i < rows; i++){
for(int j = 0; j < colums; j++){
if(i != j){
//control the reverse array
}
}
}
}
ps: my primary problem is general algorithm(nxn).
In a quadratic matrix, every diagonal has exactly one other diagonal with the same length (except the principal diagonal). So you just need to check this one:
for(int diagonal = 1; diagonal < cols - 1; ++diagonal)
{
//inspect the diagonal that starts at (0, diagonal)
//the other diagonal starts at (diagonal, 0)
int diagonalLength = cols - diagonal;
//Assume that this diagonal has a reverse counterpart
bool diagonalHasReverse = true;
//Now check if it really has
for(int i = 0; i < diagonalLength; ++i)
{
if(M[i][diagonal + i] !=
M[diagonal + diagonalLength - 1 - i][diagonalLength - 1 - i])
{
diagonalHasReverse = false;
break;
}
}
//do whatever you want with diagonalHasReverse
}
The outer loop does not process the very last (one-element) diagonal. If you want to include that, you have to modify the loop as follows:
for(int diagonal = 1; diagonal < cols; ++diagonal)