You are looking at lesson 5. Your place is still lesson 7, task 1: Modules.
hw/05: the functions gcd and digit_sum (recursively) and is_prime with contracts, and a program that simplifies a fraction.
Checklist
The folder hw/05 holds three files you fill in and one you don't change.
hw/05/numbers.cThe file numbers.h already lists three function prototypes with their contracts. You don't need to change it: topic 7 explains how .h files work. You write the function bodies in numbers.c:
int gcd(int a, int b): the greatest common divisor, recursively (Euclid's algorithm): gcd(a, 0) = a, otherwise gcd(a, b) = gcd(b, a % b). E.g. gcd(84, 36) = 12.int digit_sum(int n): the digit sum, recursively, as in the topic. digit_sum(472) = 13.int is_prime(int n): 1 if n is prime, otherwise 0. 0 and 1 are not prime. No recursion needed here: a loop is enough.Each function checks its precondition with an assert on its first line, as written in numbers.h.
hw/05/main.cThe program asks for the numerator and the denominator, prints the simplified fraction and, when it is more than 1, the mixed number. gcd helps with simplifying: divide both the numerator and the denominator by their greatest common divisor.
Numerator: 84
Denominator: 36
84/36 = 7/3
Mixed number: 2 1/3
The numerator must be non-negative, the denominator positive. Ask again after an invalid value:
Numerator: -5
Numerator must not be negative.
Numerator: 5
Denominator: 0
Denominator must be positive.
Denominator: 9
5/9 = 5/9
If the denominator is 1 after simplifying, print only the whole number: 48/12 = 4.
Input goes only through read_int from input.h.
Six tests:
hw05_functions: asserts that check your three functions (hw/05/unit_tests.c);hw05_precondition: gcd(-1, 5) must stop at your assert instead of returning something;hw05_fraction_1…3: the program's output with three inputs (hw/05/tests/);hw05_contracts: numbers.c has an assert( and no TODO left.ctest --preset debug -L hw05
While the functions aren't written, the first two tests end like this:
Start 11: hw05_functions
1/6 Test #11: hw05_functions ...................***Failed 0.04 sec
Assertion failed: gcd(84, 36) == 12, file C:/.../hw/05/unit_tests.c, line 14
Start 12: hw05_precondition
2/6 Test #12: hw05_precondition ................***Failed 0.08 sec
=== hw05_precondition: FAILED ===
The program did not stop, but it should have.
gcd(-1, 5) must stop at an assert that checks the precondition a >= 0.
--- output ---
gcd(-1, 5) returned 0: no assert stopped it.
unit_tests.c stops at the first assert that fails. Open it at line 14: you'll see which case it checks and which ones are still waiting.
Start with numbers.c: while gcd doesn't work, the fraction tests won't pass either.
Commit and push. Actions → Points: HW 5 (Fraction), 10 points when all six tests pass.
Each one ticks itself when you do it.
All six done. You know your way around.
It is laid out like CLion: the same panels in the same places. This tour shows each one, and you can reopen it any time with Tour.
Switches the left panel: Lesson plan, Project files, Changes. Click the active icon again to hide the panel.
In CLion: the tool window bar runs down the left edge, too.
This part is not on this task: it appears when a task needs it.
Every task in this lesson. Now marks your place. Clicking a task only lets you look; it never moves your place. Project files and Changes open here too.
In CLion: this spot holds the Project view (the file tree).
This part is not on this task: it appears when a task needs it.
Opens the file for your task by itself. A tab appears for each file the step opens. The breadcrumbs above the code show the folder.
In CLion: the same editor tabs and breadcrumbs.
This part is not on this task: it appears when a task needs it.
Diff shows what changed, like git diff: red is the old code, green the new.
In CLion: the diff view in the Commit window looks the same.
This part is not on this task: it appears when a task needs it.
Terminal for build commands, Database to see your tables. They open by themselves when a task needs them. Drag the top edge to resize.
In CLion: Terminal, and the Database tool window.
This part is not on this task: it appears when a task needs it.
What to do now, right above the code, so you never miss it. Numbered notes in the code explain the important lines. Details shows what you should see and a hint.
In CLion: no equivalent: this is your lecturer's guide.
This part is not on this task: it appears when a task needs it.
Done is the only button that moves you forward. Look back lets you review without losing your place.
This part is not on this task: it appears when a task needs it.
Guided, Focus code or Floating. You can drag the panel edges too. Your layout is remembered for next time.
In CLion: you can also rearrange and hide tool windows.
This part is not on this task: it appears when a task needs it.
Six small missions. Each one ticks itself when you do it, so you know you have found everything.