Thursday, 21 June 2012

write a c program to calculate sum of element of upper triangle of m*n matrix by using dynamic memory allocation


/* write a c program to calculate sum of element of upper 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 :                                                            
4 7 8                                                                          
3 6 9                                                                          
1 8 2                                                                          
the lower triangle element are:                                                
789                                                                            
sum is :24                                                                    
*/

No comments: