Showing posts with label Dynamic Memory Allocation. Show all posts
Showing posts with label Dynamic Memory Allocation. Show all posts

C Program to calculate the sum of elements of upper triangle of a n*n matrix using DMA

How to write a C Program to calculate the sum of elements of upper triangle of a n*n matrix using Dynamic memory allocation in C Programming Language ?

Solution:
/*C Program to calculate the sum of elements of upper triangle of a n*n matrix using Dynamic Memory allocation*/
#include<stdio.h>
#include<conio.h>
  void main()
{
  int **ip,m,n;
  int sum=0,i=0,j=0;
  clrscr();
  printf("\nEnter the row and column: ");
  scanf("%d%d",&m,&n);
  ip=(int**)malloc(m*sizeof(int));
  for(i=0;i<m;i++)
  ip[i]=(int*)malloc(n*sizeof(int));
  printf("\n");
  printf("\nEnter the elements: ");
  for(i=0;i<m;i++)
{
  for(j=0;j<n;j++)
{
  scanf("%d",&ip[i][j]);
}
}
  printf("\nEntered elements are:\n");
  for(i=0;i<m;i++)
{
  for(j=0;j<n;j++)
{
  printf("\t%d",ip[i][j]);
}
  printf("\n");
}
  for(i=0;i<m;i++)
{
  for(j=0;j<n;j++)
{
  if(i<=j)
  sum=sum+ip[i][j];
}
}
  printf("\nSum upper triangle is = %d",sum);
  getch();
}

OUTPUT:

Enter the row and column:
3
3

Enter the elements:
1
2
3
4
5
6
7
8
9

Entered elements are:
      1       2       3
      4       5       6
      7       8       9

Sum upper triangle is = 26

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

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



Solution:
/* 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                                                                    
*/