0% found this document useful (0 votes)
17 views11 pages

Data Structure Lab

The document describes a C++ program that implements linear array operations like insertion, deletion, printing and searching. The program uses a do-while loop with menu driven options to perform these operations and displays the output. It also sorts the array in ascending and descending order.

Uploaded by

Eknoor Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views11 pages

Data Structure Lab

The document describes a C++ program that implements linear array operations like insertion, deletion, printing and searching. The program uses a do-while loop with menu driven options to perform these operations and displays the output. It also sorts the array in ascending and descending order.

Uploaded by

Eknoor Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

1.

Aim/Overview of the practical:


Write a program to implement following operations on a linear array:
1. Read n elements and display
2. Insert a new element in the middle of an array.
3. Delete the first element of an array.
4. Find the location of a last element.

2. Program Code:

#include <iostream>
using namespace std;

int main()
{
int a[50];
int ch;
int pos=0;
int flag=0;
int temp;
for(int i=0;i<50;i++)
{
a[i]=-1;
}
// -1 REPRESENTS EMPTY BLOCK
do{
cout<<"\n1. INSERT\n";
cout<<"2. DELETION\n";
cout<<"3. PRINT\n";
cout<<"4. SEARCH\n";
cout<<"5. SORTING\n";
cout<<"6. EXIT\n";
cout<<"ENTER THE CHOICE\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"ENTER VALUE TO INSERT: \n";
int j;
cin>>j;
for(int i=0;i<50;i++)
{
if(a[i]==-1)
{
a[i]=j;
cout<<"\nVALUE STORED !! \n";
break;
}
else
{
continue;
}
}
break;
case 2:

for(int i=0;i<50;i++)
{
if(a[i]==-1)
{
a[pos-1]=-1;
cout<<"\nELEMENT DELETED SUCCESSFULLY !!\n";
break;
}
else
{
pos++;
}
}
break;

case 3:
cout<<"\nTHE ARRAY ELEMENTS ARE: \n";
for(int i=0;i<50;i++)
{
if(a[i]==-1)
{
break;
}
else
{
cout<<a[i]<<" ";
}
}
cout<<"\n";
break;
case 4:
cout<<"ENTER THE ELEMENT TO SEARCH IN ARRAY: ";
int val;

cin>>val;
for(int i=0;a[i]!=-1;i++)
{

if(a[i]==val)
{
cout<<"THE VALUE IS PRESENT AT: "<<i+1<<" th position in array\n";
flag++;
}
}
cout<<"\nTHE "<<val<<" IS PRESENET " << flag<< " TIMES IN THE
ARRAY";
break;
case 5:
cout<<"ARRAY IN ASCENDING ORDER IS: ";
for(int i=0;i<49;i++)
{
for(int j=0;j<49-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;

}
}
}
for(int i=0;i<50;i++)
{
if(a[i]==-1)
{
continue;
}

else
{
cout<<a[i]<<" ";
}
}

cout<<"\nARRAY IN DESCENDING ORDER IS: ";


for(int i=0;i<49;i++)
{
for(int j=0;j<49-i;j++)
{
if(a[j]<a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;

}
}
}

for(int i=0;i<50;i++)
{
if(a[i]==-1)
{
break;
}
else
{
cout<<a[i]<<" ";
}
}

cout<<"\n";
break;
case 6:
exit(0);
break;
default:
cout<<"PLEASE ENTER A VALID CHOICE IN BETWEEN 1 to 6";

}
}while(1);
return 0;
}
3. Program Output:

a. Read n elements and display:


b. Insert a new element in the middle of an array:

c. Delete the first element of an array:


d. Find the location of a last element:
Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):

Sr. No. Parameters Marks Obtained Maximum Marks


1.
2.
3.

You might also like