0

How to sum the value in an integer array. Help me please

how to add values of index in array

15th Apr 2018, 8:10 AM
Mushtaq Khan
Mushtaq Khan - avatar
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;
15th Apr 2018, 8:22 AM
Alex
Alex - avatar
+ 2
int numbers [10]; int sum; for (int i = 0; i < 10; i++){ sum += numbers [i]; }
15th Apr 2018, 8:14 AM
XiLef
+ 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 */
15th Apr 2018, 8:20 AM
DaEistee