- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech
Control Flow and Reusable Logic in C
A regional freight depot routes incoming orders through a small C console. Each order arrives as a short command code and a priority letter; the console must classify it, compute its shipping fee, simulate how it stacks onto pallets without ever looping forever on a bad configuration, and reorder the shipment queue when two orders need to swap priority slots. The starter project has the data structures and a skeleton main loop in place; you will fill in the decision logic, the loops, the extracted helper functions, and the pointer-based swap that the console depends on.
Lab Info
Table of Contents
-
Challenge
Step 1: Tour the freight depot's dispatch console
The freight depot routes every incoming order through a small C console. Each order arrives as two pieces of text: a short command code such as
"PICKUP"and a single priority letter such as'A'. Before the console can do anything else, it has to decide what those two values mean, simulate how the order's crates stack onto pallets, cost the shipment, and sometimes reorder two entries in the shipment queue. Every one of those steps is a control-flow or function-design decision you will make by hand over the next four steps. The starter project already has the console's data flow sketched out:src/classifier.candsrc/classifier.hhold the command and priority classifiers,src/pallet.cholds the pallet-stacking simulation,src/fees.cholds the fee and crate-count math,src/swap.cholds the priority swap,src/queue.cholds the shipment queue reorder, andsrc/report.cties them all together. Every header already declares the functions you'll implement; you'll only ever edit the matching.cfile. Each stub function body currently holds a// TODOcomment naming the task that finishes it. Step 2 fills in the classifiers: an if/else chain for the multi-character command codes, and a switch statement for the single-character priority letters, because those are the C constructs each shape actually calls for. Step 3 simulates pallet stacking with nested loops, then adds the safety counters that stop those loops from running forever when a pallet is misconfigured. Step 4 pulls repeated fee and crate arithmetic out ofmaininto typed functions, then writes a swap function that looks reasonable but silently fails because its parameters are passed by value. Step 5 rewrites that swap with pointer parameters so it actually works, uses it to reorder the shipment queue, and assembles the final per-order dispatch report. Each task changes one function's behaviour, and the change is checked by running the depot console's logic against fixed inputs and inspecting what it returns. Read the TODO and Hint comments left in the stub before you start editing -- they name the exact function and the technique the task expects, not the finished code.info> If you get stuck, you can refer to the provided solution code for each task, available in the
solutionfolder. -
Challenge
Step 2: Classify incoming orders with if/else and switch
Every order that reaches the depot starts as a short command code and a single priority letter. The dispatch console has to decide, deterministically, what those two pieces of text mean before anything else can happen. C gives you two different tools for that decision: an if/else chain for comparing strings with
strcmp, and a switch statement for branching on a single character or small integer. Getting the choice right matters -- switch cannot compare strings directly, and an if/else chain of single-character comparisons is harder to read than the switch it should have been.src/classifier.cis where both classifiers live, and right now they are just TODO markers. With commands recognized as numbers, the console needs the same treatment for priority letters -- but a priority letter is a single character, which is exactly the shape switch was built for. Two classifiers, two ways of returning a number: now the console needs one function that calls both and turns them into the single action code everything else in the dispatch loop will branch on. -
Challenge
Step 3: Simulate pallet stacking with bounded nested loops
Orders don't just get classified -- they get physically stacked onto pallets before they leave the depot. Simulating that stacking means nested loops: an outer loop over pallets, an inner loop over the crates going onto each one. Nested loops are also where C programs most often go wrong, because nothing stops a loop from running forever except the condition you write yourself.
src/pallet.cstarts with a straightforward stacking simulation before it gets a safety net.count_stacked_cratesworks fine as long aspalletCapacityis a sensible positive number. A misconfigured order -- capacity zero, or negative -- would make the inner loop's crate count never reach zero, and the outer loop would never stop. Before that happens in production,stack_with_limitadds an iteration counter that guarantees the loop always terminates. The depot's own dispatch cycle has the identical shape: keep processing batches of orders until none remain, but never past a fixed number of cycles, in caseordersRemainingnever actually reaches zero. -
Challenge
Step 4: Extract shared calculations into functions and expose the swap bug
mainhas been recomputing the same shipping-fee formula and the same crate-count arithmetic in more than one place. Pulling that logic into named functions with typed parameters and a return value is what makes it trustworthy: one definition, one place to fix, and a value the caller can check.src/fees.cis where both extracted calculations will live.count_required_crateshas the same shape ascalculate_shipping_fee: read parameters in, compute locally, return a typed result. Together they replace every scattered fee-and-crate calculationmainused to do inline. Not every function that changes two variables actually changes the caller's variables. When you pass anintby value, the function receives its own copy -- it can swap those copies all it wants, and the caller's originals are untouched when the function returns.src/swap.c'sattempt_swap_prioritieswill let you see that for yourself before you fix it in the next step. -
Challenge
Step 5: Fix the swap with pointers and wire up the dispatch report
The fix for
attempt_swap_prioritiesis to stop passing copies and start passing addresses. A pointer parameter lets a function reach back into the caller's own memory and change the value that lives there, which is exactly what reordering the shipment queue requires.src/swap.c'sswap_prioritiestakesint*parameters instead ofint. With a swap that actually works,src/queue.c'sreorder_queuecan use it to exchange two entries in the shipment queue array in place, using the&operator to handswap_prioritiesthe addresses it needs. Every helper from the last four steps -- classification, fee and crate math, pallet stacking, and the queue swap -- comes together in one place: the report the console builds for each order.src/report.c'sbuild_dispatch_reportcalls each of them in turn and formats the result. -
Challenge
Step 6: Run the application
Every task is done, and each one was graded the moment you completed it. What you have not done yet is watch the whole application run at once -- which is the only place the pieces you wrote separately become a single working program.
Start the application
Open the Terminal tab and run:
make app && ./build/release/lab.outThe program runs in the terminal and prints its output there. Every behaviour it shows is code you wrote in the previous steps, so read the output against what each task asked for -- that is the whole lab, working as one program. ### Run the checks yourself
The lab ran each task's test for you as you went, but they are ordinary tests and you can run them whenever you like. The whole suite:
make testOr one task at a time:
bash runTest.sh test_task_2_1Each failure names the task it belongs to, so the output tells you which step to go back to rather than which line to stare at. Two things worth trying before you finish: break something on purpose and watch the matching check fail, then put it back. Knowing what a failure looks like is worth as much as knowing what a pass looks like, and it costs you nothing here.
When you are done, everything you wrote is still in the editor tabs -- the lab is yours to keep reading.
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.