identifier undefined

Hello! My code has one error I cannot figure out how to fix. The D_Average identifier keeps giving an error as undefined, what do I need to do to fix this? Any tips are greatly appreciated, thanks!

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
#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 T1, class T2>
void ShowAve (T1 p, T2 q)
{
	cout << p << q;
	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);
	ComputeAve(d,4,D_Average);
	ShowAve(D_Average, " is the average of numbers in array d");

	///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;
}
Last edited on
I don't know what line that is(I'm making use of a phone), in main(): in your call to showAve()

1
2
3
4
5
6
7
8
9
10
    float A_Average=ComputeAve(a,5);

    ShowAve("The average of numbers in array a is:", A_Average);
    
    //here's where the problem is: the 3rd argument D_Average was never declared
    ComputeAve(d,4,D_Average);
    
    //Alas, you used it here, again!
    ShowAve(D_Average, " is the average of numbers in array d");
Last edited on
You've defined ShowAve twice, on line 23 and line 29, I'm guessing it's a copy and paste error - that just won't do :-)

Also reiterating @Matri X:
You never even declared D_Average, but you go ahead and use it twice!
Oh wow I didn't even notice the ShowAve on there twice, oops! Thank you!
Topic archived. No new replies allowed.