Write a Program to Search a Element in Array.

#include<iostream.h>
#include<conio.h>
#define MAXARRAY 10
void main(){
	//Clean Screen or Clear Old Content
	clrscr();
	//Variables
	int i,n=0,search,ARRAY[MAXARRAY];
	int found=0;
	//Length of Array
	cout<<"Enter the Length of an Array : ";
	cin>>n;
	//Condition to Match MAXARRAY SIZE
	if(n>MAXARRAY){
		cout<<"Sorry Max Array is 10 ";
	}else{
	//Enter Elements
	for(i=0;i<n;i++){
		cout<<"Enter Element for ARRAY["<<i<<"] : ";
		cin>>ARRAY[i];
	}
	cout<<"\n Enter the Element to Search in ARRAY : ";
	cin>>search;
	//Display + Search
	for(i=0;i<n;i++){
		if(ARRAY[i]==search){
			cout<<" [ "<<ARRAY[i]<<" ]";
			found=1;
		}else{
			cout<<" "<<ARRAY[i];
		}
		//Place Coma After Every ELement but Not in End.
		if(i!=n-1){
			cout<<",";
		}
	}
	if(found){
		cout<<"\n Element Found in [ "<<search<<" ] ";
	}else{
		cout<<"\n Element you Search in Array is Not Found";
	}
 }//else end
 getch();
}

Program Output.

MAX ARRAY
Cross MAXARRAY Length
Element Not Found
Element Not Found
Element Found
Element Found
(Visited 78 times, 1 visits today)
Share with Friends :
Written by:

Leave a Reply

Your email address will not be published. Required fields are marked *