💩

programierds

Tests
C Intermediate 8 min

Ordering: position search in C

Build a C program that searches for a value in an array and reports the position where it appears, breaking the loop as soon as it is found.

lines

19

This test is a conceptual leap: it combines sequential search, a sentinel (-1), break and a final if/else.

Check the mental order:

  1. Prepare the data: array, counter, target value and position = -1.
  2. Loop with for.
  3. Inside the for, an if that checks the match, saves the position and does break.
  4. After the loop, an if/else that decides the message based on whether position changed.

Key detail: the final if/else goes outside the for, not inside. If you put it inside the loop, it runs every iteration and the program lies.

Drag the lines. You have time.

Tiempo estimado

08:00:00

Lineas: 19

C Ordenar codigo Intermediate 8 min 19 lineas

Ordering: position search in C

Build a C program that searches for a value in an array and reports the position where it appears, breaking the loop as soon as it is found.

Algoritmo a ordenar

Order the lines to search for a value in an array. Use position = -1 as a sentinel, break when found, and then report the result with an if/else.

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[6] = {8, 14, 21, 14, 35, 42};
  4. 4
        int i;
  5. 5
        int target = 14;
  6. 6
        int position = -1;
  7. 7
        for (i = 0; i < 6; i = i + 1) {
  8. 8
            if (numbers[i] == target) {
  9. 9
                position = i;
  10. 10
                break;
  11. 11
            }
  12. 12
        }
  13. 13
        if (position != -1) {
  14. 14
            printf("The value %d was found at position %d.\n", target, position);
  15. 15
        } else {
  16. 16
            printf("The value %d was not found.\n", target);
  17. 17
        }
  18. 18
        return 0;
  19. 19
    }

Ordena todas las lineas y despues toca Verificar ordenamiento.