+ 1
C++
How do we write a program to calculate the sum of the values entered in an array of 20 cells ?
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
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.
0
Rain Yes, sorry I totally missed that, was too sleepy yesterday! I corrected it... Thanks for noticing!