Write a program to create a one dimensional array at run time using a user defined function with user given number of elements into it. Also write separate functions that would allow you to insert and delete elements into/from this array at any arbitrary location.

 /*

Write a program to create a one dimensional array at run time using a user defined function with user given number of elements into it.
Also write separate functions that would allow you to insert and delete elements into/from this array at any arbitrary location.

--------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------

        BY
            MD. ASHHAR JAWAID
            BE/1069/11

--------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------*/

#include<ctype.h>
#include<stdio.h>
#include<stdlib.h>

void create(int *p,int n)
{
     int i;
     for(i=0;i<n;i++)
     {
          scanf("%d",p);
          p++;
     }
}

void display(int *p,int n)
{
     int i;
     for(i=0;i<n;i++)
         printf("%d\n",*(p+i));
}

void ins(int *a,int n,int ip,int temp)
{
    int temp2,i ;
    for(i=0;i<n+1;i++)
    {
        if(i>=ip)
        {
            temp2 = *a ;
            *a = temp ;
            temp = temp2 ;
        }
        a++ ;
    }
}

void del(int *a,int n,int dp)
{
     int i;
     for(i=0;i<n;i++)
     {
             if(i>(dp-1))
                *a = *(a+1);
             a++ ;
    }
}

int main()
{
    int i,*p,n,a,ip,num,dp,o;
    p=(int*)malloc((n)*sizeof(int));
    printf("Enter no. of elemnts of array:");
    scanf("%d",&n);
    printf("Enter the elements:\n");
    create(p,n);
    printf("The Elements are:\n");
    display(p,n);
    do
    {
        printf("Enter 1 for insertion & 2 for deletion:");
        scanf("%d",&a);
        switch(a)
        {
            case 1printf("Enter the position and number to insert:\n");
                    scanf("%d%d",&ip,&num) ;
                    p=(int*)realloc(p,(n+1)*sizeof(int));
                    ins(p,n,ip-1,num);
                    printf("\n");
                    display(p,n+1);
                    n=n+1;
                    break;

            case 2printf("\nEnter the position to delete \n") ;
                    scanf("%d",&dp) ;
                    del(p,n,dp-1) ;
                    p=(int*)realloc(p,(n-1)*sizeof(int));
                    printf("\n");
                    display(p,n-1) ;
                    n=n-1;
                    break;
        }
        printf("\nDo you want to continue? (1 for yes/ 0 for no)");
        scanf("%d",&o);
    }
    while(o==1);
    return 0;
}

Post a Comment

0 Comments