/*
Let A be an array of n elements. Write a template function, maxfunc(…), which takes an unsorted array of type <class T> as an input parameter and returns the element with the maximum value. Assume the operators < and > are defined for the class T. In this question, you may hardcode n=5. You must write all functions you call, and also needs to write a main() to test the following data arrays stored in a data file (data.txt):
4 1 13 3 2
1.1 4.1 8.1 5.2 2.3
the student is in class // > is judged on the basis of alphabetical order
In addition, the results should be printed out in main( ) routine.
*/
#include <iostream>
#include <fstream>
usingnamespace std;
constint n =5;
int A[n];
template <class T>
T maxfunc()
{
int max=0;
for(int i=0; i< 6; i++)
{
if(A[i]>max)
A[i] = max;
}
cout << "The max value is " << max << endl;
return 0;
};
int main()
{
ifstream ins;
ins.open("data.rtf");
cin.getline("data.rtf", 5);
if(ins.is_open()){
cout << "File is open";
}
else{
cout << "File NOT Open";
}
while(ins.good())
{
for(int i=0; i<6; i++)
cout << A[i] << " " << endl;
}
maxfunc(A);
system("pause");
return 0;
}
First A[] is decraled as global, so you don't need to pass any parameters
Then maxfunc don't have/need any parameter but you pass A as parameter in main
Last use ins>>A[i]; to fill the array with value from file