template function

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int ComputeAve (int a[], int n);
int ComputeAve (float d[], int n, float&D_Average);

template <class T>
void ReadData (T x[], int n)
{
	for (int i = 0; i < n; ++i)
		cin >> x[i];
	cout << endl;
}
template <class T>
void DisplayData (T x[], int n)
{
	for (int i = 0; i < n; ++i)
		cout << x[i] << " ";
	cout << endl;
}
template <class T1, class T2>
void ShowAve (T1 p,T2 q)
{
	cout << p << q;
	cout << endl;
}
template <class T>
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 <class T>
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 <class T1, class T2, class T3>
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.