gpt4 book ai didi

c - 我有一个c练习

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

我的练习是从键盘输入列表整数,并在程序末尾加0。然后打印数组的和。这是我的代码:

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

const int MAX_ITEMS = 50;
void inputIntegerNumber(int* a, int* count);
int sumOfInteger(int* n, int* count);

int main(int argc, char** argv) {
int x[MAX_ITEMS], count;

inputIntegerNumber(&x, &count);
printf("Sum of array is %d", sumOfInteger(&x, &count));

return (EXIT_SUCCESS);
}

void inputIntegerNumber(int* a, int* count ){
do{
printf("Please! input numbers: ");
scanf("%d", a);
*count++;
}while((*a != 0) && (*count != MAX_ITEMS));

}

int sumOfInteger(int* n, int* count){
int sum = 0;

for (int i = 0; i < *count; i++)
sum += *n;

return sum;
}

不知道这是怎么回事?它没有给我一个与我想的相同的结果......

最佳答案

存在一些问题,例如 -

 inputIntegerNumber(&x, &count);
printf("Sum of array is %d", sumOfInteger(&x, &count));

在这两个调用中,您都传递了 &xx 是一个 int 数组,并且您的函数需要 int *不是int (*)[]。这至少肯定给出了一个错误。

对于这两个函数,您可以直接传递数组x

在你的函数inputIntegerNumber中这个 -

 *count++;

您需要增加count的值,因此它应该是(*count)++。首先取消引用,然后增加值。

关于c - 我有一个c练习,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43561062/

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