### template function

Hi everyone! My FindMaxMin template functions are not outputting the correct maximum and minimum values. How can I correct this? Thank you!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122`` ``````#include #include #include using namespace std; int ComputeAve (int a[], int n); int ComputeAve (float d[], int n, float&D_Average); template void ReadData (T x[], int n) { for (int i = 0; i < n; ++i) cin >> x[i]; cout << endl; } template void DisplayData (T x[], int n) { for (int i = 0; i < n; ++i) cout << x[i] << " "; cout << endl; } template void ShowAve (T1 p,T2 q) { cout << p << q; cout << endl; } template void FindMaxMin (T x[], int n, int&max, int&min) { int amax=x[0]; int amin=x[0]; for (int i = 0; i < n; ++i) { if (x[i] > amax) max = x[i]; else min = x[i]; } } template void FindMaxMin (T x[], int n, float&max, float&min) { float dmax=x[0]; float dmin=x[0]; for (int i = 0; i < n; ++i) { if (x[i] > dmax) max=x[i]; else min=x[i]; } } template void ShowMaxMin (T1 p, T2 q, T3 r) { cout << p << q << r; cout << endl; } int main() { int a[5]; char c[6]; float d[4]; string s[4]; ///Read data into each array cout << "Enter 5 integer numbers:"; ReadData(a,5); cout << "Enter 6 characters:"; ReadData(c,6); cout << "Enter 4 decimal numbers:"; ReadData(d,4); cout << "Enter 4 names:"; ReadData(s,4); ///Display all arrays cout << fixed << showpoint << setprecision(2); cout << "This is array a:"; DisplayData(a,5); cout << "This is array c:"; DisplayData(c,6); cout << "This is array d:"; DisplayData(d,4); cout << "This is array s:"; DisplayData(s,4); ///Compute the average of data in array a and d float A_Average=ComputeAve(a,5); ShowAve("The average of numbers in array a is:", A_Average); float D_Average; ComputeAve(d,4,D_Average); ShowAve(D_Average, " is the average of numbers in array d"); ///Find the maximum and minimum data in array a and d int amax, amin; float dmax, dmin; FindMaxMin(a,5,amax,amin); FindMaxMin(d,4,dmax,dmin); ShowMaxMin("In array a, Maximum and Minimum are:",amax,amin); ShowMaxMin("In array d, Maximum and minimum are:",dmax,dmin); ///Terminate program system ("pause"); return 0; } int ComputeAve (int a[], int n) { float A_Average = 0; float total = 0; for (int i = 0; i < n; ++i) { total+=a[i]; } A_Average = total/5.; return A_Average; } int ComputeAve (float d[], int n, float&D_Average) { D_Average = 0; float total = 0; for (int i = 0; i < n; ++i) { total+=d[i]; } D_Average = total/4.; return D_Average; } ``````
Look at your logic. You say if a number is bigger than the max then assign the new value to the old max. If not assign the value to the old minimum. Shouldn't it be less than the old minimum before assign to it and not if it is less than the current max?

*edit

Also shouldn't line 46 and 47 be your parameter min/max otherwise you will have to use those for your if statements then assign values to those.
Last edited on
Topic archived. No new replies allowed.