Kodewick
Start learning →
Mini challenge←0 / 70

Grocery run

#070Mini challengeArraysConditions6 min

You go shopping with money, buying down the list in order: if you can afford an item you buy it (and have less money left); if not, you skip it and look at the next. Write items_bought(prices, n, money) to return how many items you got.

Examples
items_bought({5, 12, 3, 8}, 4, 20)→3
items_bought({}, 0, 10)→0
The functionint items_bought(int prices[], int n, int money)
main.c
int items_bought(int prices[], int n, int money) {
    int bought = 0;
    for (int i = 0; i < n; i++) {
        // your code here: if you can afford it, take the price off money and add 1 to bought
    }
    return bought;
}
Ctrl + Enter runs too
Result

Run tries the examples. When they look right, Submit.