forked from waboke/solutions_to_pastquestions_2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpositionoftargetvalueinarray.cpp
39 lines (31 loc) · 993 Bytes
/
positionoftargetvalueinarray.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
#include <iostream>
using namespace std;
// Function to perform linear search
int linearSearch(int* arr, int size, int target) {
for (int i = 0; i < size; ++i) {
if (*(arr + i) == target) { // Compare the target value
return i; // Return the index
}
}
return -1; // Return -1 if target is not found
}
int main() {
int n, target;
cout << "Enter the number of elements in the array: ";
cin >> n;
int* arr = new int[n]; // Dynamically allocate array
cout << "Enter the elements of the array:" << endl;
for (int i = 0; i < n; ++i) {
cin >> *(arr + i);
}
cout << "Enter the target value to search for: ";
cin >> target;
int position = linearSearch(arr, n, target);
if (position != -1) {
cout << "Target value found at index: " << position << endl;
} else {
cout << "Target value not found in the array." << endl;
}
delete[] arr; // Free allocated memory
return 0;
}