💩

programierds

Tests
C Beginner 6 min

Ordering: largest value in an array in C

Build a C program that walks through an array and prints the largest value found.

lines

13

This program introduces a fundamental idea: to find the largest, start by assuming the first element is the largest, then compare with the rest.

That is why we do not declare largest = 0 or a random value: we use a value that is actually in the array.

Algorithm steps:

  1. Include <stdio.h>.
  2. Open main.
  3. Declare the array and the counter.
  4. Initialize largest with numbers[0].
  5. Loop from i = 1.
  6. Compare and update when needed.
  7. Print and return 0.

Pay attention to the order: an error declaring largest before the array breaks everything.

Tiempo estimado

06:00:00

Lineas: 13

C Ordenar codigo Beginner 6 min 13 lineas

Ordering: largest value in an array in C

Build a C program that walks through an array and prints the largest value found.

Algoritmo a ordenar

Order the lines to find and print the largest value in an integer array. The comparison starts from the second element.

Arrastra las lineas o usa los botones de flecha para reordenarlas. Cuando creas que estan en el orden correcto, tocas Verificar ordenamiento.

Lineas mezcladas c
  1. 1
    #include <stdio.h>
  2. 2
    int main() {
  3. 3
        int numbers[5] = {12, 7, 25, 18, 9};
  4. 4
        int i;
  5. 5
        int largest = numbers[0];
  6. 6
        for (i = 1; i < 5; i = i + 1) {
  7. 7
            if (numbers[i] > largest) {
  8. 8
                largest = numbers[i];
  9. 9
            }
  10. 10
        }
  11. 11
        printf("The largest value is: %d\n", largest);
  12. 12
        return 0;
  13. 13
    }

Ordena todas las lineas y despues toca Verificar ordenamiento.