C++ program to sum all elements of given array.
// 2017 TheFlyingKeyboard and released under MIT License // theflyingkeyboard.net #include <iostream> template <typename T> T addElements(T arrayToAdd[], int arraySize); using namespace std; int main(){ int arrayToAdd[6] = { 1, 2, 4, 4, 2, 1 }; cout << "Sum: " << addElements(arrayToAdd, 6) << endl; return 0; } template <typename T> T addElements(T arrayToAdd[], int arraySize){ T sum = 0; for(int i = 0; i < arraySize; i++){ sum += arrayToAdd[i]; } return sum; }
C++ Sum Elements Of Array