0
How to sum the value in an integer array. Help me please
how to add values of index in array
3 Antworten
+ 3
XiLef just a small but important correction: int sum = 0;
You don't want garbage values in your sum.
And a small addition which makes this easier: you can use range based for loops with arrays:
for (auto num : numbers)
sum += num;
+ 2
int numbers [10];
int sum;
for (int i = 0; i < 10; i++){
sum += numbers [i];
}
+ 2
int a, sum = 0 ;
int arrayname[] = {1, 2, 3, 4, 5};
for (a=0; a<5; a++)
{ sum += arrayname[a]; }
cout << sum << endl;
/* for the array length you can also use statement like
sizeof(arrayname)/sizeof(*arrayname) instead of just 5
*/