Exam Rank 03 42 ✰ [Working]

In the 42 Network’s peer-to-peer curriculum, Exam Rank 03 is a significant milestone that tests a student’s mastery of the C programming language and foundational systems concepts. This 4-hour exam typically occurs during the "Common Core" phase and serves as a gatekeeper to more advanced ranks. Exam Structure & Core Concepts

The exam generally presents a single problem randomly selected from two main categories. You must validate the assigned question with a 100% score to pass the rank.

Custom Functions (ft_printf or get_next_line): In many versions of the curriculum, students are asked to recreate standard library functions. This requires handling file descriptors, memory allocation with malloc, and variadic arguments using va_start and va_arg.

Geometric Rendering (micro_paint and mini_paint): Some newer iterations of the exam focus on reading operation files to draw shapes (rectangles or circles) into a terminal buffer, emphasizing file parsing and logical 2D rendering.

Backtracking & Algorithms: Advanced variations might include algorithmic challenges like BSQ (Biggest Square), which requires optimizing how you search for patterns within a grid. Essential Preparation Strategies

Passing requires more than just knowing how to code; it requires speed and precision under pressure. Exam Rank 03 42

Simulated Practice: Use tools like the 42_examshell or JCluzet's 42_EXAM trainer to replicate the real exam environment, which includes a strict terminal-based interface and no internet access.

Single-File Constraints: Unlike standard projects, exam solutions are often required to be in a single .c file. Practice organizing your helper functions within one file to avoid compilation errors.

Memory Management: Since Norminette (the school’s code style checker) is often disabled during this exam, students sometimes overlook leaks. However, the automated "bot" grader will still fail you for memory leaks or segmentation faults.

Compiler Flags: Always test your code with -Wall -Wextra -Werror. These are the standard flags used by the grading system to ensure code quality. Student Resources

This is a detailed guide and walk-through for Exam Rank 03 at 42 Network. In the 42 Network’s peer-to-peer curriculum, Exam Rank

Status: This is the final rank for the "Beginner" cycle (Piscine style). Passing this unlocks the linear curriculum (Circle 4), where projects become much larger and collaborative.


3. The Grading Experience (The "Moulinette")

The exam system at 42 (often called "Moulinette" for projects, though exams have a specific trainer) is unforgiving.

Drawing rules:

🚀 Walkthrough: ft_printf

This is the "Boss Fight" of Rank 03. If you get this, you must implement a simplified version of the standard printf.

The Prototype:

int ft_printf(const char *format, ...);

Mandatory Specifiers: You usually need to handle: %c, %s, %p, %d, %i, %u, %x, %X, %%. Strictness: If the subject says "write a function,"

Key Concepts Needed:

  1. Variadic Functions: Using <stdarg.h> macros (va_start, va_arg, va_end).
  2. Return Value: The function returns the total count of characters printed.
  3. Buffer Flushing: Since you can't use printf or putchar, you must use write.

Suggested Logic Structure:

  1. Iterate through the format string:

    • If the character is not %, write it and increment counter.
    • If the character is %, look at the next character and branch logic.
  2. Helper Functions: You need a function to print strings (%s), characters (%c), and numbers. Printing numbers (especially hex) requires a recursive or division-based helper function.

Example Skeleton:

#include <stdarg.h>
#include <unistd.h>
// Helper to write a single char
void ft_putchar(char c, int *count)
write(1, &c, 1);
    *count += 1;
// Helper to write a string
void ft_putstr(char *str, int *count)
if (!str)
        str = "(null)";
    while (*str)
ft_putchar(*str, count);
        str++;
// Helper for base conversion (Hex and Decimal)
void ft_putnbr_base(unsigned long long n, int base, char *chars, int *count)
if (n >= (unsigned long long)base)
        ft_putnbr_base(n / base, base, chars, count);
    ft_putchar(chars[n % base], count);
// Helper for signed integers (d, i)
void ft_putnbr(int n, int *count)
if (n == -2147483648)
ft_putstr("-2147483648", count);
        return;
if (n < 0)
ft_putchar('-', count);
        n = -n;
ft_putnbr_base((unsigned long long)n, 10, "0123456789", count);
// Main function
int ft_printf(const char *format, ...)
va_list args;
    int count = 0;
va_start(args, format);
    while (*format)
if (*format == '%')
         *format == 'i') ft_putnbr(va_arg(args, int), &count);
            else if (*format == 'u') ft_putnbr_base(va_arg(args, unsigned int), 10, "0123456789", &count);
            else if (*format == 'x') ft_putnbr_base(va_arg(args, unsigned int), 16, "0123456789abcdef", &count);
            else if (*format == 'X') ft_putnbr_base(va_arg(args, unsigned int), 16, "0123456789ABCDEF", &count);
            else if (*format == 'p')
ft_putstr("0x", &count);
                ft_putnbr_base((unsigned long long)va_arg(args, void *), 16, "0123456789abcdef", &count);
else if (*format == '%') ft_putchar('%', &count);
else
            ft_putchar(*format, &count);
        format++;
va_end(args);
    return (count);

Common Pitfalls:


The Generic Strategy for Both

  1. Parse the first line: The first line of the scene file always contains:
    • Background width (int) max_x
    • Background height (int) max_y
    • Background character (char)
  2. Allocate a 2D array (or 1D flat array) of characters representing the canvas. Fill it with the background character.
  3. Read the rest of the file, line by line. Each subsequent line contains:
    • Shape type ('r' for rect in micro; 'c' for circle in mini)
    • X coordinate (float)
    • Y coordinate (float)
    • Width (float) / Radius (float for mini)
    • Height (float) / (Not applicable for mini)
    • Fill character ('R' for rectangle outline, 'r' for filled; similarly 'C' / 'c' for circle)
    • Drawing character ('X' or '#')
  4. For each shape, determine which pixels to draw. This is the hardest logic.
    • Rectangle: A point is on the border if its x is between X and X+Width AND y is between Y and Y+Height, AND it lies exactly on the perimeter.
    • Circle: A point is inside the circle if sqrt((x - X)^2 + (y - Y)^2) <= Radius. A point is on the border if the absolute difference between distance and radius is less than a threshold (usually 1.0). For filled circles, draw if inside.
  5. Print the canvas row by row to stdout.