C++ program to sum all elements of given array.

// 2017 TheFlyingKeyboard
// 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
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *