logo

Improvements to finding an element in an array

#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char* argv[]) { int arr[5] = {2, 7, 8, 5, 7}; int n = 5; int i; for (i = 0; i < n; i++) { if (arr[i] == 7) { break; } } if (i >= n) { printf("Nothing found, bummer\n"); } else { printf("Found element 7 at index %d\n", i); } return 0; }
Course thumb

The C programming language made simple