【发布时间】:2016-03-28 14:16:02
【问题描述】:
所以给我的提示是“写一个函数,给定一个整数数组并返回数组中偶数的总和。函数没有给出数组的长度,而是数组中的最后一个数字为 -1。例如,如果数组包含 {2,3,5,4,-1},则函数返回 6。使用标题 int sumEven(int myArray[])。"
到目前为止我写的代码是
#include <iostream>
using namespace std;
int sumEven(int myArray[]){
int sum = 0;
for (int i=0; i++;){
if (myArray[i] >=0) {
sum+=myArray[i];
}
}
return sum;
}
但它总是返回零?我没有看到我在这里做错了什么
【问题讨论】:
-
因为
i++(当i是0)将被评估为false,for循环根本不会运行。