计算数组中的整数和 [英] Calculating the sum of integers in an array
本文介绍了计算数组中的整数和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不知道我是否只是个傻瓜,很可能是很长的一天,但是这并没有达到我想要的效果,而且,我没有看到为什么.
I don't know if I'm just being a total fool, most likely I am, it's been a long day, but this isn't working as I want it to, and, well, I don't see why.
它应该能够输入11个数字,在每行上输入一个新数字,将它们添加到数组中,然后将它们加起来,但是这是行不通的.即使我要递增i,也不会停止退出循环.
It should be able to have 11 numbers entered, a new number on each line, add them to the array, then total them, yet it's just not working. It's not stopping to exit the loop, even though I am incrementing i.
有什么想法吗?
int main(void) {
int array[10];
int i;
int sum = 0;
for ( i = 0; i < 11; i++){
scanf("%d", &array[i]);
}
for (i = 0; i < 11; i++) {
sum += array[i];
}
printf("%d", sum);
return 0;
}
推荐答案
您的数组中有10个元素,编号为0-9.您正在缓冲区中溢出,因此所有选项都关闭了.这是不确定的行为.
You have 10 elements in the array, numbered 0 - 9. You are overflowing the buffer, so all bets are off. This is undefined behaviour.
这篇关于计算数组中的整数和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文