Saturday, 5 October 2013

Creating dynamic array

    In this programm we Creating array of given size dynamically   & doing operation of insertion & deletion
 Must include header " malloc.h ".
     For better description see " C program " carefully.

                                                            C code :
 //make an array of given size dynamicaly & do oeration of insersion & deletion\  
 #include<stdio.h>  
 #include<malloc.h>  // including heder malloc.h  
 void insert(int *,int);  
 void display(int *,int);  
 void po_delete(int *,int);  
 void operation(int *,int);  
 main()  
 {  
   int *arr; // define ponter variabe of int type  
      int size,i;  
      printf("enter size of array requred : ");  
      scanf("%d",&size);  
        arr=(int *)malloc(size*sizeof(int));  // creating array of given size dynamicaly  
       printf("input elements of array \n");  
        for(i=0;i<size;i++)  
         {  
              scanf("%d",(arr+i));  
         }  
         display(arr,size);  
         operation(arr,size);  
    return 0;  
 }   
 void insert(int *arr,int size) // simply insertion   
 {  
      int i,pos,n;  
      printf("\n input number u have to insert :");  
      scanf("%d",&n);  
      printf("\n input position to at which u have to insert :");  
      scanf("%d",&pos);  
      for(i=size-1;i>=pos;i--)  
       {  
             *(arr+i)=*(arr+i-1);  
       }  
      *(arr+pos-1)=n;  
      display(arr,size);  
 }   
 void po_delete(int *arr,int size) // deletion   
 {  
      int i,pos;  
      printf("\n insert the postion of element to be deleted :");  
      scanf("%d",&pos);  
      for(i=pos;i<size;i++)  
       {  
            *(arr+i-1)=*(arr+i);  
       }  
       *(arr+size-1)=0; // last element must be vacant  
      display(arr,size);  
      operation(arr,size);  
 }   
 void display(int *arr,int size)  
 {  
       int i;  
       printf("\n data elements of array \n");  
    for(i=0;i<size;i++)  
    {  
         printf("\t%d",*(arr+i));  
    }  
 }  
 void operation(int *arr,int size)  
 {  
      int n;  
       printf("\n\tinput.... \n\t 2 for input a number at given posion \n\t 1 for delete a no from given position\n\t\t:::");  
    scanf("%d",&n);  
    switch(n)  
    {  
         case 2:  
              insert(arr,size);  
              break;  
         case 1:  
              po_delete(arr,size);  
              break;  
         default :  
              printf("u enter wrong keyword");  
              operation(arr,size);  
              break;  
    }  
 }  

OUTPUT :-

3 comments:

  1. I truly love your blog.. Excellent colors &
    theme. Did you develop this website yourself? Please reply back as I'm planning to create my very own site and would love to know where you got this from or just what the theme
    is called. Cheers!

    My web blog what is a hemroid

    ReplyDelete
  2. I like what you guys are usually up too.
    This sort of clever work and reporting! Keep up
    the amazing works guys I've incorporated you guys to blogroll.



    Feel free to visit my homepage ... symptoms of endometriosis

    ReplyDelete
  3. I muѕt voice my gratitude for finding the time to help peοple like me on thіs particulaг material.
    Your commitment to the subject mater is excellent. I thoгoughly loved thiѕ
    article andd came away feeling smаrter and informed. Α fantastic gift tοo envy upon.
    I nticipate the next post.

    Have a look at my webpage - B2B enterprises

    ReplyDelete

THANKS FOR UR GREAT COMMENT

Blogger Widgets