You forgot a ;
in line 4 of your code dude! (In the image you posted.)
Actually there is no need of line 4: int lsearch(int [], int, int)
. Because in the next line you are defining the function itself. You can skip the prototype declaration if you wish.
And from next time please post proper code, not just an image. And by code I mean actual code that is causing error. Here the code in your image is different from the one which you have typed in your post!
In your typed code you are calling lsearch
as lsearch(arr[], N, ITEM)
[line 34]. A call should be made like this: lsearch(arr, N, ITEM)
.
Here is you corrected code:
#include <iostream>
using namespace std;
int lsearch(int [], int, int);
int main() {
int N, ITEM, INDEX, ar[20];
cout << "How many elements? (max 20): " << endl;
cin >> N;
cout << "Enter elements: " << endl;
for (int i = 0; i < N; i++)
cin >> ar[i];
cout << "Your array is as follows: " << endl;
for (int i = 0; i < N; i++)
cout << ar[i] << endl;
cout << "Enter the element to be searched for: " << endl;
cin >> ITEM;
INDEX = lsearch(ar, N, ITEM);
if (INDEX == -1)
cout << "Element not found!" << endl;
else
cout << "Item found at index: " << INDEX << " position: " << INDEX + 1 << endl;
return 0;
}
int lsearch(int ar[], int N, int ITEM) {
for (int i = 0; i < N; i++)
if (ar[i] == ITEM)
return i;
return -1;
}
Sample Run:
How many elements? (max 20): 5
Enter elements: 1 2 3 4 5
Your array is as follows:
1
2
3
4
5
Enter the element to be searched for: 4
Item found at index: 3 position: 4
This code is practically the same (I guessed this code from your image):
#include <iostream>
using namespace std;
int lsearch(int[], int, int); // Your line 4 which isn't necessary and where you missed a semi-colon!
int lsearch(int ar[], int N, int ITEM) {
for (int i = 0; i < N; i++)
if (ar[i] == ITEM)
return i;
return -1;
}
int main() {
// same as in above code
}
You should also check out this thread on why "using namespace std" is considered a bad practice.