List three methods to calculate the sum of the elements of an array.
The three methods to calculate the sum of the elements of an array are:
1. Using a for loop: for (int i=0; i<n; i++) { sum += arr[i]; }
2. Using a while loop: int sum = 0; while (arr[i] != -1) { sum += arr[i]; }
3. Using a vector: std::vector<int> vect { arr }; std::accumulate(vect.begin(), vect.end(), 0);
1. The naïve approach is to iterate through the array and sum up all the elements.
2. One can use a for loop to calculate the sum.
3. Another method is to use the built-in sum() function, which takes an array as an argument and returns the sum of the elements.