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

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

C Program Simple linked list

How to write a C Program Simple Linked List in C Programming Language ?


The following program shows how a simple, linear linked list can be constructed in C, using dynamic memory allocation and pointers.

Solution 1:
#include<stdlib.h>
#include<stdio.h>

struct list_el {
   int val;
   struct list_el * next;
};

typedef struct list_el item;

void main() {
   item * curr, * head;
   int i;

   head = NULL;

   for(i=1;i<=10;i++) {
      curr = (item *)malloc(sizeof(item));
      curr->val = i;
      curr->next  = head;
      head = curr;
   }

   curr = head;

   while(curr) {
      printf("%d\n", curr->val);
      curr = curr->next ;
   }


Solution 2 :
/*simple linked list*/
#include <stdio.h>

int main(void)
{
    struct entry
    {
        int value;
        struct entry* next;
    }n1, n2, n3, *list_pointer;
     
    n1.value = 100;
    n1.next = &n2;
 
    n2.value = 200;
    n2.next = &n3;
 
    n3.value = 300;
    n3.next = (struct entry *) 0;
 
    list_pointer = &n1;
 
    while (list_pointer !=n3.next)
    {
        printf("%i\n", list_pointer->value);
        list_pointer = list_pointer->next;
    }
    return 0;
}