Kodewick
Start learning →
Basic←→0 / 70

Weigh the luggage

#059BasicStructsLoops4 min

The family is off on a trip; each suitcase is a struct Bag: whose it is and how many kilos. Write total_kg(bags, n) to add up the kilos of the n bags. No bags gives 0.

Examples
total_kg({{"Ana", 18}, {"Ben", 23}}, 2)→41
total_kg({{"Kai", 7}}, 1)→7
The functionint total_kg(struct Bag bags[], int n)
main.c
struct Bag {
    const char *owner;
    int kg;
};

int total_kg(struct Bag bags[], int n) {
    int total = 0;
    for (int i = 0; i < n; i++) {
        // your code here: add bag i's kg to total
    }
    return total;
}
Ctrl + Enter runs too
Result

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