# Exercises

### Exercises

1. Write a function `max3` that returns the largest of three integers.

2. Write a function `minSlice` that returns the smallest element of a slice of integers.

3. Write a function `count` that counts how many times a byte appears in a string.

4. Write a function `reverse` that reverses a slice in place using pointers.

5. Write a module `temperature.zig` with public functions:

```text
celsiusToFahrenheit
fahrenheitToCelsius
```

6. Write a function:

```zig
fn swap(a: *i32, b: *i32) void
```

that exchanges two integers.

7. Write a function that returns both quotient and remainder using a struct:

```zig
const Result = struct {
    quotient: i32,
    remainder: i32,
};
```

8. Write a function `contains` that checks whether a slice contains a given value.

9. Write a program split across files:

```text
main.zig
math.zig
stats.zig
```

where `main.zig` imports the others.

10. Write a module with one public function and several private helper functions.

11. Write a function that modifies every element of a slice through a pointer.

12. What is the difference between:

```zig
i32
```

and:

```zig
*i32
```

when used as function parameters?

13. Explain why this function is unsafe:

```zig
fn bad() *i32 {
    var x: i32 = 0;
    return &x;
}
```

14. Write a function that accepts:

```zig
[]const u8
```

and prints the string.

15. Write a function that returns an optional integer:

```zig
?i32
```

Return `null` if division by zero occurs.

16. Organize a small project into modules with clearly separated responsibilities.

17. Write a function `map` that applies another function to every element of a slice.

18. Write a function that computes the average of a slice of floating-point values.

19. Write a module `vector.zig` containing:

```text
Vec2
add
sub
scale
dot
```

20. Rewrite a long function into smaller functions with clearer names.

21. Design a public interface for a parser module. Decide which declarations should remain private.

22. Write a function that receives a struct by pointer and modifies one field.

23. Explain why explicit imports help avoid namespace collisions.

24. Write a small command-line calculator using multiple source files.

