-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinearSearch.cpp
More file actions
40 lines (37 loc) · 802 Bytes
/
linearSearch.cpp
File metadata and controls
40 lines (37 loc) · 802 Bytes
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
#include <iostream>
using namespace std;
int linearSearch(int *arr, int size, int val){
bool flag = false;
int index = -1;
for(int i=0; i<size && !flag;i++){
if (arr[i] == val){
flag = true;
index = i;
}
}
return index;
}
void printArray(int *arr, int size_of_array){
cout << "Array = {";
for(int i; i<size_of_array; i++){
cout<< arr[i] <<",";
if (i==size_of_array-1){
cout<<'\b';
}
}
cout <<"}\n";
}
int main() {
int arr[]={77,2,16,99,36,81};
int size_of_array = sizeof(arr)/sizeof(int);
int searching_val = 36, index;
printArray(arr,size_of_array);
index = linearSearch(arr,size_of_array,searching_val);
if(index!=-1){
cout<< "Value found at index " << index <<'\n';
}
else {
cout << "Value not found";
}
return 0;
}