Popular Posts

6/24/2012

Sample Array in C++


The following code :
=====================

#include<iostream.h>
#include<conio.h>
void inputarray(int a[],int n){
for(int i=0;i<n;i++){
cout<<"Enter a["<<i<<"]= ";
cin>>a[i];
}
}
void display(int a[],int n){
for(int i=0;i<n;i++)
cout<<a[i]<<"\t";
cout<<endl;
}
int search(int a[],int n,int x){
int index=-1;
for(int i=0;i<n;i++)
if(x==a[i]){
index=i;
break;
}
return index;;
}
void sort(int a[],int n){
int i,j,t;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j]){
t=a[i];a[i]=a[j];a[j]=t;
}
}
void main(){
int a[40],n,x,pos;
do{
clrscr();
cout<<"Input N : ";cin>>n;
inputarray(a,n);
display(a,n);
cout<<"The Result After sort"<<endl;
sort(a,n);
display(a,n);
cout<<"Input Data to search : ";cin>>x;
pos=search(a,n,x);
if(pos==-1) cout<<"search not found"<<endl;
else cout<<"The Result Search in index : "<<pos<<endl;
   getch();
}while(1);

}

0 comments:

Post a Comment