forked from abhijithshaji17/cpp-mastery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path25.cpp
More file actions
41 lines (40 loc) · 1.07 KB
/
Copy path25.cpp
File metadata and controls
41 lines (40 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// linearsearch operation on array using templates
// izmethod linearsearch
#include <iostream>
using namespace std;
// Changed return type from 'void' to 'int'
template <typename T>
int linsear(T a[], int lb, int ub, T val) {
for (int i = lb; i <= ub; i++) {
if (a[i] == val) {
return i; // Returns the index where 'val' was found
}
}
return -1; // Returns -1 if not found
}
int main(){
int a[10], n, key;
cout<<"Enter the no. of array elements: ";
cin>>n;
// Read elements into the array
cout<<"Enter elements: ";
for (int i=0; i<n; i++) {
cin>>a[i];
}
cout<<"\nArray:\n";
cout<<"[";
for(int i=0;i<n;i++){
cout<<" "<<a[i];
}
cout<<"]";
cout << "\nEnter the value to search: ";
cin >> key;
// Call function with array, lower bound (0), upper bound (n - 1), and key
int result = linsear(a, 0, n - 1, key);
if (result != -1) {
cout << "Element found at index: " << result << "\n";
} else {
cout << "Element not found in the array.\n";
}
return 0;
}