Array Maximum in C

Level 3129 keystrokes

Finds the maximum value in an array using a loop.

C code

// Unsorted values to search
1int arr[] = {3, 7, 2, 9, 5};
// Take the first element as the current maximum
2int max = arr[0];
// Start at index 1, since index 0 is already in max
3for (int i = 1; i < 5; i++) {
// Replace max when a larger value appears (7, then 9)
4 if (arr[i] > max) max = arr[i];
5}
// Print the largest value: 9
6printf("%d", max);

Output

9
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C code and how often they appear.

  • ;× 6
  • ,× 5
  • [× 4
  • ]× 4
  • =× 4
  • (× 3
  • )× 3
  • "× 2
  • {× 2
  • }× 2

More C examples at level 3

← C typing practice