Could someone please take a look at why my function isn't compiling?

Hi,
I'm quite stuck on this problem I'm having in my code. I'm pretty much following along on this tutorial for my computer science lab and I'm not sure what is going on with my code.

The instructions are:
1. Add this function prototype, where the first parameter is a reference to the maximum value in the list, the second is a reference to the minimum, and the third is the number of numbers in the list (represented by "..."):

int avgx(int&, int&, int, ...);

2. Add this code block to the end of main(), to compute the average of 5 integers, using numbers other than the ones listed here:

int a, aMax, aMin;
a = avgx(aMax, aMin, 5, 81, 92, 73, 84, 95);

3. After adding the above lines, add cout statements to print the average, maximum, and minimum values computed by the avgx function.


I went ahead and did all these steps but I think where I'm going wrong is where I'm adding the code block to the end of main(). Everywhere I tried adding it to it would not compile. I'm really stuck on this issue. I hope someone can give me some pointers.

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>
using std::cout;
using std::endl;

#include <cstdarg>


// function prototype(s)
int avg(int, ...); 
double davg(double, ...); // added double function
int avgx(int&, int&, int, ...);

int main()
{
	cout << "The average of 5 test scores is " << avg(5, 81, 92, 73, 84, 95) << endl;

	cout << "The average of 5 double scores is " << davg(5, 71.1, 22.3, 63.4, 94.3, 95.3) << endl;

	int a, aMax, aMin;
	a = avgx(aMax, aMin, 8, 51, 26, 12, 65, 25);

}

// return average of a variable length list of integers
int avg(int n , ...) // "n" is the numbers in the list "..." is the list
{
	va_list list; // assign the name "list" to a variable length list of integers
	va_start(list, n); // tell c__ that the list begins AFTER the argument "n"
	int num; // store the numberes from the list in "num" as they are "read"

	// create the total of "n" numbers in the list
	int total = 0; // track the total of the numbers in the list
	for (int i = 0; i < n; i ++)
	{
		num = va_arg(list, int); // set num equal to the next number in the list, as an int
		total = total + num; // increment the total
	}
	
	va_end(list); // close the list -- REQUIRED

	// compute and return the average
	return total / n;
}

double davg(double d, ...) // "d" is the numers in the list 
{
	va_list list;
	va_start(list, d);
	double num;

	double total = 0;
	for (int i = 0; i < d; i ++)
	{
		num = va_arg(list, double);
		total = total + num;
	}

	va_end(list); // close the list

	return total / d;
}


	// returns average, and passes max and min back through argument list
	int avg(int& mx, int& mn, int n, ...) // n is the numbers of numbers in the list
	{
		va_list list; // assign the name "list" to the variable length list of integers
		va_start(list, n); // tell c++ that the list begins AFTER the argument "n"
		int num; // store the numbers from the list in "num" as they are "read"

		// create the total of "n" numbers in the list
		int total = 0; // track the total of the numbers in the list
		for (int i = 0; i < n; i ++)
		{
			num = va_arg(list, int); // set num equal to the next number in the list , as an int
			if ((i == 0) || (mn > num)) // update min value
				mn = num;
			if ((i == 0) || (mx < num)) // update max value
				mx = num;
			total = total + num; // increment the total 
		}
		va_end(list); // close the list -- REQUIRED

		// compute and return the average
		return total / n;
	}


I believe lines 19/20 are my problem areas but I have no idea where to stick them.
Last edited on
Line 65:

1
2
3
	// returns average, and passes max and min back through argument list
	int /* avg */ avgx(int& mx, int& mn, int n, ...) // n is the numbers of numbers in the list
	{
Thanks so much for pointing that out. I totally overlooked that typo. However, even after I fixed avg to avgx I'm still receiving compilation errors for that line of code.

It is saying about

a = avgx(aMax, aMin, 8, 51, 26, 12, 65, 25);

'a' : undeclared identifier
'aMax' undeclared identifier
'aMin' undeclared identifier

I don't understand why it is telling me that they are undeclared when I declared all the identifiers in the function parameter

When I played around with my code a little bit and moved int a, aMax, aMin; out of the int main() function like so,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using std::cout;
using std::endl;

#include <cstdarg>


// function prototype(s)
int avg(int, ...); 
double davg(double, ...); // added double function
int avgx(int&, int&, int, ...);

int main()
{
	cout << "The average of 5 test scores is " << avg(5, 81, 92, 73, 84, 95) << endl;

	cout << "The average of 5 double scores is " << davg(5, 71.1, 22.3, 63.4, 94.3, 95.3) << endl;

	cout << avgx << endl;

}

	int a, aMax, aMin;


I receive this strange compilation message warning me that avgx function will cause runtime stack overflow.

Further more when I try and do this step "3. After adding the above lines, add cout statements to print the average, maximum, and minimum values computed by the avgx function. "

by doing cout << avgx << endl; in my main() function i get the number 012D11A0. I've been messing with this for awhile but still cannot get it to output a real number. Any help would be greatly appreciated!
Last edited on
Do you still have the original line 19 - int a, aMax, aMin; - in your code?
Without it, a, aMax and aMin are undeclared.


EDIT:

> by doing cout << avgx << endl; in my main() function i get the number 012D11A0.

cout << avgx << endl; does not call the function and then print its result. It just prints the address of the function (012D11A0 in the above example).

Revert your code to what it was in your original post at the top of this thread, with just the one change on line 65 - ie. avgx instead of avg.
Last edited on
Topic archived. No new replies allowed.