为什么会打印出以下代码"0 0 0 0 0 0 0 "
?我期待着"1 3 6 10 15 21 28 "
。
#include <iostream>
using namespace std;
void PrefixSum(float * input, float * output, int n){
float sum = 0;
for (int i=0;i<n;i++){
float value = input[i];
sum += value;
output[n] = sum;
}
}
int main(int argc, const char * argv[])
{
float input[] = {1,2,3,4,5,6,7};
float output[] = {0,0,0,0,0,0,0};
PrefixSum(input, output, 7);
for (int i=0;i<7;i++){
cout << output[i] << " ";
}
return 0;
}