2017-04-22 125 views
-5

我的练习是输入从键盘输入整数,然后按程序结束。然后打印数组的总和。这是我的代码:我有练习c

#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; 
} 

我不知道它有什么问题吗?它没有给我一个结果,同样我认为......

+2

初学者'* count ++;' - >'(* count)++;'参见C * Operator Precedence *。 –

+1

当问这样一个问题时,请非常清楚地定义*你的输入,预期输出和实际输出。 – deniss

+2

打开编译器警告。编译器会告诉你一些错误的东西。另一个不对的地方是,你永远不会访问超出第一个数组的元素。 –

回答

1

有喜欢的一些问题 -

inputIntegerNumber(&x, &count); 
printf("Sum of array is %d", sumOfInteger(&x, &count)); 
在你通过 &xx两个呼叫

int数组和你的函数需要int *int (*)[]。这一定是至少有一个错误。

对于这两种功能,您都可以直接传递数组x

而且在你的函数inputIntegerNumber本 -

*count++; 

你需要增加的count价值,所以它应该是(*count)++。首先解除引用,然后递增该值。

1

你在代码中犯了一些错误,比如将指针传递给指针&x的值(因为数组基本上是指向某个内存位置的指针),并且一次又一次地覆盖相同的位置。在scanf("%d", a);中,您将一次又一次覆盖第一个位置,而不会在输入循环中更改a。您需要了解数组及其用法。在sumOfInteger函数中,您也不会更改n的值。我改变了你的代码,我能够看到所需的输出。

#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 = 0; // zero elements in array 

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

    return (EXIT_SUCCESS); 
} 

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

    *count = aIndex; 
} 

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

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

    return sum; 
} 

当我运行它,我可以看到:

~/Documents/src : $ ./a.out 
    Please! input numbers: 1 
    Please! input numbers: 2 
    Please! input numbers: 3 
    Please! input numbers: 0 
    Sum of array is 6 
0

你过于复杂的事情。你坐下来写一个程序之前,总是写是必须做

  • 从命令行阅读数字的一般步骤
  • 将它们转换为整数,并将它们保存在内存中
  • 计算总和
  • 打印

下面是修改后的方案

#include <stdio.h> 
#include <stdlib.h> /* for strtol */ 

#define DIE(msg) fprintf(stderr, "%s", msg); exit(EXIT_FAILURE) 

int main(int argc, char *argv[]) 
{ 
    int *nums; 

    if (argc <= 1) 
     DIE("Usage: [int-list]\n"); 

    /* skip program name */ 
    --argc; 
    ++argv; 

    nums = malloc(argc * sizeof(int)); 
    if (!nums) 
     DIE("Out of mem\n"); 

    for (int i = 0; i < argc; ++i) { 
     char *end; 
     double val = strtol(argv[i], &end, 0); 

     if (end == argv[i]) /* no digits detected */ 
      DIE("Usage: [int-list]\n"); 

     nums[i] = val; 
    } 
    printf("%d\n", add(nums, argc)); 

    free(nums); 
} 
int add(int arr[], size_t n) 
{ 
    int sum = 0; 

    for (int i = 0; i < n; ++i) 
     sum += arr[i]; 

    return sum; 
} 

要完成strtol错误处理是OP的练习。