Triangular Matrix
In this question , we will be given an NxN matrix. We have to determine whether the matrix is a triangular matrix.Write a program in C.
The diagonal of the matrix M of size NxN is the set of entries M(0,0), M(1,1), M(2,2), ..., M(N,N).
A matrix is upper triangular if every entry below the diagonal is 0. For example,
1 1 3
0 0 1
0 0 2
is an upper triangular matrix. (The diagonal itself, and the entries above and below the diagonals can be zeroes or non-zero integers.)
A matrix is lower triangular if every entry above the diagonal is 0. For example,
1 0 0
4 1 0
4 2 2
is a lower triangular matrix.
A matrix is triangular if it is either upper triangular or lower triangular.
This can be done easily using arrays .But we have to do it without using array.
Input
Enter the size of the matrix, N
Enter the NXN matrix
Output
If the input matrix is triangular, then print yes. Otherwise, print no.
C code
#include <stdio.h>
int main()
{
int a,i,j,c1=0,c2=0,N;
scanf("%d",&N);
for(i=1;i<=N;i++)
{
for (j=1;j<=N;j++)
{ scanf("%d",&a);
if ( (j<=i) && (a==0))
{c1++;}
if ( (j>=i) && (a==0))
{c2++;}
}
}
//printf("%d %d",c1,c2);
if (c1== ((N*N)-N)/2 || c2==((N*N)-N)/2) printf("yes");
else printf("no");
return 0;
}
Notes:
I have used the fact that in a matrix of NXN there are N diaganol elements and total N*N elements
so elements which are above/under the diagonal are =(N*N -N)/2
Notes:
I have used the fact that in a matrix of NXN there are N diaganol elements and total N*N elements
so elements which are above/under the diagonal are =(N*N -N)/2
No comments:
Post a Comment