Find Element Position in Array
C++
Easy
6 views
Problem Description
Search for a specific element and return its position (index). If not found, return -1. This teaches linear search concept.
Logic: Compare each element with search value
Official Solution
void question5_find_position() {
int arr[] = {12, 45, 67, 23, 89};
int size = 5;
int searchElement = 67;
int position = -1;
for(int i = 0; i < size; i++) {
if(arr[i] == searchElement) {
position = i;
break;
}
}
if(position != -1) {
cout << "Element found at index: " << position << endl;
} else {
cout << "Element not found" << endl;
}
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!