Showing posts with label Dynamic memory allcation. Show all posts
Showing posts with label Dynamic memory allcation. Show all posts
C PROGRAMS : DYNAMIC MEMORY ALLOCATION

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;
}

C PROGRAMS : DYNAMIC MEMORY ALLOCATION

C PROGRAMS : DYNAMIC MEMORY ALLOCATION

C PROGRAMS : DYNAMIC MEMORY ALLOCATION
1. Program to dynamically allocate memory using calloc for "n" integer elements, input elements, display it and then free the memory
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>

int main()
{
    int *ptr,n,i;
    
    printf("Enter number of elements : ");
    scanf("%d",&n);
    
    // Allocating memory dynamically
    ptr=(int *)calloc(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));
 }
 
 // Deallocating memory
 free(ptr);
 
    getch(); // Linux user - Remove this

    return 0;
}

2. Program to dynamically allocate memory using malloc for "n" integer elements, input elements, display it and then free the memory
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>

int main()
{
    int *ptr,n,i;
    
    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));
 }
 
 // Deallocating memory
 free(ptr);
 
    getch(); // Linux user - Remove this

    return 0;
}

3. Program to dynamically allocate memory using malloc for "n" integer elements, input elements and then display it. Then reallocate memory for the same pointer variable, input elements, display it and then free the memory
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>

int main()
{
    int *ptr,n,i;
    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));
 }
 
 printf("\nEnter number of elements : ");
    scanf("%d",&n);
    
    // Reallocating memory 
 ptr=realloc(ptr,n);
 
 // 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));
 }
 
 // Deallocating memory
 free(ptr);
    
    getch(); // Linux user - Remove this

    return 0;
}

4. Program to find the length of the string
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>
void main()
{
 char *ptr;
 
 // Declaring variable i=to iterate loop, l=lenght of string
 int i, l=0;
 
    // Allocating memory
 ptr=(char *)malloc(12*sizeof(char));
 
 // Inputing string
 printf("Enter string : ");
 gets(ptr);
 
 // Displaying string
 printf("Entered string : ");
 puts(ptr);
 
 // Calculating length
 while(ptr[l]!='\0')
 {
 l++;
 }
 
 // Printing length of string
 printf("Lenght of string : %d",l);
 
 // Deallocating memory
 free(ptr);
 
 getch(); // Linux user - Remove this

 return 0;
}