• Home
  • MY TRYOUTS
  • tips
  • tech news
  • PROGRAMS
  • Downloads
  • About
  • Contact

Lab program-1

1. Design, Develop and Implement a menu driven Program in C for the following Array operations a. Creating an Array of N Integer Elements b. Display of Array Elements with Suitable Headings c. Inserting an Element (ELEM) at a given valid Position (POS) d. Deleting an Element at a given valid Position(POS) e. Exit. Support the program with functions for each of the above operations     

//Header files
#include<stdio.h>
#include<stdlib.h>
//Global declarations
int a[20];
int n,val,i,pos;
/*Function Prototype*/
void create();
void display();
void insert();
void delete();


int main()
{
    int choice;
    while(choice)
    {
        printf("\n\n--------MENU-----------\n");
        printf("1.CREATE\n");
        printf("2.DISPLAY\n");
        printf("3.INSERT\n");
        printf("4.DELETE\n");
        printf("5.EXIT\n");
        printf("-----------------------");
        printf("\nENTER YOUR CHOICE:\t");
        scanf("%d",&choice);
    switch(choice)
    {
    case 1:     create();
        break;
    case 2:
        display();
        break;



    case 3:
        insert();
        break;
    case 4:
        delete();
        break;
    case 5:
        exit(0);
    default:
        printf("\nInvalid choice:\n");
    }
    }
    return 0;
}



// Creating an Array

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



//displaying an array elements

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


//inserting an element into an array

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=n+1;
}



//deleting an array element
void delete()
{
printf("\nEnter the position of the element to be deleted:\t");
scanf("%d",&pos);
if(pos<n)
{
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);
}
else{
   printf("\n Invalid position\n");
}
}

// End of Program












Powered by Create your own unique website with customizable templates.
  • Home
  • MY TRYOUTS
  • tips
  • tech news
  • PROGRAMS
  • Downloads
  • About
  • Contact