#include <stdio.h>
#include <stdint.h>
int linear_search(uint8_t *arr, uint8_t n, uint8_t key) {
for (uint8_t i = 0; i < n; i++) {
if (arr[i] == key) {
return i; // Found at index i
}
}
return -1; // Not found
}
int main() {
uint8_t n, key;
scanf("%hhu", &n);
uint8_t arr[100];
for (uint8_t i = 0; i < n; i++) {
scanf("%hhu", &arr[i]);
}
scanf("%hhu", &key);
int index = linear_search(arr, n, key);
printf("%d", index);
return 0;
}Here’s a clear and embedded-style solving algorithm for performing a linear search on a uint8_t array:
n: number of elements in the arrayarr[]: array of n unsigned 8-bit integerskey: the value to search forindex = -1 to represent "not found"i from 0 to n - 1:arr[i] == key:index = iindex (either the found index or -1)Input:
n = 5
arr = [10, 20, 30, 40, 50]
key = 30
Output:
2
Input
5 10 20 30 40 50 30
Expected Output
2