Q1: Write a function template that takes a single type parameter (T) and accepts four function arguments: an array of T, a start index, a stop index (inclusive), and an optional initial value. The function returns the sum of all the array elements in the specified range and the initial value. Use the default constructor of T for the default initial value.
#include <iostream>
using namespace std;
template <class T>
T sumArr(T arr[], T start, T stop, T init)
{
  T sum=0;
  for(T i=start;i<=stop;i++){
    sum=sum+arr[i];
  }
  sumArr<T>();{
  init=5;
}
  return sum;
}
int main()
{
  Â
  return 0;
}
Comments
Leave a comment