Searching the value in an 1D Array in C++ taking random values....

//Program to search an value in an 1D array and print its position and index number.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[100], i, n, s, flag=0;
clrscr();
cout<<"Enter the size of an array: ";
cin>>n;

//Taking random input in an array.
randomize();
for(i=0;i<n;i++)
{
a[i]=random(100)+1;
}
//Taking the value to search in an array.
cout<<"Enter the value to be searched in an array: ";
cin>>s;
//Searching the value in an array.
for(i=0;i<n;i++)
{
if(s==a[i])
{
flag++;
cout<<endl<<s<<" is found at position no. "<<i+1
<<" and index no. "<<i;
}
}
if(flag==0) cout<<endl<<s<<" is not found in an array.";

//displaying the entire array list.
cout<<"\n\nThe entire array list is shown herewith:\n";
for(i=0;i<n;i++)
cout<<a[i]<<"\t";

getch();
}
Powered by Blogger.