ショーケースのケーキはそれぞれ struct Cake で、名前と値段を持っています。いちばん安いケーキの名前を返す cheapest(cakes, n) を書きましょう。ケーキは少なくとも 1 つあります。同じ値段なら先にあるほうを返します。
cheapest({{"cheesecake", 28}, {"brownie", 12}, {"tiramisu", 30}}, 3)→'brownie'cheapest({{"donut", 6}}, 1)→'donut'const char *cheapest(struct Cake cakes[], int n)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++) {
// ここにコードを書く:このケーキが best 番目のケーキより安ければ、best を i にする
}
return cakes[best].name;
}
実行すると例で試せます。よさそうなら提出しましょう。