Serial search is also called \"linearSearch\". Suppose you are writing a standal
ID: 3820125 • Letter: S
Question
Serial search is also called "linearSearch". Suppose you are writing a standalone C function called linearSearch that searches through an unsorted C++ builtin array array of int values using this technique. Write the function prototype for this function mdash ONLY the function protototype. Be sure to include the name and type all the input parameters you would need, and a suitable type for the return value. Assume the function returns the index of the value if found, and -1 if the value is not found. Choose parameter names that make sense and represent good programming style (i.e. clearly document the purpose of the parameters that are being passed in.)Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
int linearSearch(int a[],int key,int n)
{
for (int i = 0; i < n; ++i)
{
if(a[i]==key)
return i;
}
return -1;
}
int main(int argc, char const *argv[])
{
int a[]={1,2,3,4,5,6,7};
cout<<"Array is ";
for (int i = 0; i < 7; ++i)
{
cout<<a[i]<<" ";
}
cout<<"Key is 5 ";
int index=linearSearch(a,5,7);
if(index==-1)
cout<<"Key not found ";
else
cout<<"Key found at index " <<index<<endl;
return 0;
}
==========================================================================
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ g++ lineaSea.cpp
[7] Done subl lineaSea.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
Array is
1 2 3 4 5 6 7 Key is 5
Key found at index 4
=========================================================================
Please rate my answer
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.