Even Odd Indices

🎯 Array Hurdle

Even Odd Indices

Difficulty: ⚫⚫◯◯

Tags: array, input, indices

Hurdle: Passing this question is sufficient to pass the arrays hurdle.

Description

Write a C program that reads integers from standard input until a 0 is entered.

The program should then print:

  1. First, all integers at even indices (index 0, 2, 4, ...)
  2. Then, all integers at odd indices (index 1, 3, 5, ...)

The final 0 should not be printed.

All numbers should be printed on a single line, separated by spaces.

Examples

Example 1

Input:
44
3
42
5
49
100
0

Output:
44 42 49 3 5 100

Explanation:

  • Index 0: 44 (even index)
  • Index 1: 3 (odd index)
  • Index 2: 42 (even index)
  • Index 3: 5 (odd index)
  • Index 4: 49 (even index)
  • Index 5: 100 (odd index)

Even indices first: 44, 42, 49 Then odd indices: 3, 5, 100 Result: 44 42 49 3 5 100

Example 2

Input:
1
2
3
4
5
0

Output:
1 3 5 2 4

Explanation:

  • Even indices (0, 2, 4): 1, 3, 5
  • Odd indices (1, 3): 2, 4 Result: 1 3 5 2 4

Example 3

Input:
42
0

Output:
42

Explanation: Only one number at index 0 (even). No odd indices.

Example 4

Input:
10
20
0

Output:
10 20

Explanation:

  • Index 0 (even): 10
  • Index 1 (odd): 20 Result: 10 20

Constraints

  • At least 1 integer will be entered before the final zero.
  • At most 10000 integers will be entered before the final zero.
  • No integer will be smaller than 1, except for the final zero.
  • Input will only contain integers, one per line.
  • Your program should produce exactly one line of output.
  • Do not use command-line arguments (argc, argv).
  • No error checking is necessary.

Hints

  • Use an array to store all the integers.
  • Use a loop to read integers until you see 0.
  • Use two separate loops to print: one for even indices, one for odd indices.
  • Be careful with spacing between numbers.

Testing

echo -e "44\n3\n42\n5\n49\n100\n0" | ./even-odd-indices
44 42 49 3 5 100

echo -e "1\n2\n3\n4\n5\n0" | ./even-odd-indices
1 3 5 2 4

echo -e "42\n0" | ./even-odd-indices
42

echo -e "10\n20\n0" | ./even-odd-indices
10 20
Code Editor
Output
Run your code to see output here...