TOEPLITZ MATRIX
Consider the following I/O samples.
#include<stdio.h>
int main()
{
int n,m,arr[100][100],row,col,count=0;
scanf("%d %d",&n,&m);
for(row=0;row<n;row++)
{
for(col=0;col<m;col++)
{
scanf("%d",&arr[row][col]);
}
}
for(row=0;row<n;row++)
{
for(col=0;col<m;col++)
{
if(row+1<n && col+1<m && arr[row][col]!=arr[row+1][col+1])
{
count=1;
break;
}
}
}
if(count == 1)
printf("Not a Toeplitz Matrix");
else
printf("Toeplitz Matrix");
}
SAMPLE INPUT: 3 3
1 5 6
2 1 5
3 2 1
2 1 5
3 2 1
SAMPLE OUTPUT: Toeplitz Matrix
ALGORITHM :
For each element
in our matrix, check the value of its immediate diagonal located at
. If we find an element whose value differs from that of its immediate diagonal, then the matrix is not a Toeplitz Matrix.

C PROGRAM:
For each element
in our matrix, check the value of its immediate diagonal located at
. If we find an element whose value differs from that of its immediate diagonal, then the matrix is not a Toeplitz Matrix.

C PROGRAM:
#include<stdio.h>
int main()
{
int n,m,arr[100][100],row,col,count=0;
scanf("%d %d",&n,&m);
for(row=0;row<n;row++)
{
for(col=0;col<m;col++)
{
scanf("%d",&arr[row][col]);
}
}
for(row=0;row<n;row++)
{
for(col=0;col<m;col++)
{
if(row+1<n && col+1<m && arr[row][col]!=arr[row+1][col+1])
{
count=1;
break;
}
}
}
if(count == 1)
printf("Not a Toeplitz Matrix");
else
printf("Toeplitz Matrix");
}
OUTPUT:
The given source code in C program is short and simple to understand. The source code is well tested in DEV-C++ and is completely error free.
If you have any feedback about this article and want to improve this, please comment in the comment section.
Comments
Post a Comment