And what would be the conditions that must be met during execution of the function? (Assertion)
I want to make sure that my assertion would describe what I know is true after running the ith loop.
int linearsearch(int arr[], int n, int target) {
for (int i = 0; i < n; i++) {
if (arr[i] == target) return i;
}
return -1;
}
This is just an iterative linear search function that returns the index of the target if the target is found and -1 otherwise.
Loop invariant for the problem of linear search must make a statement about all array elements that were previously searched, namely, that none of them is equal to target:
You need to prove several points about it:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With