In this algorithm , we have to find the sum of lower triangular elements of matrix . Supoose a 2d matrix ,which is 3*3 then lower triangular elements are those which are below left diagonal including left diagonal i.e when either i=j or when i>j . So, when the condition is true, the element is added in variable sum . & hence the sum of lower triangular elements of matrix is found .
#include<stdio.h>
#include<conio.h>
void main()
{ int a[10][10],i,j,m,sumleft=0;
clrscr();
printf("\n program to find sum of left triangular matrix elements");
printf("\n (rows & columns should be same)");
printf("\n\n enter no. of rows & cloumns : ");
scanf("%d",&m);
printf("\n now enter them : \n");
for(i=0;i<m;i++)
{ for(j=0;j<m;j++)
{ printf("enter a[%d][%d] = ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\n displaying matrix : \n");
for(i=0;i<m;i++)
{ for(j=0;j<m;j++)
{ printf("\t a[%d][%d] = %d",i,j,a[i][j]);
if(i>=j)
{ sumleft = sumleft + a[i][j];
}
}
printf("\n");
}
printf("sum of left triangular matrix elements = %d",sumleft);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{ int a[10][10],i,j,m,sumleft=0;
clrscr();
printf("\n program to find sum of left triangular matrix elements");
printf("\n (rows & columns should be same)");
printf("\n\n enter no. of rows & cloumns : ");
scanf("%d",&m);
printf("\n now enter them : \n");
for(i=0;i<m;i++)
{ for(j=0;j<m;j++)
{ printf("enter a[%d][%d] = ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\n displaying matrix : \n");
for(i=0;i<m;i++)
{ for(j=0;j<m;j++)
{ printf("\t a[%d][%d] = %d",i,j,a[i][j]);
if(i>=j)
{ sumleft = sumleft + a[i][j];
}
}
printf("\n");
}
printf("sum of left triangular matrix elements = %d",sumleft);
getch();
}

No comments:
Post a Comment
Guys if you think something is wrong or should be edit than please do comment.