ctf Lesson 7 24 min read

CTF: Arrays

12 challenges — Week 5

Arrays Challenges

12 challenges Week 5 Week 5 (Arrays, Strings, and Command-Line Arguments)

Array manipulation exercises covering single and multi-dimensional arrays, string handling, array traversal patterns, tallying/counting algorithms, and integration with file I/O. Emphasizes practical array patterns used in systems programming and data processing.


Difficulty Breakdown

Level Count
Beginner 5
Intermediate 6
Advanced 1

Challenges

1. Array Mystery7

Difficulty: Beginner Time: ~3 min Type: trace execution

What element values are stored in array a after the following code runs? int a[7] = {1, 7, 5, 6, 4, 14, 11}; for (int i = 0; i < 6; i++) { if (a[i] > a[i + 1]) { a[i + 1] = a[i + 1] * 2; } }

Concepts: array indexing and element access, loop bounds and off-by-one errors, conditional modification of array elements, order of operations (comparison before modification), tracing execution step-by-step through a loop

Practice on CodeStepByStep


2. Count Short Strings

Difficulty: Beginner Time: ~5 min Type: function implementation

Write count_short_strings that accepts array of strings (char*[]) and size, returns the count of strings with length <= 3. Use strlen() to determine string length.

Concepts: working with arrays of strings (char*[]), strlen() function from string.h, comparison operators with function return values, loop iteration over array size parameter, counting pattern with accumulated variable

Practice on CodeStepByStep


3. Find Pairs

Difficulty: Intermediate Time: ~8 min Type: function implementation

Write find_pairs that accepts two arrays of doubles and their sizes, returns the number of pairs (a, b) where a is from the first array, b is from the second array, and a < b.

Concepts: nested loops (double loop pattern), working with multiple arrays and size parameters, floating-point comparison, counting pattern with nested iterations, combinatorial logic (all pairs from two sets)

Practice on CodeStepByStep


4. Max Row

Difficulty: Intermediate Time: ~10 min Type: function implementation

Write max_row that accepts rows, cols, and a 2-D array, returns the index of the row with the greatest sum. In case of a tie, return the smallest index.

Concepts: 2-D array declaration and access (list[i][j]), multi-dimensional array iteration with nested loops, row-wise summation algorithm, finding maximum and tracking index, tie-breaking rule (smallest index wins)

Practice on CodeStepByStep


5. Most Frequent Digit

Difficulty: Intermediate Time: ~8 min Type: function implementation

Write most_frequent_digit that takes an integer (can be positive or zero), returns the digit (0-9) that occurs most frequently. If there is a tie, return the lower digit value.

Concepts: using an array to tally/count occurrences (frequency histogram), digit extraction using modulo (n % 10) and integer division (n / 10), while loop for extracting digits from a number, finding maximum count and returning the index, tie-breaking rule (return smallest index)

Practice on CodeStepByStep


6. Print Reverse

Difficulty: Beginner Time: ~5 min Type: function implementation

Write print_reverse that takes an array of strings and its length, prints the strings in reverse order (from end to beginning), with each string separated by a single space, and a newline at the end.

Concepts: iterating arrays in reverse (start at len-1, decrement to 0), array of strings (char*[]), printf() with %s format specifier, loop control (i–, i >= 0), edge case: empty array (len=0)

Practice on CodeStepByStep


7. Reverse

Difficulty: Intermediate Time: ~8 min Type: function implementation

Write reverse that accepts an array of integers and its length, modifies the array in-place to reverse the order of elements.

Concepts: in-place array reversal using two-pointer technique, loop bounds: only iterate to len/2 (not the full array), swapping elements using a temporary variable, pointer parameter passing (modifying array in caller), integer division (len/2)

Practice on CodeStepByStep


8. Section Attendance

Difficulty: Advanced Time: ~30 min Type: complete program

Read from a file sections.txt. Each line represents a section with 9 weeks × 5 students = 45 characters. For each character: ‘a’ = absent (+0 points), ‘n’ = attended (+1 point), ‘y’ = attended with problems completed (+3 points). Sum points per student (modulo indexing for 5 students), cap at 20 points maximum. Print each student’s capped score.

Concepts: file I/O: fopen(), fgets(), fclose(), character-by-character processing of file input, modulo arithmetic for cycling through students (char_index % 5), tallying/accumulation pattern across file lines, conditional logic (a/n/y mapping to points)

Practice on CodeStepByStep


9. Swap

Difficulty: Beginner Time: ~3 min Type: function implementation

Write swap that accepts an array of integers and two indexes, swaps the elements at those two indexes in-place.

Concepts: temporary variable for swapping, pointer parameter passing (in-place modification), array indexing with arbitrary indexes, simple three-step swap algorithm

Practice on CodeStepByStep


10. Swap All

Difficulty: Beginner Time: ~5 min Type: function implementation

Write swap_all that accepts two arrays of integers and their length, swaps the entire contents of the two arrays in-place (element-by-element).

Concepts: element-by-element swapping across two arrays, looping through arrays in parallel (same index in both), temporary variable for swapping, in-place modification of multiple pointer parameters, loop bounds (len parameter controls iteration)

Practice on CodeStepByStep


11. Switch Pairs

Difficulty: Intermediate Time: ~8 min Type: function implementation

Write switch_pairs that accepts an array of strings and its length, switches the order of consecutive pairs. If the array has an odd length, the last element remains in place.

Concepts: loop increment by 2 (i+=2) for pair processing, condition i < len-1 to avoid out-of-bounds access, swapping pairs of elements, string pointer swapping (char**), handling odd-length arrays gracefully

Practice on CodeStepByStep


12. Temperatures

Difficulty: Intermediate Time: ~20 min Type: complete program

Write a complete program that prompts the user for the number of days, reads that many temperature values, computes and prints the average temperature, then prints the count of days with temperature above the average.

Concepts: Variable Length Arrays (VLA): double temps[n], user input with scanf(), two-pass algorithm (first pass: sum for average, second pass: count above average), loop for summation and comparison, floating-point arithmetic and division

Practice on CodeStepByStep