gpt4 book ai didi

c - 数组 C 中正整数的和

转载 作者:行者123 更新时间:2023-11-30 17:27:03 24 4
gpt4 key购买 nike

我正在尝试查找数组中所有正数的总和。到目前为止,我已经想出了这个;

int largest_sum_sequence(int list, int size)
{
int sum = 0, *index = 0;
for (index < size; index = size; index++)
{
if (list[index] > 0)
{
sum = sum + list[index];
}
}
printf("%d", sum);
return sum;
}

我的程序总是崩溃。我很确定这与索引有关。每当我使用 list[index] 时,它都会说我需要使用指针作为索引,但我不知道如何正确执行此操作。感谢帮助!

最佳答案

您不希望索引成为指针,并且您的 for 循环不正确。尝试:

int sum = 0, index = 0;
for (; index < size; index++)

关于c - 数组 C 中正整数的和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26537224/

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