-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinearSearch.cpp
50 lines (44 loc) · 996 Bytes
/
linearSearch.cpp
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
#include <iostream>
using namespace std;
int linearSearch(int a[], int size, int key);
void linearSearch2(int a[][4], int r, int c, int key);
int main()
{
int numbs[] = {2, 9, -1, 4, 3};
cout<<"Enter a value to search for: ";
int value;
cin>>value;
int index = linearSearch(numbs, 5, value);
if(index != -1)
{
cout<<value<<" found at index "<<index<<endl;
}
else
cout<<value<<" was not found in the array.\n";
// int a[][4] = {{2, 5,1,6}, {8,1, 6, 7}};
// linearSearch2(a, 2, 4, 8);
return 0;
}
int linearSearch(int a[], int size, int key)
{
for(int i=0; i<size; i++)
if(key == a[i])
return i;
return -1;
}
void linearSearch2(int a[][4], int r, int c, int key)
{
int i, j;
bool isFound = false;
for(i=0; i<r; i++)
for(j=0; j<c; j++)
if(key == a[i][j])
{
isFound = true;
break;
}
if(isFound)
cout<<key<<" found at row "<<i<<" and column "<<j<<endl;
else
cout<<key<<" was not found.\n";
}