Run ❯
Get your
own
website
❯
Run Code
Ctrl+Alt+R
Change Orientation
Ctrl+Alt+O
Change Theme
Ctrl+Alt+D
Go to Spaces
Ctrl+Alt+P
#include
// Returns the index of the value, or -1 if it is not found int linearSearch(int arr[], int length, int value) { for (int i = 0; i < length; i++) { if (arr[i] == value) { return i; } } return -1; } int main() { int numbers[] = {4, 9, 2, 7, 5, 8}; int length = sizeof(numbers) / sizeof(numbers[0]); int index = linearSearch(numbers, length, 7); if (index != -1) { printf("Found 7 at index %d\n", index); } else { printf("7 was not found\n"); } return 0; }
Found 7 at index 3