Complete the code to return the index of the target element in the array or -1 if not found.
int linearSearch(int arr[], int n, int target) {
for (int i = 0; i < n; i++) {
if (arr[i] == [1]) {
return i;
}
}
return -1;
}i instead of the target value in the comparison.arr[i] to itself instead of the target.The code compares each element arr[i] with the target to find a match.
Complete the code to loop through the array correctly for linear search.
int linearSearch(int arr[], int n, int target) {
for (int i = 0; i < [1]; i++) {
if (arr[i] == target) {
return i;
}
}
return -1;
}The loop should run from 0 to n - 1 to cover all elements.
Fix the error in the return statement to indicate the target was not found.
int linearSearch(int arr[], int n, int target) {
for (int i = 0; i < n; i++) {
if (arr[i] == target) {
return i;
}
}
return [1];
}i after the loop ends.Returning -1 indicates the target was not found in the array.
Fill both blanks to create a linear search that returns true if the target is found, false otherwise.
bool linearSearch(int arr[], int n, int target) {
for (int i = 0; i < [1]; i++) {
if (arr[i] == [2]) {
return true;
}
}
return false;
}The loop runs up to n, and compares each element to target.
Fill all three blanks to create a linear search that returns the index or -1 if not found, using a while loop.
int linearSearch(int arr[], int n, int target) {
int i = 0;
while (i [1] n) {
if (arr[i] == [2]) {
return [3];
}
i++;
}
return -1;
}The while loop runs while i < n, compares arr[i] to target, and returns i if found.