When i am this void linearSearch function in code with cout it is showing this error:
<source>: In function 'int main()':
<source>:28:10: error: no match for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream<char>'} and 'void')
28 | cout << linearSearch(arr, n, key) << endl;
| ~~~~ ^~ ~~~~~~~~~~~~~~~~~~~~~~~~~
| | |
| | void
| std::ostream {aka std::basic_ostream<char>}
And when i write int linearSearch function with return it works fine.
#include <bits/stdc++.h>
using namespace std;
void linearSearch(int arr[], int n, int key)
{
for (int i = 0; i < n; i++)
{
if (arr[i] == key)
{
cout << i;
}
}
cout << "not found";
}
int main()
{
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int key;
cin >> key;
cout << linearSearch(arr, n, key) << endl;
return 0;
}