Calculating the sum of integers in an array

2020-04-07 05:02发布

问题:

I don't know if I'm just being a total fool, most likely I am, it's been a long day, but this isn't working as I want it to, and, well, I don't see why.

It should be able to have 11 numbers entered, a new number on each line, add them to the array, then total them, yet it's just not working. It's not stopping to exit the loop, even though I am incrementing i.

Any ideas?

int main(void) {
 int array[10];
 int i;
 int sum = 0;
  for ( i = 0; i < 11; i++){
   scanf("%d", &array[i]);
  }
  for (i = 0; i < 11; i++) {
   sum += array[i];
  }
printf("%d", sum);

return 0;

}

回答1:

You have 10 elements in the array, numbered 0 - 9. You are overflowing the buffer, so all bets are off. This is undefined behaviour.



回答2:

You can't add eleven entries to a ten-element array.



回答3:

My guess is buffer over-run since the for-loop reads in 11 numbers and the 11th number gets stored outside the array, probably overwriting i.

Try changing the 11 to a 10 in the for loop.



回答4:

You're storing eleven numbers into an array of size 10. Thus you're storing the last element out of bounds, which invokes undefined behavior.

The reason that this undefined behavior manifests itself as an infinite loop in your case is probably that i is stored after array in memory on your system and when you write a number into array[10] (which is out of bounds, as I said), you're overwriting i. So if you entered a number smaller than 11 this will cause the loop to continue and ask for input once more.



回答5:

If an array is a[10], then every array starts from its index number 0, so here it will have 10 elements; given that their positions will start from 0 to 9, counting gives 10 elements.

You can try this:

main()         
{        
    int a[10], i, n, sum=0;    

    printf("enter no. of elements");
    scanf("%d",&n); 
    printf("enter the elements");   

    for(i=0;i<n;i++)    
        scanf("%d",&a[i]);

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

    for(i=0;i<n;i++)
        printf("\n a[%d] = %d", i, a[i]);

    printf("\n sum = %d",sum);
    getch();

}


回答6:

You have problems with your array declaration. You are defining an array of size 10 array[10] and saying the program to calculate the sum of 11 elements which is resulting in memory overflows.

To correct the program just increase size of the array as array[11]. Also if you wish you can check the recursive approach to find sum of array elements.



回答7:

Try this:

void main() {
 int array[10];
 int i;
 int sum = 0;

  for ( i = 0; i < 11; i++){
   scanf("%d", &array[i]);
  }
  for (i = 0; i < 11; i++) {
   sum = sum + array[i] ;
  }
printf("%d", sum);

return 0;
}


回答8:

int main()
{
    int a[10];
    int i,j;
    int x=0;
    printf("Enter no of arrays:");
    scanf("%d",&j);
    printf("Enter nos:");
    for(i=0;i<j;i++)
    {
        scanf("%d",&a[i]);
    }
    for (i=0;i<j;i++)
    {
        x=x+a[i];
    }
    printf("Sum of Array=%d",x);
    return 0;
}


标签: c arrays