The following code implements linear search (Searching algorithm) which is used to find whether a given number is present in an array and if it is present then at what location it occurs. It is also known as sequential search. It is very simple and works as follows: We keep on comparing each element with the element to search until the desired element is found
Source Code:-
#include <iostream.h>
#include <conio.h>
void main()
{ clrscr();
int array[100], search, c, n;
cout<<"Program for Linear Search by-Tarun Rawat\n";
cout<<"Enter the number of elements in array :";
cin>>n;
cout<<"Enter "<<n<<" integer\n";
for (c = 0; c < n; c++)
cin>>array[c];
cout<<"Enter the number to search\n";
cin>>search;
for (c = 0; c < n; c++)
{ if (array[c] == search) /* if required element found */
{ cout<<search<<" is present at location "<< c+1;
}
}
if (c == n)
cout<<search<<" is not present in array.\n";
getch();
}
Welcome to " way2cplusplus.blogspot.in " Objective of this blog is to implement various Computer Science Engineering Lab problems into C++ programming language. These are basically most common Lab Exercise problems based on the curriculum of engineering colleges throughout the Nation. These lab exercises are also relevant to Data structure. Simply C++ programming zone...
No comments:
Post a Comment