Upper Triangular Lower Triangular

Write a program to check whether a matrix (use dynamic memory allocation
to create the matrix) is
i)Upper Triangular
ii)Lower Triangular

#include
#include
#include
int chkUpper(int**,int,int);
int chkLower(int**,int,int);
void main()
{
	int **a;
	int noOfRows,noOfCols;
	int i,j;
	int upper,lower;
	clrscr();
	printf("\nEnter Number Of Rows: ");
	scanf("%d",&noOfRows);
	printf("\nEnter Number Of Columns: ");
	scanf("%d",&noOfCols);
	if(noOfRows!=noOfCols)
	{
		printf("\nThe Matrix Is Not A Square Matrix So It Cannot Be Verified");
		printf("\nPress Any Key To Exit..");
		getch();
		return;
	}
	a=(int**)calloc(noOfRows,sizeof(int*));
	for(i=0;i<noOfRows;i++)
		a[i]=(int *)calloc(noOfCols,sizeof(int *));
	printf("\nEnter Elements Of %d X %d Matrix: \n",noOfRows,noOfCols);
	for(i=0;i<noOfRows;i++)
		for(j=0;j<noOfCols;j++)
			scanf("%d",a[i]+j);
	printf("\nThe Matrix You Entered Is: \n");
	for(i=0;i<noOfRows;i++)
	{
		for(j=0;j<noOfCols;j++)
		{
			printf("%d ",a[i][j]);
		}/*End of j for loop*/
		printf("\n");
	}/*End of i for loop*/
	upper=chkUpper(a,noOfRows,noOfCols);
	lower=chkLower(a,noOfRows,noOfCols);
	if(upper==1)
		printf("\nThe Matrix Is A Upper Triangular");
	else
		printf("\nThe Matrix Is Not A Upper Triangular");
	if(lower==1)
		printf("\nThe Matrix Is A Lower Triangular");
	else
		printf("\nThe Matrix Is Not A Lower Triangular");
	getch();
}
int chkUpper(int**a,int rows,int cols)
{
	int i,j;
	for(i=0;i<rows;i++)
	{
		for(j=0;j<cols;j++) 		{ 			if(i>j)
			{
				if(a[i][j]!=0)
					return 0;
			}/*End of if*/
			if(i<=j)
			{
				if(a[i][j]==0)
					return 0;
			}/*End of if*/
		}/*end of j for loop*/
	}/*end of i for loop*/
	return 1;
}/*End of function int chkUpper(int**,int,int)*/
int chkLower(int**a,int rows,int cols)
{
	int i,j;
	for(i=0;i<rows;i++)
	{
		for(j=0;j<cols;j++)
		{
			if(i<j) 			{ 				if(a[i][j]!=0) 					return 0; 			}/*End of if*/ 			if(i>=j)
			{
				if(a[i][j]==0)
					return 0;
			}/*End of if*/
		}/*end of j for loop*/
	}/*end of i for loop*/
	return 1;
}/*End of function int chkLower(int**,int,int)*/

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *