Skip to the task
Layout
Back to classic view

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

HOMEWORKTask 22 of 23 · step 9: Homework: the fractionHomework: the fraction
Homework: the fraction in practice/02/

hw/05: the functions gcd and digit_sum (recursively) and is_prime with contracts, and a program that simplifies a fraction.

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

The folder hw/05 holds three files you fill in and one you don't change.

The functions: hw/05/numbers.c

The 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.

The fraction: hw/05/main.c

The 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.

Tests

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.

Handing in

Commit and push. Actions → Points: HW 5 (Fraction), 10 points when all six tests pass.

practice 02 main.c Code Diff
UNCHANGED This step does not change this file.
#include <limits.h>
#include <stdio.h>
#include "input.h"
int main(void) {
const int current_year = 2026;
int study_year = 1;
char group = 'A';
int birth_year = read_int("Year of birth: ");
int g1 = read_int("Grade 1: ");
int g2 = read_int("Grade 2: ");
int g3 = read_int("Grade 3: ");
double average = (g1 + g2 + g3) / 3.0;
int age = current_year - birth_year;
printf("Year of study: %d\n", study_year);
printf("Group: PI25%c\n", group);
printf("Age: %d years\n", age);
printf("Average: %.2f\n", average);
printf("In memory: int %zu B, double %zu B, char %zu B\n", sizeof study_year, sizeof average,
sizeof group);
printf("\n");
printf("Largest int: %d\n", INT_MAX);
long long century = 100LL * 365 * 24 * 60 * 60;
printf("100 years: %lld s\n", century);
printf("0.1 + 0.2 = %.17f\n", 0.1 + 0.2);
return 0;
}