Calculate sum of element of lower triangle of m*n matrix by using dynamic memory allocation

How to write a C program to calculate sum of element of lower triangle of m*n matrix by using dynamic memory allocation in C Programming Language ?


Solution:
/* C program to calculate sum of element of lower triangle of m*n matrix
by using dynamic memory allocation */
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void main()
{
int *a[20];
int i,j,m,n,sum=0,k,*p;
clrscr();
printf("\nenter the rows and coloumn :");
scanf("%d%d",&m,&n);
if(m==n)
{
printf("\nenter the element :\n");
for(i=0;i<m;i++)
{
a[i]=(int *)malloc(n * sizeof(int));
for(j=0;j<n;j++)
{
scanf("%d",a[i]+j);
}
}
k=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i>j)
{
*(p+k)=a[i][j];
k++;
}
}
}
printf("the lower triangle element are:\n");
for(i=0;i<m;i++)
{
printf("%d",*(p+i));
sum=sum+*(p+i);
}
printf("\nsum is :%d",sum);
}
else
printf("not possible");
getch();
}
/*

enter the rows and coloumn :3 3

enter the element :
1 2 3
7 8 9
3 6 9
the lower triangle element are:
736
sum is :16
*/


Learn More :