0% found this document useful (0 votes)
109 views

Linear Search

This C++ code implements a linear search algorithm to search for a key value within an integer array. The search function takes the array, key, and size as parameters, iterates through the array until it finds a matching key or reaches the end, and returns the index or -1 if not found. The main function prompts the user for a key, calls search to find the element, and prints the result.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views

Linear Search

This C++ code implements a linear search algorithm to search for a key value within an integer array. The search function takes the array, key, and size as parameters, iterates through the array until it finds a matching key or reaches the end, and returns the index or -1 if not found. The main function prompts the user for a key, calls search to find the element, and prints the result.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Linear Search

#include <conio.h>
#include <iostream.h>

int search(int a[], int k, int si){


int i=0;
while(k!=a[i] && i<si ){ i++; }
if(i<si)
return i;
else
return -1;
}

void main()
{
clrscr();

const int s=5;


int a[s]={3,5,1,4,2};
int key,element;

cout<<"\nEnter the key: ";


cin>>key;

element=search(a,key,s);
if(element!=-1)
cout<<"\nKey found at location ["<<element<<"]" ;
else
cout<<"\nKey was not found";

getch();
}

Click the link below to see more examples

URL: http://ravianeducation.blogspot.com
E-Mail: [email protected]
Farhan: 03008855006

You might also like