Implement find_max to write the greatest array value into out. Return 1 on success and 0 when the array is empty, the pointer is null, or out is null.
Requirements
Do not read values[0] before checking length
Leave out unchanged on failure
Use size_t for length
Example
Input
{ -4, 12, 0 }, 3
Expected output
success, 12
Show a hint
Write down the input contract and the failure cases before coding.
local runtime
RUNTIME OUTPUT
Use your C runtime locally, then compare with solution.c.
Use your local runtime, then review the reference solution
LEARN FROM THE SOLUTION
Why the solution works.
The first element is only safe to use after the empty case has been rejected. Computing into a local maximum and assigning out at the end means callers never receive a partial or invented result when the input contract fails.
What this exercise teaches
Array traversal
Output parameters
Preconditions
A PRACTICAL PLAN
Work through Find the maximum safely with intent.
Translate the contract.Turn the requirements into a short checklist before editing your-solution.c.
Use the example as evidence.Predict the result for the supplied input, then add one boundary case such as an empty value, a limit, or unexpected input.
Review the implementation.Compare your choices against solution.c only after a real attempt.
EXERCISE FAQ
Before you move on.
What does “Find the maximum safely” teach?
This beginner C exercise focuses on Array traversal, Output parameters, Preconditions. Its requirements define the exact behavior to implement before you write code.
How should I validate this C solution?
Start with the displayed example input and expected output, then test a boundary case suggested by the requirements. Write and compile the starter in your local C environment, exercise the example and edge cases, then compare your approach with solution.c.
When should I open the reference solution?
Attempt Find the maximum safely first. Then open the read-only solution file to compare the contract, edge-case handling, and implementation choices—not simply to copy the final code.