Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <class array_type>
  6. array_type sum(array_type *a, const int N)
  7. {
  8.     array_type sum = 0;
  9.     for (int i = 0; i < N; i++) {
  10.         sum += a[i];
  11.     }
  12.     return sum;
  13. }
  14.  
  15.  
  16. int main() {
  17.     int arrayInt[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  18.     int intArraySize = sizeof(arrayInt) / sizeof(arrayInt[0]);
  19.  
  20.     float arrayFloat[] = { 2.1, 3.2, 4.3, 5.4, 6.5, 7.6, 8.7, 9.8, 10.9, 11.0 };
  21.     int floatArraySize = sizeof(arrayFloat) / sizeof(arrayFloat[0]);
  22.  
  23.     double arrayDouble[] = { 2.1, 3.2, 4.3, 5.4, 6.5, 7.6, 8.7, 9.8, 10.9, 11.0 };
  24.     int doubleArraySize = sizeof(arrayDouble) / sizeof(arrayDouble[0]);
  25.  
  26.     cout << "Array Int Sum: " << sum(arrayInt, intArraySize) << endl;
  27.     cout << "Array Float Sum: " << sum(arrayFloat, floatArraySize) << endl;
  28.     cout << "Array Double Sum: " << sum(arrayDouble, doubleArraySize) << endl;
  29.  
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement