title: Function Template that will add all elements of any 2-D integer array in C++
body: <p>I have been tasked with creating a function template that will add all elements of any 2-dimensional integer array as part of an exercise for a programming theory language course.</p>
  <p>I have tried several methods and keep getting compiler errors that I don't understand.</p>
  <p>1)</p>
  <pre><code>template&lt;typename T&gt;
  T addArraysOne(T rows, T cols, T arr[][cols])
  {
      T output;
      for(int i = 0; i &lt; rows; i++)
      {
          for(int j = 0; j &lt; cols; j++)
          {
              output += arr[i][j];
          }
      }
      return output;
  }
  </code></pre>
  <p>This one errors as the parameter for the number of columns is &quot;not a constant&quot;.</p>
  <p>2)</p>
  <pre><code>int addArraysTwo(int rows, int cols, int** arr)
  {
      int output;
      for(int i = 0; i &lt; rows; i++)
      {
          for(int j = 0; j &lt; cols; j++)
          {
              output += *arr[i*rows+j];
          }
      }
      return output;
  }
  </code></pre>
  <p>This one errors when I try and call it with <code>addArraysTwo(5, 5, arr);</code> because it &quot;does not match the function call&quot;, when &quot;arr&quot; is a 2 dimensional int array. I haven't found any problems particularly similar to this online. Any suggestions on how to modify these so that I do not piss off the compiler would be much appreciated. Thanks!</p>