如何编写允许用户将不同值输入数组的代码

时间:2016-07-08 14:19:52

标签: c arrays scanf

我是C的初学者(学习免费资源),我正在尝试编写一个简单的代码,将用户输入的一组数字添加到一组数组中....我做错了什么?

int numbers;
    int number=1;
    int counter = 0;
    int pot[100];
    int calculate;
    int result;

printf("how many numbers do you want to calculate?:"); //asking user how many different integers would be calculated

scanf("%d",&numbers);

while (counter < numbers)

{
    printf("input number %d\n", number);//asking user to input figures to be calculated
    scanf("%d",&pot[0]+1); //user would input values into spaces in the array

    counter++;
    number++;

}


printf("Please press 1 for addition and 2 for multiplication");
scanf("%d",&calculate);

switch (calculate)

{
case 1: result = pot[0]+ pot[0]+1;//this is supposed to add all the individual values within the arrays

printf("the result is %d", result);
break;

case  2: result = pot[0]* pot[0]+1;//this is supposed to multiply all the individual values within the arrays
break;
}



    return 0;

4 个答案:

答案 0 :(得分:2)

int numbers;
int number=1;
// int counter = 0; instead used `i`
int pot[100];
int calculate;
int result;
int i = 0;

printf("how many numbers do you want to calculate?:"); //asking user how many different integers would be calculated

scanf("%d",&numbers);

printf("input %d numbers \n", numbers);//asking user to input figures to be calculated

for(i = 0; i < numbers; i++)
{
    printf("input number %d \n", number);
    scanf("%d", &pot[i]);   //user would input values into spaces in the array
    number++;
}

//scanf("%d",&pot[0]+1);     /* this line is wrong*/
//counter++; 
//number++;     


printf("Please press 1 for addition and 2 for multiplication");
scanf("%d",&calculate);

switch (calculate)

{
case 1: result = 0;//this is supposed to add all the individual values within the arrays
        for(i = 0;i < numbers; i++)
        {
             result = result + pot[i];
        }

        printf("the result is %d", result);
        break;

case  2: result = 1;//this is supposed to multiply all the individual values within the arrays
         for(i = 0;i < numbers; i++)
         {
             result = result * pot[i];
         }
         printf("the result is %d", result);
         break;
}



return 0;

此代码应该适用于您的程序。

答案 1 :(得分:1)

我不确定你要做什么,所以这是我的猜测:

int sum(const int *arr, size_t n)
{
    int ret;

    for (ret = 0; --n >= 0; ret += arr[n])
        ;

    return ret;
}
long mpy(const int *arr, size_t n)
{
    long ret;

    for (ret = 1; --n >= 0; ret *= arr[n])
        ;

    return ret;

}
// remove trailing newline
void remTrailingNl(char *buf)
{
    if (buf[strlen(buf) - 1] == '\n')
        buf[strlen(buf) - 1] = '\0';
}

int main()
{
    int *arr, choice, tmp;
    char line[80], *endp;
    size_t n;

    printf("How many numbers are in the array?");
    fgets(line, 80, stdin);
    remTrailingNl(line);
    n = strtol(line, &endp, 10);
    // **endp will be the null character if conversion was successful
    if (**endp == '\0')
        if ((arr = malloc(n * sizeof(int))) == NULL) {
            perror("malloc");
            return 1;
        }

    printf("Begin entering your numbers, each number to a line\n");

    // walking backwards, populate the array
    while (n-- >= 0) {
        fgets(line, 80, stdin);
        remTrailingNl(line);
        tmp = strtol(line, &endp, 10);
        if (**endp == '\0')
            arr[n] = tmp;
        else {
            fprintf(stderr, "Invalid number at position %d\n", n + 1);
            return 1;
        }
    }

    printf("Enter 1 for addition, and 2 for multiplication\n");
    fgets(line, 80, stdin);
    remTrailingNl(line);
    choice = strtol(line, &endp, 10);
    if (**endp == '\0' && choice == 1 || choice == 2) {
        switch (choice) {
            case 1: // add
                printf("Sum is %d\n", sum(arr, n));
                break;
            case 2:
                printf("Product is %ld\n", mpy(arr, n));
                break;
        }
    } else {
        fprintf(stderr, "Invalid choice\n");
        return 1;
    }
    // heap memory must be freed
    free(arr);
    // return 0 on success
    return 0;
}

我们对long使用mpy,因为乘法可以给出非常大的答案。另外,不要使用scanf,因为尽管看起来非常无辜,但要做到这一点非常棘手。 strtol更好,因为它详细说明了数字无效的原因。阅读手册页here。我们在调用remTrailingNl后立即调用fgets,因为fgets始终在缓冲区中放置一个尾随换行符。

答案 2 :(得分:1)

如果你想要一个简单的代码只是为了在数组中添加数字,数字是由用户给出的,你可以跟着这个。

int i, num, ans=0, pot[100];
printf("Give the number of elements:");
scanf("%d",&num);
printf("Enter numbers");
for(i=0;i<num;i++) {
  scanf("%d",&pot[i]);
  ans = ans + pot[i];
}
printf("Sum is %d", ans);
}

通过这种方式直接显示添加,而不是先扫描它,然后再次进行循环并逐个添加。

(而且我也是一名C学习者,如果我在任何地方都错了,请纠正我。) 希望这会简化它。

答案 3 :(得分:0)

So, finally got a working program!!

int pot[100];
int numbers;
int count;
int number = 1;
int calculate;
int result;


printf("how many numbers do you want to calculate:");
scanf("%d", &numbers);

for(count=0;count<numbers;count++)

{
printf("enter number %d:\n", number);
scanf("%d",&pot[count]);
number++;

}

printf("Please press 1 for addition and 2 for multiplication");
scanf("%d",&calculate);

switch (calculate)

{
case 1: result = 0;

        for(count = 0;count < numbers; count++)
        {
             result = result + pot[count];
        }

        printf("the result is %d", result);
        break;

case  2: result = 1;

         for(count = 0;count< numbers; count++)
         {
             result = result * pot[count];
         }
         printf("the result is %d", result);
         break;
}

返回0;