DIAGONAL MATRIX
Problem Description
Diagonal Matrix.A square matrix which has zeros everywhere other than the main diagonal. Entries on the main diagonal may be any number, including 0.
Write a program to find whether a given matrix is a diagonal matrix or not.
Input Format:
The input consists of (n*n+1) integers. The first integer corresponds to the number of rows/columns in the matrix. The remaining integers correspond to the elements in the matrix. The elements are read in row wise order, first row first, then second row and so on. Assume that the maximum value of m and n is 5.
Output Format:
Print yes if it is a diagonal matrix. Print no if it is not a diagonal matrix.
CODING ARENA::
#include <stdio.h>
int main()
{
int x[10][10] , nr, r , c , flag ;
scanf("%d", &nr) ;
if(nr==nr) /* checking for square matrix */
{
for(r=0 ; r<nr ; r++)
for(c=0 ; c<nr ; c++)
scanf("%d" , &x[r][c]) ;
flag=1 ;
for(r=0 ; r<nr ; r++)
for(c=0 ; c<nr ; c++)
if(r==c) /* true for diagonal elements */
{
if(x[r][c]==0)
flag=0;
}
else
{
if(x[r][c]!=0)
flag=0;
}
if(flag==1)
printf("yes") ;
else
printf("no") ;
}
return 0;
}
Test Case 1
Input (stdin)3
4 5 3
5 4 1
0 0 3
Expected Output
no
Test Case 2
Input (stdin)3
1 0 0
0 2 0
0 0 3
Expected Output
yes
No comments:
Post a Comment