Monday, 14 August 2017

Write a C program to find whether a given matrix is an upper triangular matrix or not.

                                    UPPER TRIANGULAR MATRIX
                                        
                                              C-Solution
#include<stdio.h>
#include<stdlib.h>
int **create(int r,int c)
{
    int **mat,i;
    mat=(int **)malloc(r*sizeof(int *));
    for(i=0;i<r;i++)
    {
        *(mat+i)=(int *)malloc(c*sizeof(int ));
    }
    return mat;
}
int **getelement(int **m,int r,int c)
{
    int i,j;
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            scanf("%d",*(m+i)+j);
        }
    }
    return m;
}
void exec(int **m,int r,int c)
{
    int i,j,c1=1;
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            if((j<i)&&*(*(m+i)+j)!=0)
            {
                c1=0;
            }
        }
    }
    if(c1==1)
    {
        printf("yes");
    }
    else
    {
        printf("no");
    }
}
int main()
{
    int **m;
    int r;
    scanf("%d",&r);
    m=create(r,r);
    m=getelement(m,r,r);
    exec(m,r,r);
    return 0;
}

1 comment: