C — Inventory project with boundary tests
This C inventory function receives an array of events and its length. It returns both the remaining stock and the number of rejected events in a struct.
A simple example
Trace one stock change
- Start with 3 in stock
- Do this Take 1 away
- Result 2
#include <stdio.h>
int main(void) {
int stock = 3;
stock -= 1;
printf("%d\n", stock);
}
2
Build an inventory event processor
The function takes a pointer and a count. For an empty input the count is zero, so the null pointer is never dereferenced.
The first delivery brings five units and a shipment takes two. A request to ship nine and an unknown event are both rejected, so stock stays at three. The final delivery adds four and closes the ledger at seven.
Run the example
C17. Run these commands from the directory containing the example. Use a Unix-style shell.
cc -std=c17 lesson.c -o lesson
./lesson
#include <stdio.h>
#include <assert.h>
enum Kind { RECEIVE, SHIP, UNKNOWN };
struct Event { enum Kind kind; int units; };
struct Result { int stock; int rejected; };
struct Result process(const struct Event *events, size_t count) {
struct Result result = {0, 0};
for (size_t i = 0; i < count; ++i) {
struct Event event = events[i];
if (event.kind == RECEIVE && event.units > 0) result.stock += event.units;
else if (event.kind == SHIP && event.units > 0 && event.units <= result.stock)
result.stock -= event.units;
else ++result.rejected;
}
return result;
}
int main(void) {
struct Event events[] = {{RECEIVE, 5}, {SHIP, 2}, {SHIP, 9}, {UNKNOWN, 1}, {RECEIVE, 4}};
struct Event exact[] = {{RECEIVE, 2}, {SHIP, 2}};
struct Result empty = process(NULL, 0);
struct Result drained = process(exact, 2);
struct Result result = process(events, sizeof events / sizeof events[0]);
assert(empty.stock == 0 && empty.rejected == 0);
assert(drained.stock == 0 && drained.rejected == 0);
assert(result.stock == 7 && result.rejected == 2);
printf("%d %d\n", result.stock, result.rejected);
return 0;
}
7 2
Start with zero units. Receive 5 and ship 2 to leave 3. Reject the shipment of 9 and the unknown event. Finally receive 4. The two output numbers are the ending stock of 7 and the two rejected events.
Buy supplies for the next shift
A breakfast kiosk has 28 cups left, expects 64 orders tomorrow and keeps four spare cups for spills. The supervisor subtracts usable stock from the target and buys sealed sleeves of ten, rounding the order up to a whole sleeve.
#include <stdio.h>
int main(void) {
int orders = 64, spare = 4, on_hand = 28;
int shortage = orders + spare > on_hand ? orders + spare - on_hand : 0;
printf("%d sleeves", (shortage + 9) / 10);
return 0;
}
4 sleeves
The target is 68 cups. After counting the 28 on hand, the kiosk needs 40 more, exactly four sleeves of ten.
Try it yourself
Append a shipment of 7, followed by another shipment of 1.
Check your answer
The first shipment exactly empties stock. The next one is rejected. Update the expected result to 0 units and 3 rejections. Keep the existing checks for the empty event list and exact depletion.