#include <iostream>
// A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements
#define COLS 4
bool isDiagonalSameValue(int matrix[][COLS], int i, int j, int rows, int cols) {
int value = matrix[i][j];
while (++i < rows && ++j < cols) {
std::cout << i << " " << j << " - " << matrix[i][j] << "\n";
if (matrix[i][j] != value) {
return false;
}
}
std::cout << "-------" << "\n";
return true;
}
bool isToeplitz(int matrix[][COLS], int rows, int cols) {
for (int i = 0; i < rows; i++) {
if (!isDiagonalSameValue(matrix, 0, i, rows, cols)) {
return false;
}
}
for (int j = 1; j < cols; j++) {
if (!isDiagonalSameValue(matrix, j, 0, rows, cols)) {
return false;
}
}
return true;
}
int main()
{
int matrix[][COLS] = {
{ 2, 7, 9, 8 },
{ 4, 2, 7, 9 },
{ 3, 4, 2, 7 },
{ 0, 3, 4, 2 },
{ 6, 0, 3, 4 }};
int rows = (sizeof(matrix) / sizeof(matrix[0]));
int cols = (sizeof(matrix) / sizeof(matrix[0][0])) / rows;
if (isToeplitz(matrix, rows, cols)) {
std::cout << "Matrix is a Toeplitz";
}
else {
std::cout << "Matrix is not a Toeplitz";
}
}
/*
run:
1 1 - 2
2 2 - 2
3 3 - 2
-------
1 2 - 7
2 3 - 7
-------
1 3 - 9
-------
-------
-------
2 1 - 4
3 2 - 4
4 3 - 4
-------
3 1 - 3
4 2 - 3
-------
4 1 - 0
-------
Matrix is a Toeplitz
*/