In: Computer Science
#include <iostream>
using namespace std;
int linearSearch(int Salaries[],int n,int key) //function for linearseach
{
int count = 0; //initialize count of the key of which we are searching as 0
for (int i=0; i < n; i++)
if (Salaries[i] == key) //check whether the element in the array and key is same
count++; // if yes,increment teh count by 1
return count; // return the count
}
int main() {
int Salaries[5],i,key;
cout << "enter the salaries:";
for(i=0;i<5;i++)
{
cin >> Salaries[i]; //read elements into array
}
cout << "enter the key to be searched:";
cin >> key; // read the key
int n = sizeof(Salaries)/sizeof(Salaries[0]); //calculate n
cout << linearSearch(Salaries,n,key); // call the function
return 0;
}
Program:
Output: