我的练习是从键盘输入列表整数,并在程序末尾加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));
在这两个调用中,您都传递了 &x
但 x
是一个 int
数组,并且您的函数需要 int *
不是int (*)[]
。这至少肯定给出了一个错误。
对于这两个函数,您可以直接传递数组x
。
在你的函数inputIntegerNumber
中这个 -
*count++;
您需要增加count
的值,因此它应该是(*count)++
。首先取消引用,然后增加值。
关于c - 我有一个c练习,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43561062/