UPPER TRIANGULAR MATRIX
Problem Description
An upper triangular matrix is a square matrix in which all the elements below the diagonal are zero.
That is, all the non-zero elements are in the upper triangle:
Write a C program to find whether a given matrix is an upper triangular 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 rowwise 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 an upper triangular matrix . Print no if it is not an upper triangular matrix
CODING ARENA::
#include <stdio.h>
int main()
{
int MAX_ROWS;
scanf("%d",&MAX_ROWS);
int array[MAX_ROWS][MAX_ROWS];
int row, col, isUpper;
for(row=0; row<MAX_ROWS; row++)
{
for(col=0; col<MAX_ROWS; col++)
{
scanf("%d", &array[row][col]);
}
}
isUpper = 1;
for(row=0; row<MAX_ROWS; row++)
{
for(col=0; col<MAX_ROWS; col++)
{
if(col<row && array[row][col]!=0)
{
isUpper = 0;
}
}
}
if(isUpper == 1)
{
printf("yes\n");
}
else
{
printf("\nno");
}
return 0;
}
Test Case 1
Input (stdin)3
1 2 2
0 3 3
1 2 3
Expected Output
no
Test Case 2
Input (stdin)3
1 2 3
0 5 6
0 0 9
Expected Output
yes
No comments:
Post a Comment