You are on page 1of 4

//MENUDRI VENARRAYLEARNI NGPROGRAM

#incl
ude<i ost ream. h>
#incl
ude<coni o. h>
#defineSI ZE11
//Functionf ort rav ersi ngar ray
voidtrav erse( intar ray [
],
intn)
{
cout <<" Gi venAr r ay:" ;
//loopf ort rav er singanddi splaying
for(inti =0;i<n; i
++)
{
cout <<ar ray [i]
<<"" ;
}
};
//Functionf ori nser tingel ement
voidinser t(i
ntar ray [],
int&n, intpos, i
ntv al
ue)
{
//increasi ngsi zeofar rayby1
n+=1;
//loopf ori nser tingt heel ement
for(inti =n;i>=pos- 1;i
--)
{
ar ray [
i]=ar ray [i
-1];
}
//Gi vi
ngv aluet ot henewspace
ar r
ay [
pos- 1]=v al ue;
}
//Functionf ordel etingt hear rayel ement
voiddel eti
ng( i
ntar ray [],
int&n, i
ntv alue)
{
intpos=0;
//loopf ormat chi ngt heel ement
for(inti =0;i<n; i
++)
{
if(array [i
]==v alue)
{
pos=i ;
//loopf ordel et i
ngt hemat chedel
ement
for(intj =pos; j<n;j++)
{
ar ray [
j]=ar ray [
j+1];
}
}
}
//reduci ngt hesi zeofar r
ayby1
n-=1;
}
//Functionf orl i
nearsear ch
voidlinear( i
ntar ray []
,intn, inttar get)
{
inti=0;
//loopf orsear chofel ement
for(i;
i<n; i
++)
{
if(array [i
]==t arget )
{
cout <<" Thegi venv alueisfoundatthel
ocat
ion"
<<i
+1<<endl
;
br
eak;
}
}
}
/
/Functi
onf orbi nar ysear ch
v
oidbinary(intar ray []
,intn, intt ar
get )
{
intst=0, end=n- 1, mi d;
while(st <=end)
{
mi d=( st+end) /2;
if(ar ray [
mi d] ==t arget )
{
cout <<" Thegi v env alueisfoundatthelocat
ion"
<<mi
d+1<<endl
;
ret urn;
}
elsei f(array [mi d] >target )
{
end=mi d-1;
}
elsei f(array [mi d] <target )
{
st =mi d+1;
}
else
{
cout <<" Thegi v env alueisnotfound.
"<<endl
;
}
}
}
/
/Functi
onf orbubbl esor ti
ngt hear r
ay
v
oidbubbl e(intar ray []
,intn)
{
for(
inti =0; i
<n- 1;i++)
{
for (intj =0;j<n- 1;j++)
{
if(array [j]
>ar ray [j
+1] )
{
i
ntt emp=ar ray[j
];
array [j]
=ar r
ay [
j+1];
array [j+1] =temp;
}
}
}
}
/
/Functi
onf ordi splay ingt hear r
ay
v
oiddisplay (intar ray [],
intn)
{
//l
oopf ordi spl ayt hear r
ay
for(
inti =0; i
<n; i++)
{
cout <<ar ray [i]<<"" ;
}
}
;
i
ntmain()
{
i
ntar ray[
SIZE],n,pos, val ue,i=0;
i
ntans=1;
clrscr();
cout <<"--
---
--
--
-----
-ARRAYLEARNI NG- -
---
--
----
---
-";
cout <<"\nGivet hesi zeoft hear r
ay :" ;
cin>>n;
cout <<"\nGivet heel ement soft hear ray:\n";
//loopf orinser ti
ngt hel ement si nt hear ray
for(i
;i<n;i
++)
{
cin>>array[i]
;
}
//loopf orcont inuat ionofmenu
whi l
e( ans==1)
{
intkey ;
cout<<"\n----
----
------
----
--MENU- ----
---
--
----
---
---
";
cout<<"\n\ nPleasesel ectf rom t hef ollowingopt ions"<<endl;
cout<<"1"<<" \tFort raver singt hear r
ay "<<endl ;
cout<<"2"<<" \tFori nser tingt heel ementatnewposi t
ion"<<endl
;
cout<<"3"<<" \tFordel et ingt heel ementbyv alue"
<<endl ;
cout<<"4"<<" \tForl i
nearsear chael ement "<<endl;
cout<<"5"<<" \tForbi nar ysear chael ement "<<endl;
cout<<"6"<<" \tForsor t
ingt hear r
ay "
<<endl ;
cout<<"7"<<" \tFordi spl ay i
ngt hear r
ay "<<endl ;
cout<<"\nPRESSANYOFTHEGI VENKEY: " ;
cin>>key ;
swi t
ch(key )
{
case1:
traver se( ar r
ay ,n);
break;
case2:
cout <<" Gi vet hev aluey ouwantt oadd: ";
cin>>v alue;
cout <<" Gi vet heposi tionatwhi cht heelementi stobeenet
ered: "
;
cin>>pos;
i
nser t( array ,
n, pos, val ue);
break;
case3:
cout <<" Gi vet hev aluey ouwantt odel ete: ";
cin>>v alue;
deleting( ar ray ,n,value) ;
break;
case4:
cout <<" Gi vev al uey ouwantt osear ch: ";
cin>>v alue;
l
inear (ar ray ,
n,v alue) ;
break;
case5:
cout <<" Gi vev al uey ouwantt osear ch: ";
cin>>v alue;
binar y(ar ray,n,v al
ue) ;
break;
case6:
bubbl e( ar ray,n) ;
break;
case7:
di
splay(
arr
ay,n)
;
break;
defaul
t:
cout<<"
PLeasesel
ectakeyf
rom abov
eopt
ions:
";
break;
}
i
f(key>1&&key <7)
{
cout<<"Aft
erdoingoperat
ion: "
<<endl
;
tr
averse(arr
ay,
n);
}
//aski
ngf orconti
nuati
onofmenu
cout<<"\n\nWANNACONTI NUE'0forno'
and'
1fory
es'
:";
ci
n>>ans;
i
f(ans==0)
{
break;
}
}
getch(
);
ret
urn0;
}

You might also like