DS 1 – ARRAY OPERATIONS

  DS LAB
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.

array.c – PROGRAM

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

int a[10],n,elem,i,pos;
void create();
void display();
void insert();
void delete();

void create()
{
int i;
printf(“Enter the size of arrayn”);
scanf(“%d”,&n);
printf(“Enter the elements of arrayn”);
for(i=0; i<n; i++)
scanf(“%d”,&a[i]);
}

void display()
{
int i;
printf(“The array elements are:n”);
for(i=0; i<n; i++)
printf(“%dt”,a[i]);
}

void insert()
{
int i;
printf(“Enter the position for new element:n”);
scanf(“%d”,&pos);
printf(“Enter the element to be inserted:n”);
scanf(“%d”,&elem);
for (i = n-1; i <=pos; i–)
a[i+1]=a[i];
a[pos]=elem;
n=n+1;
}

void delete()
{
printf(“Enter  position of element to be deleted:n”);
scanf(“%d”,&pos);
elem=a[pos];
for (int i = 0; i < n-1; i++)
a[i]=a[i+1];
n=n-1;
printf(“Deleted element is %dn”,elem );
}

int main()
{
int ch;
while(ch)
{
printf(“nn______MENU_____n”);
printf(“1.Createn2.Displayn3.Insertn4.Deleten5.Exitn”);
printf(“Enter Your Choice:”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:create();break;
case 2:display();break;
case 3:insert();break;
case 4:delete();break;
case 5:exit(0);break;
default :printf(“INVALID CHOICEn”);
}
}return 0;

}

LEAVE A COMMENT