+ 1

C++

How do we write a program to calculate the sum of the values entered in an array of 20 cells ?

8th Jan 2019, 12:14 PM
Shubhra Jyotsna
Shubhra Jyotsna - avatar
3 Answers
0
You can use a loop to go through every value in the array and sum them to a variable, something like this: int arr[] = {1, 2, 3}; int result = 0; for(int i = 0; i < 3; i++) { result += arr[i]; } cout << result; // Output 6
8th Jan 2019, 2:39 PM
Sekiro
Sekiro - avatar
0
Sekiro You should set the integer variable result to 0 first, or you could get some memory garbage there. The output won't be consistent.
8th Jan 2019, 3:45 PM
Rain
Rain - avatar
0
Rain Yes, sorry I totally missed that, was too sleepy yesterday! I corrected it... Thanks for noticing!
8th Jan 2019, 11:45 PM
Sekiro
Sekiro - avatar