-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinearsearch.cpp
More file actions
62 lines (60 loc) · 1.08 KB
/
Copy pathlinearsearch.cpp
File metadata and controls
62 lines (60 loc) · 1.08 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<iostream>
using namespace std;
template<class T>
class array
{
private:
int size;
T*element;
public:
array(int s)
{
size=s;
element=new T[size];
}
void readarray();
void display();
int lsearch(T key);
};
template<class T>
void array<T>::readarray()
{
cout<<"\n elements in the array:";
for(int i=0;i<size;i++)
{
cin>>element[i];
}
}
template<class T>
void array<T>::display()
{
cout<<"\n the elements in the array are:";
for(int i=0;i<size;i++)
{
cout<<element[i]<<"\t";
}
}
template<class T>
int array<T>::lsearch(T key)
{
int index=0;
while(index<size)
{
if(element[index]==key)
return (index+1);
index++;
}
return 0;
}
int main()
{
array<int>a(10);
a.readarray();
a.display();
cout<<"\nSearching for element 5\n";
if(a.lsearch(8)==0)
cout<<"\n Element not found";
else
cout<<"\nElement 5 found at position "<<a.lsearch(5);
return 0;
}