Skip to the task
Layout
Back to classic view

You are looking ahead. Your place is still task 1: Functions.

CREATETask 10 of 23 · step 5: RecursionCreate recursion.c
Create recursion.c in practice/05/

Two recursive functions with contracts: digit_sum with a depth, and factorial.

Checklist

  • Every task is one commit with a clear message.
  • Zero compiler warnings: the code builds with -Werror.
  • Once per topic you step through your code in the debugger (CLion, gdb).
  • Every function has preconditions and postconditions, and assert tests check them.
Details & hint
  • digit_sum(int n, int depth): the base case n < 10, otherwise n % 10 + digit_sum(n / 10, depth + 1). printf("%*s...", depth * 2, "", ...) writes depth * 2 spaces before the name.
  • factorial(int n): the base case n == 0 → 1, otherwise n * factorial(n - 1). The precondition is n >= 0 && n <= 20.

Stuck? %lld prints a long long. With %d the compiler reports format '%d' expects argument of type 'int'.

practice 05 CMakeLists.txt Code Diff +2
M · MODIFIED Green = add, blue = change, red mark = removed line.
add_executable(p05_grades main.c)
target_link_libraries(p05_grades PRIVATE course_input)
add_executable(p05_recursion recursion.c)