Skip to content

Commit 445b168

Browse files
authored
Update LinearSearch.cpp
1 parent e61f82d commit 445b168

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

C++/Algorithms/Searching-Algorithms/LinearSearch.cpp

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,29 @@
22
// is present then return its location, otherwise
33
// return -1
44

5-
#include <iostream>
6-
using namespace std;
7-
8-
int search(int arr[], int n, int x)
9-
{
10-
int i;
11-
for (i = 0; i < n; i++)
12-
if (arr[i] == x)
13-
return i;
14-
return -1;
15-
}
16-
17-
int main(void)
18-
{
19-
int arr[] = { 2, 3, 4, 10, 40 };
20-
int x = 10;
21-
int n = sizeof(arr) / sizeof(arr[0]);
22-
int result = search(arr, n, x);
23-
(result == -1)? cout<<"Element is not present in an array"
24-
: cout<<"Element is present at an index " <<result;
25-
return 0;
26-
}
5+
#include <iostream>
6+
using namespace std;
7+
8+
int search(int arr[], int n, int x)
9+
{
10+
int i;
11+
for (i = 0; i < n; i++)
12+
if (arr[i] == x)
13+
return i;
14+
return -1;
15+
}
16+
17+
// Driver code
18+
int main(void)
19+
{
20+
int arr[] = { 2, 3, 4, 10, 40 };
21+
int x = 10;
22+
int n = sizeof(arr) / sizeof(arr[0]);
23+
24+
// Function call
25+
int result = search(arr, n, x);
26+
(result == -1)
27+
? cout << "Element is not present in array"
28+
: cout << "Element is present at index " << result;
29+
return 0;
30+
}

0 commit comments

Comments
 (0)