Lab Program 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.
#include<stdio.h>
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.
#include<stdio.h>
#include<stdlib.h>
#define MAX 5
int a[MAX], pos,
elem;
int n = 0;
void create();
void display();
void insert();
void delete();
void main()
{
int choice;
while(1)
{
printf("\n\n~~~~MENU~~~~");
printf("\n=>1.
Create an array of N integers");
printf("\n=>2.
Display of array elements");
printf("\n=>3.
Insert ELEM at a given POS");
printf("\n=>4. Delete an element at a
given POS");
printf("\n=>5. Exit");
printf("\nEnter your choice: ");
scanf("%d",
&choice);
switch(choice)
{
case
1: create();
break;
case
2: display();
break;
case
3: insert();
break;
case
4: delete();
break;
case
5: exit(1);
break;
default:
printf("\nPlease enter a
valid choice:");
}
}
}
void create()
{
int
i;
printf("\nEnter
the number of elements: ");
scanf("%d",
&n);
printf("\nEnter
the elements: ");
for(i=0;
i<n; i++)
{
scanf("%d",
&a[i]);
}
}
void display()
{
int
i;
if(n
== 0)
{
printf("\nNo
elements to display");
return;
}
printf("\nArray
elements are: ");
for(i=0;
i<n;i++)
printf("%d\t
", a[i]);
}
void insert()
{
int
i;
if(n
== MAX)
{
printf("\nArray
is full. Insertion is not possible");
return;
}
do
{
printf("\nEnter
a valid position where element to be inserted: ");
scanf("%d", &pos);
}while(pos
> n);
printf("\nEnter
the value to be inserted: ");
scanf("%d", &elem);
for(i=n-1;
i>=pos ; i--)
{
a[i+1]
= a[i];
}
a[pos] = elem;
n = n+1;
display();
}
void delete()
{
int
i;
if(n == 0)
{
printf("\nArray
is empty and no elements to delete");
return;
}
do
{
printf("\nEnter
a valid position from where element to be deleted: ");
scanf("%d",
&pos);
}while(pos>=n);
elem = a[pos];
printf("\nDeleted
element is : %d \n", elem);
for(
i = pos; i< n-1; i++)
{
a[i]
= a[i+1];
}
n = n-1;
display();
}
Output:
~~~~MENU~~~~
=>1. Create
an array of N integers
=>2. Display
of array elements
=>3. Insert
ELEM at a given POS
=>4. Delete
an element at a given POS
=>5. Exit
Enter your
choice: 1
Enter the number of elements: 3
Enter the elements: 10 20 30
~~~~MENU~~~~
=>1. Create
an array of N integers
=>2. Display
of array elements
=>3. Insert
ELEM at a given POS
=>4. Delete
an element at a given POS
=>5. Exit
Enter your
choice: 2
Array elements are: 10 20
30
~~~~MENU~~~~
=>1. Create
an array of N integers
=>2. Display
of array elements
=>3. Insert
ELEM at a given POS
=>4. Delete
an element at a given POS
=>5. Exit
Enter your
choice: 3
Enter a valid position where element to be
inserted: 5
Enter a valid position where element to be
inserted: 4
Enter a valid position where element to be
inserted: 3
Enter the value to be inserted: 40
Array elements are: 10 20
30 40
~~~~MENU~~~~
=>1. Create
an array of N integers
=>2. Display
of array elements
=>3. Insert
ELEM at a given POS
=>4. Delete
an element at a given POS
=>5. Exit
Enter your
choice: 3
Enter a valid position where element to be
inserted: 4
Enter the value to be inserted: 50
Array elements are: 10 20
30 40 50
~~~~MENU~~~~
=>1. Create
an array of N integers
=>2. Display
of array elements
=>3. Insert
ELEM at a given POS
=>4. Delete
an element at a given POS
=>5. Exit
Enter your
choice: 3
Array is full. Insertion is not possible
~~~~MENU~~~~
=>1. Create
an array of N integers
=>2. Display
of array elements
=>3. Insert
ELEM at a given POS
=>4. Delete
an element at a given POS
=>5. Exit
Enter your
choice: 4
Enter a valid position from where element to be
deleted: 5
Enter a valid position from where element to be
deleted: 6
Enter a valid position from where element to be
deleted: 4
Deleted element is: 50
Array elements are: 10 20
30 40
~~~~MENU~~~~
=>1. Create
an array of N integers
=>2. Display
of array elements
=>3. Insert
ELEM at a given POS
=>4. Delete
an element at a given POS
=>5. Exit
Enter your
choice: 4
Enter a valid position from where element to be
deleted: 2
Deleted element is: 30
Array elements are: 10 20
40
~~~~MENU~~~~
=>1. Create
an array of N integers
=>2. Display
of array elements
=>3. Insert
ELEM at a given POS
=>4. Delete
an element at a given POS
=>5. Exit
Enter your
choice: 4
Enter a valid position from where element to be
deleted: 1
Deleted element is: 20
Array elements are: 10 40
~~~~MENU~~~~
=>1. Create
an array of N integers
=>2. Display
of array elements
=>3. Insert
ELEM at a given POS
=>4. Delete
an element at a given POS
=>5. Exit
Enter your
choice: 4
Enter a valid position from where element to be
deleted: 0
Deleted element is: 10
Array elements are: 40
~~~~MENU~~~~
=>1. Create
an array of N integers
=>2. Display
of array elements
=>3. Insert
ELEM at a given POS
=>4. Delete
an element at a given POS
=>5. Exit
Enter your
choice: 4
Enter a valid position from where element to be
deleted: 0
Deleted element is: 40
No elements to display
~~~~MENU~~~~
=>1. Create
an array of N integers
=>2. Display
of array elements
=>3. Insert
ELEM at a given POS
=>4. Delete
an element at a given POS
=>5. Exit
Enter your
choice: 4
Array is empty and no elements to delete
~~~~MENU~~~~
=>1. Create
an array of N integers
=>2. Display of
array elements
=>3. Insert
ELEM at a given POS
=>4. Delete
an element at a given POS
=>5. Exit
Enter your
choice: 5
Also Credits to:
Manoj Taleka (manoj89biet@gmail.com)
Yashaswini Jogi (jogi.yash@gmail.com)
No comments:
Post a Comment