Kodewick
Start learning →
Basic←→0 / 70

Cheapest cake

#061BasicStructsArrays5 min

Each cake in the counter is a struct Cake: name and price. Write cheapest(cakes, n) to return the name of the cheapest one. There's at least 1; if two cost the same, return the earlier one.

Examples
cheapest({{"cheesecake", 28}, {"brownie", 12}, {"tiramisu", 30}}, 3)→'brownie'
cheapest({{"donut", 6}}, 1)→'donut'
The functionconst char *cheapest(struct Cake cakes[], int n)
main.c
struct Cake {
    const char *name;
    int price;
};

const char *cheapest(struct Cake cakes[], int n) {
    int best = 0;
    for (int i = 1; i < n; i++) {
        // your code here: if this cake is cheaper than cake best, set best to i
    }
    return cakes[best].name;
}
Ctrl + Enter runs too
Result

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