Kodewick
开始学 →
基础←→0 / 70

一周步数

#052基础指针数组约 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 也能运行
结果

写好了先点「运行」试试示例,没问题再点「提交」。