0% found this document useful (0 votes)
52 views1 page

Array Function

This document describes a linear search algorithm. It defines an array a with 20 elements to store input data, variables n for the number of elements, x for the element to search for, and i as a counter. It prompts the user to enter the number of elements, populates the array, prompts for the search element x. It then loops from i=0 to i<n, comparing each element a[i] to x, and sets a flag if a match is found. After the loop, it checks the flag to print if the element was found and its position, or not found.

Uploaded by

Bhawna Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views1 page

Array Function

This document describes a linear search algorithm. It defines an array a with 20 elements to store input data, variables n for the number of elements, x for the element to search for, and i as a counter. It prompts the user to enter the number of elements, populates the array, prompts for the search element x. It then loops from i=0 to i<n, comparing each element a[i] to x, and sets a flag if a match is found. After the loop, it checks the flag to print if the element was found and its position, or not found.

Uploaded by

Bhawna Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

LinearSearch()

#include<iostream>

using namespace std;

int main()
{
int a[20],n,x,i,flag=0;
cout<<"How many elements?";
cin>>n;
cout<<"\nEnter elements of the array\n";

for(i=0;i<n;++i)
cin>>a[i];

cout<<"\nEnter element to search:";


cin>>x;

for(i=0;i<n;++i)
{
if(a[i]==x)
{
flag=1;
break;
}
}

if(flag)
cout<<"\nElement is found at position "<<i+1;
else
cout<<"\nElement not found";

return 0;
}

You might also like