Practical 1
Practical 1
UID: 18BCS1232
OBJECTIVE:
Write a menu driven program those implements following operations (using separate functions) on a linear
array.
ALGORITHMS:
INSERTION:
1. START
2. SET J=N
3. SET N=N+1
4. REPEAT STEPS 5 AND 6 WHILE J>=K
5. SET A[J+1]=A[J]
6. SET J=J-1
7. SET A[K]=ITEM
8. STOP
DELETION:
1. START
2. SET J=K
3. REPEAT STEPS 4 AND 5 WHILE J<N
4. SET A[J]=A[J+1]
5. SET J=J+1
6. SET N=N+1
7. STOP
SEARCH:
1. START
2. SET J=0
3. REPEAT STEPS 4 AND 5 WHILE J<N
4. IF A[J] IS EQUAL ITEM THEN GOTO STEP 6
5. SET J=J+1
6. PRINT J,ITEM
7. STOP
DISPLAY:
1. START
2. SET I=0
3. REPEAT STEPS 4 AND 5 WHILE I<N
4. PRINT A[I]
5. I=I+1
6. STOP
FLOWCHARTS:
1.) INSERTION:
2:DELETION:
3. SEARCHING
4.) DISPLAY
SOURECE CODE:
#include<iostream>
using namespace std;
int main(void)
{
int r;
label_1:
cout<<"FOR QUE. 1 A PRESS 1"<<endl;
cout<<"FOR QUE. 1 B PRESS 2"<<endl;
cout<<"FOR QUE. 1 C PRESS 3"<<endl;
cout<<"FOR QUE. 1 D PRESS 4"<<endl;
cin>>r;
switch(r)
{
case 1:{
int n;
cout<<"ENTER THE SIZE OF THE ARRAY:"<<endl;
cin>>n;
int a[100];
cout<<"ENTER THE ENTRIES"<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int i=n;i>=0;i--)
{
a[i]=a[i-1];
}
cout<<"ENTER THE FIRST ELEMENT TO BE ENTERED:\n";
cin>>a[0];
cout<<"ENTER THE LAST ELEMENT TO BE ENTERED:\n";
cin>>a[n];
cout<<"ARRAY:"<<endl;
for(int i=0;i<n+1;i++)
{
cout<<a[i]<<endl;
}
}
break;
case 2:
{
int n;
cout<<"ENTER THE SIZE OF THE ARRAY";
cin>>n;
int a[n];cout<<"ENTER THE ENTRIES";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"ENTER THE POSITION";
int x;
cin>>x;
int pos=x-1;
for(int i=pos;i<n;i++)
{
a[i]=a[i+1];
}
cout<<"ARRAY:"<<endl;
for(int i=0;i<n-1;i++)
{
cout<<a[i];
}
}
break;
case 3:{
int a[4];
cout<<"ENTER THE ENTRIES"<<endl;
for(int i=0;i<4;i++)
{
cin>>a[i];
}
cout<<"ENTER THE NUMBER TO BE SEARCHED";
int b;
cin>>b;
for(int i=0;i<4;i++)
{
if(a[i]==b)
{
cout<<i+1;
}
}
}
break;
case 4:
int n;
cout<<"ENTER THE SIZE OF THE ARRAY:"<<endl;
cin>>n;
int a[n];
cout<<"ENTER THE ENTRIES"<<endl;
for(int i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"THE ELEMENTS OF THE ARRAY"<<endl;
for(int i=0;i<n;i++)
{
cout<<a[i]<<endl;
}
break;
}
char k;
cout<<"TO PERFORM AGAIN PRESS 'Y' AND TO EXIT PRESS 'N";
cin>>k;
if(k=='Y')
{
goto label_1;
}
}
OUTPUT:
1.)