You are on page 1of 10

OUTPUT

/*Program to find largest and smallest element in an array*/ #include<iostream.h> #include<conio.h> void main() { int arr[20],n,i,j,temp; clrscr(); cout<<"Enter no. of elements"; cin>>n; cout<<"\nEnter array elements"; for(i=0;i<n;i++) { cin>>arr[i]; } for(i=0;i<n;i++) { for(j=0;j<n-1;j++) { if(arr[j]>arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp;

} } } cout<<"\nSmallest element of array : "<<arr[0]; cout<<"\nGreatest element of array : "<<arr[n-1]; getch(); }

OUTPUT

/*Program to insert an element at nth position in an array*/ #include<iostream.h> #include<conio.h> void main() { int arr[20],n,pos,i,j,value; clrscr(); cout<<"Enter no. of elements"; cin>>n; cout<<"\nEnter array elements"; for(i=0;i<n;i++) { cin>>arr[i]; } cout<<"\nEnter the position where you want to insert the new element"; cin>>pos; cout<<"\nEnter new element"; cin>>value; cout<<"\nList before insertion :"; for(i=0;i<n;i++) { cout<<arr[i]<<" "; } for(j=n;j>=pos-1;j--) { arr[j+1]=arr[j]; } arr[pos-1]=value; n++; cout<<"\nList after insertion :"; for(i=0;i<n;i++) { cout<<arr[i]<<" "; } getch(); }

OUTPUT

/*Program to delete an element at nth position in an array*/ #include<iostream.h> #include<conio.h> void main() { int arr[20],n,pos,i,j; clrscr(); cout<<"Enter no. of elements"; cin>>n; cout<<"\nEnter array elements"; for(i=1;i<=n;i++) { cin>>arr[i]; } cout<<"\nEnter the position where you want to delete the element"; cin>>pos; cout<<"\nList before deletion :"; for(i=1;i<=n;i++) { cout<<arr[i]<<" "; } for(j=pos;j<n;j++) { arr[j]=arr[j+1]; } n--; cout<<"\nList after deletion :"; for(i=1;i<=n;i++) { cout<<arr[i]<<" "; } getch(); }

OUTPUT

/*Program to update an element at nth position in an array*/ #include<iostream.h> #include<conio.h> void main() { int arr[20],n,pos,i,value; clrscr(); cout<<"Enter no. of elements"; cin>>n; cout<<"\nEnter array elements"; for(i=0;i<n;i++) { cin>>arr[i]; } cout<<"\nEnter the position where you want to update the element";

cin>>pos; cout<<"\nEnter new element : "; cin>>value; cout<<"\nList before updation :"; for(i=0;i<n;i++) { cout<<arr[i]<<" "; } pos--; arr[pos]=value; cout<<"\nList after updation :"; for(i=0;i<n;i++) { cout<<arr[i]<<" "; } getch(); }

OUTPUT

/*Program to search an element in an array using Linear Search*/ #include<iostream.h> #include<conio.h> void main() { clrscr(); int a[50],n,i,f=0,value; cout<<"Enter how many element"; cin>>n; cout<<"\nEnter the elements:"; for(i=0;i<n;i++) cin>>a[i]; cout<<"\nEnter the number which you want to search"; cin>>value; for(i=0;i<n;i++) { if(a[i]==value) { cout<<"\nnumber is present at location : "<<i+1; f=1; } } if(f==0) { cout<<"\nnumber not present"; } getch(); }

OUTPUT

/*program to search an element from array using Binary Search*/ #include<iostream.h> #include<conio.h> void main() { clrscr(); int list[10],item,n,mid; cout<<"\n***** Binary search ******"; cout<<"\nEnter length of list"; cin>>n; cout<<"\nEnter a sorted list"; for(int i=0;i<n;i++) cin>>list[i]; cout<<"\nEnter the value to be serached "; cin>>item; int beg=0; int end=n-1; mid=(beg+end)/2; while(beg<=end) { if(item==list[mid]) { cout<<"\nelement exists at location ="; break; } else if (item<list[mid]) { end=mid-1; } else { beg=mid+1; } mid=(beg+end)/2;

} if(list[mid]==item) cout<<list[mid]; else cout<<"\nelement is not present in the list"; getch(); }

OUTPUT

/*Program to sort array elements using Bubble Sort*/ #include<iostream.h> #include<conio.h> void main() { int arr[20],n,i,j,temp; clrscr(); cout<<"Enter no. of elements"; cin>>n; cout<<"\nEnter array elements"; for(i=0;i<n;i++) { cin>>arr[i]; } cout<<"\nArray elements before sorting : "; for(i=0;i<n;i++) {

cout<<arr[i]; } for(i=0;i<n;i++) { for(j=0;j<n-1;j++) { if(arr[j]>arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } cout<<"\nArray elements after sorting : "; for(i=0;i<n;i++) { cout<<arr[i]; } getch(); }

15

You might also like