简体   繁体   English

给定一个整数数组,求其元素之和

[英]Given an array of integers, find the sum of its elements

Given an array of integers, find the sum of its elements.给定一个整数数组,求其元素之和。 my problem is that;我的问题是; in c language you have only fixed size array;在 c 语言中,你只有固定大小的数组; my code does not print the sum of random size array which the question demands;[1,2,3,4,5,-------n elements] it prints for only for ex 6 or defnite size aray;我的代码不打印问题要求的随机大小数组的总和;[1,2,3,4,5,-------n 个元素] 它只为 ex 6 或确定大小的数组打印; using a loop;使用循环;

#include <stdio.h>
int main() {
    int i;
    scanf("%d\n",&i);
    int a[6];
    int sum=0;
    for(i=0;i<=5;i++)
        scanf("%d\n",&a[i]);
    for(i=0;i<=5;i++)
        sum=sum+a[i];
    printf("%d\n",sum);
    return 0;
}

It seems you want to let you user input the number of elements in the array.您似乎想让用户输入数组中的元素数。 Your code scan that information into variable i .您的代码将该信息扫描到变量i Therefore you can't use variable i as counter in the for loops.因此你不能在 for 循环中使用变量i作为计数器。 You need two different variables.您需要两个不同的变量。 One variable to hold the the number of integers to include in the sum and another variable for the loops.一个变量用于保存要包含在总和中的整数数量,另一个变量用于循环。

Further to get a variable sized array, you need to use the users input when you define the array.为了进一步获得可变大小的数组,您需要在定义数组时使用用户输入。 This is called VLA (Variable Length Array).这称为 VLA(可变长度阵列)。

Something like:就像是:

#include <stdio.h>
int main() {
    int i;
    int N = 0;         // New variable holding the number of integers in the sum
    scanf("%d\n",&N);  // scan into N
    int a[N];          // Use N for the VLA
    int sum=0;
    for(i=0;i<N;i++)   // Use N as limit
        scanf("%d\n",&a[i]);
    for(i=0;i<N;i++)   // Use N as limit
        sum=sum+a[i];
    printf("%d\n",sum);
    return 0;
}

That said - be careful about VLAs.也就是说 - 小心 VLA。 If the user inputs a high number for N a stack overflow may occur.如果用户输入 N 的高数,可能会发生堆栈溢出。 If you want to use VLAs your code should enforce a maximum limit for the users input.如果您想使用 VLA,您的代码应该对用户输入实施最大限制。

It's typically better to use dynamic allocation instead of VLA.通常最好使用动态分配而不是 VLA。 Like:喜欢:

#include <stdio.h>
#include <stdlib.h>
int main() {
    int i;
    int N = 0;
    scanf("%d\n",&N);
    int *a = malloc(N * sizeof *a);  // Dynamic allocation
    if (a == NULL) exit(1);          // Check for allocation failure
    int sum=0;
    for(i=0;i<N;i++)
        scanf("%d\n",&a[i]);
    for(i=0;i<N;i++)
        sum=sum+a[i];
    printf("%d\n",sum);
    free(a);                         // Free allocated memory
    return 0;
}

Some extra comments:一些额外的评论:

1) To calculate the sum you actually don't need an array. 1)要计算您实际上不需要数组的总和。 Just scan into some int and add it to sum .只需扫描到一些int并将其添加到sum No need for storing it in an array first.无需先将其存储在数组中。

2) Always check the return value of scanf . 2) 始终检查scanf的返回值。 Example: if (scanf("%d\\n",&N) != 1) exit(1);示例: if (scanf("%d\\n",&N) != 1) exit(1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM