Matrix is toeplitz or not

C program to find if given matrix is toeplitz or not.

A Toeplitz (or diagonal-constant) matrix is a matrix in which each descending diagonal from left to right is constant, i.e., all elements in a diagonal are same.

Diagonals are to be traversed as shown in image below,


In general, any n×m matrix A[][] is a Toeplitz matrix if every cell A[i][j] is same as A[i-1][j-1], A[i+1][j+1], A[i-2][j-2], A[i+2][j+2], ..

Program

#include<stdio.h>
#include<conio.h>
#define MAX 10

void read_matrix(int mat[MAX][MAX], int row, int col)
{
    int i, j;
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            printf("Element [%d][%d]\t:",i,j);
            scanf("%d",&mat[i][j]);
        }
    }
}

void print_matrix(int mat[MAX][MAX], int row, int col)
{
    int i, j;
    for(i=0;i<row;i++)
    {
        for(j=0;j<col;j++)
        {
            printf("%d\t",mat[i][j]);
        }
        printf("\n");
    }
}

int toeplitz(int mat[MAX][MAX], int row, int col)
{
    int i, j;

    for(i=0;i<row-1;i++)
    {
        for(j=0;j<col-1;j++)
        {
            if(mat[i][j] != mat[i+1][j+1])
            {
                return(0);
            }
        }
    }

    return(1);
}

void main()
{
    int mat[MAX][MAX];
    int row, col;

    printf("Enter the number of rows\t:");
    scanf("%d",&row);
    printf("Enter the number of columns\t:");
    scanf("%d",&col);

    printf("Enter the elements of the matrix\n");
    read_matrix(mat, row, col);

    printf("\nMatrix entered is\n");
    print_matrix(mat, row, col);

    if(toeplitz(mat, row, col))
        printf("\nMatrix is a toeplitz matrix");
    else
        printf("\nMatrix is not a toeplitz matrix");

    getch();
}

Output

********** Run1 ********** 

Enter the number of rows        :4
Enter the number of columns     :5
Enter the elements of the matrix
Element [0][0]  :3
Element [0][1]  :7
Element [0][2]  :0
Element [0][3]  :9
Element [0][4]  :8
Element [1][0]  :5
Element [1][1]  :3
Element [1][2]  :7
Element [1][3]  :0
Element [1][4]  :9
Element [2][0]  :6
Element [2][1]  :5
Element [2][2]  :3
Element [2][3]  :7
Element [2][4]  :0
Element [3][0]  :4
Element [3][1]  :6
Element [3][2]  :5
Element [3][3]  :3
Element [3][4]  :7

Matrix entered is
3       7       0       9       8
5       3       7       0       9
6       5       3       7       0
4       6       5       3       7

Matrix is a toeplitz matrix


********** Run2 ********** 

Enter the number of rows        :4
Enter the number of columns     :5
Enter the elements of the matrix
Element [0][0]  :3
Element [0][1]  :7
Element [0][2]  :0
Element [0][3]  :9
Element [0][4]  :8
Element [1][0]  :5
Element [1][1]  :3
Element [1][2]  :7
Element [1][3]  :0
Element [1][4]  :9
Element [2][0]  :6
Element [2][1]  :4
Element [2][2]  :3
Element [2][3]  :7
Element [2][4]  :0
Element [3][0]  :4
Element [3][1]  :6
Element [3][2]  :5
Element [3][3]  :3
Element [3][4]  :7

Matrix entered is
3       7       0       9       8
5       3       7       0       9
6       4       3       7       0
4       6       5       3       7

Matrix is not a toeplitz matrix