gpt4 book ai didi

c - 为什么它不能正确添加我的号码

转载 作者:行者123 更新时间:2023-11-30 21:24:43 25 4
gpt4 key购买 nike

我正在尝试将数组中的 100 个数字相加,这是我的代码:从 17 到 117 的数字加起来应该更多。

#include <stdio.h>
#include <stdlib.h>

int main(){
int* number = malloc(101*sizeof(int));
int num = 0;

while (num != 101){
number[num] = 17 + num;
num = num + 1;
};


num = 0;

while(num != 101){
printf(" %d \n", number[num]);
num = num + 1;
}

num = 0;
int sum = 0;
while (num != 101){
number[num] = 17;
sum = sum + number[num];
num = num + 1;

}

printf(" %d \n ", sum );

return 0;
}

底部打印语句给出的值为 1717

最佳答案

while (num != 101){
number[num] = 17;
sum = sum + number[num];

删除将所有内容重置为 17 的第二行

关于c - 为什么它不能正确添加我的号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35165399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com