Skip to content

Commit 8ff3531

Browse files
Update LinearSearchRecursive.cpp
update with formatting and small changes
1 parent f63e884 commit 8ff3531

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

src/cpp/LinearSearchRecursive.cpp

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,36 @@
11
#include <iostream>
2-
#include <vector>
3-
using namespace std;
42

5-
bool linearSearchRecursive(int arr[], int n, int index, int target){
6-
if(index >= n){
7-
return false;
8-
}
3+
using namespace std;
94

10-
if(arr[index] == target){
11-
return true;
12-
}
5+
int linearSearchRecursive(int arr[], int n, int index, int target) {
6+
if (index >= n) {
7+
return -1;
8+
}
9+
10+
if (arr[index] == target) {
11+
return index;
12+
}
1313

14-
return linearSearchRecursive(arr,n, index+1, target);
14+
return linearSearchRecursive(arr, n, index + 1, target);
1515
}
1616

17-
int main(){
18-
int n;
19-
cout<< "Enter The size of array : ";
20-
cin>> n;
21-
int arr[n];
22-
cout<<"Enter the element of array : "<<endl;
23-
for(int i = 0;i<n;i++){
24-
cin>>arr[i];
25-
}
17+
int main() {
18+
int n;
19+
cout << "Enter The size of array : ";
20+
cin >> n;
21+
int arr[n];
22+
cout << "Enter the element of array : " << endl;
23+
for (int i = 0; i < n; i++) {
24+
cin >> arr[i];
25+
}
2626

27-
int target;
28-
cout<<"Enter the element you need to search in the array : ";
29-
cin>>target;
30-
bool answer = linearSearchRecursive(arr, n, 0, target);
31-
if(answer){
32-
cout<<"Found"<<endl;
33-
}else{
34-
cout<<"Not Found"<<endl;
35-
}
36-
}
27+
int target;
28+
cout << "Enter the element you need to search in the array : ";
29+
cin >> target;
30+
int answer = linearSearchRecursive(arr, n, 0, target);
31+
if (answer >= 0) {
32+
cout << "Target Element Found on index : " << answer << endl;
33+
} else {
34+
cout << "Target Element Not Found" << endl;
35+
}
36+
}

0 commit comments

Comments
 (0)