title: C++: Issue with using Function Templates to Edit Array
body: <p>So I have this assignment which requires me to use function templates to load array data from the keyboard, print the two smallest values in each array, sort the arrays in descending order, save the data to a text file, then retrieve that text file.
  My main issue is that i keep getting errors around my smallPrint function template. an error message reads:
   "no instance of function template "smallPrint" matches the argument list
  argument types are: (float [7], int, int, const int)
  Another thing that pops when when i try to run the program is an issue within my printArray function template. I don't see anything wrong.</p>
  
  <p>I really appreciate any help at all.</p>
  
  <p>This is the assignment:</p>
  
  <p>"Modify arrtemp.cpp by removing the reassignment of the data for each array and by adding five new function templates. 
  1.) The first new function template should allow the user to enter the array data from the keyboard. 
  2.) The second new function template should print the smallest and second smallest values for an array without sorting. You may assume that each array consists of distinct data. 
  3.) The third new function template should sort the array in descending order. 
  4.) The fourth new function template should save the save data to a text file. 5.) The fifth new function template should retrieve the array data from the text file. Output should include the smallest and second smallest values for each array along with all three arrays being printed out in descending order twice, once before the text file is saved and one after it is retrieved. make sure to include [template ] before each function template. Each array should  be saved to a separate text file . You will also need to show all 3 text files."</p>
  
  <p>Below is arrtemp.cpp and what i have written:</p>
  
  <p>arrtemp.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  //function template to print an array
  //works for multiple data types
  
  template &lt;class T&gt;
  void printarray (T *a, const int n)
  {
      for (int i = 0; i &lt; n; i++)
          cout &lt;&lt; a[i] &lt;&lt; "  ";
      cout &lt;&lt; endl;
  }
  
  int main()
  {
      const int n1 = 5, n2 = 7, n3 = 6;
      int a[n1] = {2, 4, 6, 8, 10};
      float b[n2] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};
      char c[n3] = "HELLO";
  
      cout &lt;&lt;"the integer array" &lt;&lt; endl;
      printarray(a, n1);
  
      cout &lt;&lt;"the float array" &lt;&lt; endl;
      printarray(b,n2);
  
      cout &lt;&lt;"the string is" &lt;&lt; endl;
      printarray(c,n3);
      return 0;
  }
  </code></pre>
  
  <p>What i have Written:</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;fstream&gt;
  using namespace std;
  
  template &lt;class T&gt;
  T loadArray(T *a, const int n )
  {
      for (int i = 0; i &lt; n; i++)
      {
          cin &gt;&gt; a[i];
      }
  }
  
  template &lt;class T&gt;
  T smallPrint(T *a, T *small, T *small2, const int n)
  {
      small = a[0];
      small2 = a[0];
      for (int i = 1; i &lt; n; i++)
      {
          if (a[i] &lt; small)
              small = a[i];       
      }
      for (int i = 1; i &lt; n; i++)
      {
          if (a[i] &gt; small &amp;&amp; a[i] &lt; small2)
              small2 = a[i];
      }
      cout &lt;&lt; small &lt;&lt; ' ' &lt;&lt; small2;
      cout &lt;&lt; endl &lt;&lt; endl;
  }
  
  template &lt;class T&gt;
  T sort(T *a, const int n)
  {
      for (int i = 0; i &lt; n; i++)
          for (int j = 0; j &lt; n; j++)
      {
          if (a[j] &lt; a[j + 1])
          {
              T t = a[j];
              a[j] = a[j + 1];
              a[j + 1] = t;
          }
      }
  }
  
  template &lt;class T&gt;
  T saveFile(T *a, T small, T small2, const int n, string filename)
  {
      ofstream outfile(filename, ios::out);
      for (int i = 0; i &lt; n; i++)
      {
          outfile &lt;&lt; a[i] &lt;&lt; ' ';
      }
      outfile &lt;&lt; small &lt;&lt; small2;
      outfile.close();
  }
  
  template &lt;class T&gt;
  T retrieveFile(T *a, T small, T small2, const int n, string filename)
  {
      ifstream infile(filename, ios::in);
      for (int i = 0; i &lt; n; i++)
      {
          infile &gt;&gt; a[i];
      }
      infile &gt;&gt; small &gt;&gt; small2;
      infile.close();
  }
  
  template &lt;class T&gt;
  T printArray(T*a, const int n)
  {
      for (int i = 0; int &lt; n; i++)
          cout &lt;&lt; a[i] &lt;&lt; ' ';
      cout &lt;&lt; endl;
  }
  
  int main()
  {
      const int n1 = 5, n2 = 7, n3 = 6;
      int a[n1];
      float b[n2];
      char c[n3];
      int smalli, smalli2;
      float smallf, smallf2;
      char smallc, smallc2;
  
      loadArray(a, n1);
      loadArray(b, n2);
      loadArray(c, n3);
  
      smallPrint(a, smalli, smalli2, n1);
      smallPrint(b, smallf, smallf2, n2);
      smallPrint(c, smallc, smallc2, n3);
  
  
      sort(a, n1);
      sort(b, n2);
      sort(c, n3);
  
      printArray(a, n1);
      printArray(b, n2);
      printArray(c, n3);
  
      saveFile(a, smalli, smalli2, n1, "i:\\ints.txt");
      saveFile(b, smallf, smallf2, n2, "i:\\floats.txt");
      saveFile(c, smallc, smallc2, n3, "i:\\chars.txt");
  
      retrieveFile(a, smalli, smalli2, n1, "i:\\ints.txt");
      retrieveFile(b, smallf, smallf2, n2, "i:\\floats.txt");
      retrieveFile(c, smallc, smallc2, n3, "i:\\chars.txt");
  
      printArray(a, n1);
      printArray(b, n2);
      printArray(c, n3);
  
      system("PAUSE");
      return 0;
  }
  </code></pre>
  
  <hr>
  
  <p>This is the Corrected Version that works:</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;fstream&gt;
  using namespace std;
  
  template &lt;class T&gt;
  void loadArray(T *a, const int n)
  {
      for (int i = 0; i &lt; n; i++)
      {
          cout &lt;&lt; "Enter a number (" &lt;&lt; n-i &lt;&lt; " left): ";
          cin &gt;&gt; a[i];
      }
      cout &lt;&lt; endl;
  }
  
  template &lt;class T&gt;
  void smallPrint(T *a, T &amp;small, T &amp;small2, const int n)
  {
      small = a[0];
      small2 = a[0];
      for (int i = 1; i &lt; n; i++)
      {
          if (a[i] &lt; small)
              small = a[i];
      }
      for (int i = 1; i &lt; n; i++)
      {
          if (a[i] &gt; small &amp;&amp; a[i] &lt; small2)
              small2 = a[i];
      }
      cout &lt;&lt; small &lt;&lt; ' ' &lt;&lt; small2;
      cout &lt;&lt; endl &lt;&lt; endl;
  }
  
  template &lt;class T&gt;
  void sort(T *a, const int n)
  {
      for (int i = 0; i &lt; n; i++)
          for (int j = 0; j &lt; n; j++)
          {
              if (a[j] &lt; a[j + 1])
              {
                  T t = a[j];
                  a[j] = a[j + 1];
                  a[j + 1] = t;
              }
          }
  }
  
  template &lt;class T&gt;
  void saveFile(T *a, T small, T small2, const int n, string filename)
  {
      ofstream outfile(filename, ios::out);
      for (int i = 0; i &lt; n; i++)
      {
          outfile &lt;&lt; a[i] &lt;&lt; ' ';
      }
      outfile &lt;&lt; small &lt;&lt; ' ' &lt;&lt; small2;
      outfile.close();
  }
  
  template &lt;class T&gt;
  void retrieveFile(T *a, T small, T small2, const int n, string filename)
  {
      ifstream infile(filename, ios::in);
      for (int i = 0; i &lt; n; i++)
      {
          infile &gt;&gt; a[i];
      }
      infile &gt;&gt; small &gt;&gt; small2;
      infile.close();
  }
  
  template &lt;class T&gt;
  void printArray(T*a, const int n)
  {
      for (int i = 0; i &lt; n; i++)
          cout &lt;&lt; a[i] &lt;&lt; ' ';
      cout &lt;&lt; endl;
  }
  
  int main()
  {
      const int n1 = 5, n2 = 7, n3 = 6;
      int a[n1];
      float b[n2];
      char c[n3];
      int smalli, smalli2;
      float smallf, smallf2;
      char smallc, smallc2;
  
      loadArray(a, n1);
      loadArray(b, n2);
      loadArray(c, n3);
  
      smallPrint(a, smalli, smalli2, n1);
      smallPrint(b, smallf, smallf2, n2);
      smallPrint(c, smallc, smallc2, n3);
  
  
      sort(a, n1);
      sort(b, n2);
      sort(c, n3);
  
      printArray(a, n1);
      printArray(b, n2);
      printArray(c, n3);
  
      saveFile(a, smalli, smalli2, n1, "i:\\ints.txt");
      saveFile(b, smallf, smallf2, n2, "i:\\floats.txt");
      saveFile(c, smallc, smallc2, n3, "i:\\chars.txt");
  
      retrieveFile(a, smalli, smalli2, n1, "i:\\ints.txt");
      retrieveFile(b, smallf, smallf2, n2, "i:\\floats.txt");
      retrieveFile(c, smallc, smallc2, n3, "i:\\chars.txt");
  
      printArray(a, n1);
      printArray(b, n2);
      printArray(c, n3);
  
      system("PAUSE");
      return 0;
  }`
  </code></pre>