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.
I really appreciate any help at all.
This is the assignment:
"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."
Below is arrtemp.cpp and what i have written:
arrtemp.cpp
#include <iostream>
using namespace std;
//function template to print an array
//works for multiple data types
template <class T>
void printarray (T *a, const int n)
{
for (int i = 0; i < n; i++)
cout << a[i] << " ";
cout << 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 <<"the integer array" << endl;
printarray(a, n1);
cout <<"the float array" << endl;
printarray(b,n2);
cout <<"the string is" << endl;
printarray(c,n3);
return 0;
}
What i have Written:
#include<iostream>
#include<fstream>
using namespace std;
template <class T>
T loadArray(T *a, const int n )
{
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
}
template <class T>
T smallPrint(T *a, T *small, T *small2, const int n)
{
small = a[0];
small2 = a[0];
for (int i = 1; i < n; i++)
{
if (a[i] < small)
small = a[i];
}
for (int i = 1; i < n; i++)
{
if (a[i] > small && a[i] < small2)
small2 = a[i];
}
cout << small << ' ' << small2;
cout << endl << endl;
}
template <class T>
T sort(T *a, const int n)
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
if (a[j] < a[j + 1])
{
T t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
template <class T>
T saveFile(T *a, T small, T small2, const int n, string filename)
{
ofstream outfile(filename, ios::out);
for (int i = 0; i < n; i++)
{
outfile << a[i] << ' ';
}
outfile << small << small2;
outfile.close();
}
template <class T>
T retrieveFile(T *a, T small, T small2, const int n, string filename)
{
ifstream infile(filename, ios::in);
for (int i = 0; i < n; i++)
{
infile >> a[i];
}
infile >> small >> small2;
infile.close();
}
template <class T>
T printArray(T*a, const int n)
{
for (int i = 0; int < n; i++)
cout << a[i] << ' ';
cout << 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;
}
This is the Corrected Version that works:
#include<iostream>
#include<fstream>
using namespace std;
template <class T>
void loadArray(T *a, const int n)
{
for (int i = 0; i < n; i++)
{
cout << "Enter a number (" << n-i << " left): ";
cin >> a[i];
}
cout << endl;
}
template <class T>
void smallPrint(T *a, T &small, T &small2, const int n)
{
small = a[0];
small2 = a[0];
for (int i = 1; i < n; i++)
{
if (a[i] < small)
small = a[i];
}
for (int i = 1; i < n; i++)
{
if (a[i] > small && a[i] < small2)
small2 = a[i];
}
cout << small << ' ' << small2;
cout << endl << endl;
}
template <class T>
void sort(T *a, const int n)
{
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
{
if (a[j] < a[j + 1])
{
T t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
template <class T>
void saveFile(T *a, T small, T small2, const int n, string filename)
{
ofstream outfile(filename, ios::out);
for (int i = 0; i < n; i++)
{
outfile << a[i] << ' ';
}
outfile << small << ' ' << small2;
outfile.close();
}
template <class T>
void retrieveFile(T *a, T small, T small2, const int n, string filename)
{
ifstream infile(filename, ios::in);
for (int i = 0; i < n; i++)
{
infile >> a[i];
}
infile >> small >> small2;
infile.close();
}
template <class T>
void printArray(T*a, const int n)
{
for (int i = 0; i < n; i++)
cout << a[i] << ' ';
cout << 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;
}`