Kodewick
開始學 →
基礎←→0 / 70

一週步數

#052基礎p < steps + n約 4 分鐘

手環記下了每天的步數。寫 total_steps(steps, n),把 n 天的步數加起來。迴圈讓指標 p 從第一天走到最後一天,你只要把 p 指著的數加進 total。n 是 0 時返回 0。

示例
total_steps({3000, 4500, 5200}, 3)→12700
total_steps({800}, 1)→800
函式int total_steps(int *steps, int n)
main.c
int total_steps(int *steps, int n) {
    int total = 0;
    for (int *p = steps; p < steps + n; p++) {
        // 在這裡寫:把 p 指著的步數加進 total
    }
    return total;
}
Ctrl + Enter 也能執行
結果

寫好了先點「執行」試試示例,沒問題再點「提交」。