Drill

ProblemsC++ › inventory

What the shelf is worth

easyinventoryArraysMathC++

Month-end valuation multiplies what is on each shelf by its unit cost and adds it up.

stockValue(rows: list<Row>) → int

C++ needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

int stockValue(std::vector<Row> rows) {
    
}

Worked examples

CallResult
stockValue(std::vector<Row>{Row{3, 250}, Row{2, 100}})950
stockValue(std::vector<Row>{Row{-4, 500}, Row{1, 500}})500
stockValue(std::vector<Row>{Row{0, 900}})0
stockValue(std::vector<Row>{})0

Hint

One pass, one accumulator, one guard.

Reference solution in C++
int stockValue(std::vector<Row> rows) {
    int total = 0;
    for (const auto& r : rows) if (r.qty > 0) total += r.qty * r.unitCost;
    return total;
}

The same problem in another language

More inventory problems in C++