insertion, deletion,display,search,sorting,create using function

#include<stdio.h>
#include<stdlib.h>
int a[20];
int m,n,p,val,i,j,key,pos,temp;

int main()
{
        int ch;
        do
            {
                printf("\n\n\t--------Menu-----------\n");
                printf("\t1.Create\n");
                printf("\t2.Display\n");
                printf("\t3.Insert\n");
                printf("\t4.Delete\n");
                printf("\t5.Search\n");
                printf("\t6.Sort\n");
                printf("\t7.Exit\n");
                printf("-----------------------");
                printf("\n\n\tEnter your choice:\t");
                scanf("%d",&ch);
                switch(ch)
                {
                        case 1:         create();
                                        break;
                        case 2:
                                        display();
                                        break;
                        case 3:
                                        insert();
                                        break;

                        case 4:
                                        del();
                                        break;
                        case 5:
                                        search();
                                        break;
                        case 6:
                                        sort();
                                        break;

                        case 7:
                                        exit(0);
                                        break;
                        default:
                                        printf("\n\n Enter the corrent choice\n please enter between (1-7)");
                                        break;
                }
        }
        while(1);
            return 0;
}

void create()
{
        printf("\nEnter the size of the array elements:\t");
        scanf("%d",&n);
        printf("\nEnter the elements for the array:\n");
        for(i=0;i<n;i++)
        {
                scanf("%d",&a[i]);
        }
}


void display()
{
        int i;
        printf("\nThe array elements are:\n");
        for(i=0;i<n;i++)
            {
                 printf("%d\t",a[i]);
            }
 }


void insert()

{
        printf("\nEnter the position for the new element:\t");
        scanf("%d",&pos);

        printf("\nEnter the element to be inserted :\t");
        scanf("%d",&val);

        for(i=n-1;i>=pos;i--)
        {
                a[i+1]=a[i];
        }

        a[pos] = val;
        n++;
}


void del()
{
        printf("\nEnter the position of the element to be deleted:\t");
        scanf("%d",&pos);
        val=a[pos];
        for(i=pos;i<n-1;i++)
        {
                a[i]=a[i+1];
        }
        n=n-1;
        printf("\nThe deleted element is =%d",val);
}


void search()
{
        printf("\nEnter the element to be searched:\t");
        scanf("%d",&key);

        for(i=0;i<n;i++)
        {
                if(a[i]==key)
                {
                        printf("\nThe element is present at position %d",i);
                        break;
                }
        }
        if(i==n)
        {
                printf("\nThe search is unsuccessful");
        }
}


void sort()
{
        for(i=0;i<n-1;i++)
        {
                for(j=0;j<n-1-i;j++)
                {
                    if(a[j]>a[j+1])
                        {
                                temp=a[j];
                                a[j]=a[j+1];
                                a[j+1]=temp;
                        }
                }
        }
        printf("\nAfter sorting the array elements are:\n");
        display();
}


OUTPUT :-







For Other Programs Visit The WebSite:-   https://www.techapurba.com/
                                                                                                                      
Follow Me On Social Media :-
INSTA photography page :- https://www.instagram.com/photo_mania_hub/

Insta photography link :- https://www.instagram.com/photo_mania_hub/
For tech related videos visit my other website :- https://apurbatechinfo.blogspot.com/

Post a Comment

0 Comments