0% found this document useful (0 votes)
57 views5 pages

Datastru PDF

The document contains code to perform various operations on a linear array: 1) Insert an element into the array at a given position by shifting other elements and inserting the new element. 2) Delete an element from a given position by shifting subsequent elements forward. 3) Search for a given element and return its location if found, otherwise return that it was not found. 4) Display all elements of the linear array.

Uploaded by

manu
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)
57 views5 pages

Datastru PDF

The document contains code to perform various operations on a linear array: 1) Insert an element into the array at a given position by shifting other elements and inserting the new element. 2) Delete an element from a given position by shifting subsequent elements forward. 3) Search for a given element and return its location if found, otherwise return that it was not found. 4) Display all elements of the linear array.

Uploaded by

manu
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/ 5

CSE-22

EXPERIMENT NO.-01

Question1-write a program that implement the following operation in a


following operation in a linear array-:

1.1-to insert element in linear array


ANS CODE:

#include<iostream>
using namespace std;
int main()
{
int ar[20],i,k,item,n;
cout<<"enter no of elements"<<endl;
cin>>n;
cout<<"enter elements"<<endl;
for(i=0;i<n;i++)
cin>>ar[i];
cout<<"enter the postion u want to enter"<<endl;
cin>>k;
cout<<"enter the element which u want to enter"<<endl;
cin>>item;
for(i=n-1;i>=k;i--)
ar[i+1]=ar[i];
ar[k]=item;
cout<<"display"<<endl;
for(i=0;i<=n;i++)
cout<<ar[i];
return 0;
}
1.2- to delete an element when position is given.
Ans-CODE:

#include<iostream>
using namespace std;
int main()
{
int ar[20],i,k,n;
cout<<"enter no of elements u want to print"<<endl;
cin>>n;
cout<<"enter elements"<<endl;
for(i=0;i<n;i++)
cin>>ar[i];
cout<<"enter the posiont u want to delete "<<endl;
cin>>k;
for(i=k;i<n-1;i++)
ar[i]=ar[i+1];
cout<<"display"<<endl;
for(i=0;i<n-1;i++)
cout<<ar[i];
return 0;
}
1.3-to find location of given position
Ans- CODE:

#include<iostream>
using namespace std;
int main()
{
int ar[20],i,k,n,j,loc;
cout<<"enter no of elements u want to print"<<endl;
cin>>n;
cout<<"enter elements"<<endl;
for(i=0;i<n;i++)
cin>>ar[i];
cout<<"enter the element u want to search"<<endl;
cin>>k;
for(i=0;i<n;i++)
{
if (ar[i]==k)
{
j=1;
loc=i;
break;
}
}
if(j==1)
{cout<<"search suucesfull"<<endl;
cout<<"location is "<<loc;
}
else
cout<<"search is unsucessfull"<<endl;
return 0;
}

1.4-to display element of the linear array

Ans- CODE:
#include<iostream>
using namespace std;
int main()
{
int ar[20],i,k,item,n;
cout<<"enter no of elements"<<endl;
cin>>n;
cout<<"enter elements"<<endl;
for(i=0;i<n;i++)
cin>>ar[i];
for(i=0;i<n;i++)
cout<<ar[i]<<endl;
return 0;
}

You might also like