C PROGRAMS : DYNAMIC MEMORY ALLOCATION

C PROGRAMS : DYNAMIC MEMORY ALLOCATION
5. Program to find the sum of "n" elements
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>

int main()
{
    int *ptr,n,i, sum=0;
    
    printf("Enter number of elements : ");
    scanf("%d",&n);
    
    // Allocating memory dynamically
    ptr=(int *)malloc(n*sizeof(int));
    
    // Inputing elements
    printf("Enter Elements : ");
    for(i=0;i<n;i++)
    {
       scanf("%d",(ptr+i));
    }
    
    // Calculating sum
   for(i=0;i<n;i++)
    {
     sum = sum + *(ptr+i);
    }  
    
    // Displaying elements
    printf("Entered elements : ");
    for(i=0;i<n;i++)
    {
    printf("%d ",*(ptr+i));
    }
 
    // Displaying sum
    printf("\nSum of elements : %d",sum);

    // Deallocating memory
    free(ptr);
 
    getch(); // Linux user - Remove this

    return 0;
}

6. Program to sort "n" elements in ascending order
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>

int main()
{
    int *ptr,n,i,j, temp;
    
    printf("Enter number of elements : ");
    scanf("%d",&n);
    
    // Allocating memory dynamically
    ptr=(int *)malloc(n*sizeof(int));
    
    // Inputing elements
    printf("Enter Elements : ");
    for(i=0;i<n;i++)
    {
       scanf("%d",(ptr+i));
    }
    
    // Displaying elements
    printf("Entered elements : ");
    for(i=0;i<n;i++)
    {
       printf("%d ",*(ptr+i));
    }
 
    // Sorting elements
   for(i=0;i<n;i++)
        {
     for(j=i+1;j<n;j++)
     {
      if( *(ptr+i) > *(ptr+j) )
      {
       temp=*(ptr+i);
       *(ptr+i)=*(ptr+j);
       *(ptr+j)=temp;
      }
     }
  }  
    
 
 // Displaying sorted elements
 printf("\nElements sorted in ascending order : ");
 for(i=0;i<n;i++)
        {
        printf("%d ",*(ptr+i));
 }
 
 // Deallocating memory
 free(ptr);
 
    getch(); // Linux user - Remove this

    return 0;
}

7. Program to search an element
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>

int main()
{
    int *ptr,n,i, sum=0;
    
    //Declaring variable es=element to be searched, flag=indicates if element found or not
    int es, flag=0;
    
    printf("Enter number of elements : ");
    scanf("%d",&n);
    
    // Allocating memory dynamically
    ptr=(int *)malloc(n*sizeof(int));
    
    // Inputing elements
    printf("Enter Elements : ");
    for(i=0;i<n;i++)
    {
    scanf("%d",(ptr+i));
 }
    
    // Displaying elements
    printf("Entered elements : ");
    for(i=0;i<n;i++)
    {
    printf("%d ",*(ptr+i));
 }
 
    // Inputing element to be searched
    printf("\nEnter element to be searched : ");
    scanf("%d",&es);
    
    // Searching element
   for(i=0;i<n;i++)
    {
     if( *(ptr+i)==es)
     {
      flag=1;
      break;
     }
 }  
 
 // Displaying element searched or not
 if(flag==1)
 printf("Element found ");
 
 else
 printf("Element not found ");
 
 // Deallocating memory
 free(ptr);
 
    getch(); // Linux user - Remove this

    return 0;
}

8. Program to enter three strings and sort them in ascending order alphabetically
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>
#include<string.h>
int main()
{
 int i,j;
 
 // Declaring variable temp=tempeorary helps in swapping strings
 char temp[50];
 
 // Here 3 = number of string which we can enter
 char *p1[3];

 // Inputing string
 printf("Enter three strings : ");
 for(i=0;i<3;i++)
 {
  p1[i]=(char *)malloc(12*sizeof(char));
  gets(p1[i]);
 }
 
 // Performing sorting
 for(i=0;i<3;i++)
 {
  for(j=i+1;j<3;j++)
  {
   // swapping strings
   if(strcmp(p1[i],p1[j])>0)
   {
    strcpy(temp,p1[i]);
    strcpy(p1[i],p1[j]);
    strcpy(p1[j],temp);
   }
  }
 }
 
 // Displaying sorted string
 printf("Sorted string : \n");
 for(i=0;i<3;i++)
 puts(p1[i]);

 getch(); // Linux user - Remove this

 return 0;
}

Share this

Related Posts

Previous
Next Post »