Still stuck on trying to expand my program to add an overloaded function to handle floating point numbers

Hi,

I'm stuck on the last part of my program. The directions are the following~

Expand the program to add an overloaded function to handle floating point numbers (i.e., doubles). Include output for one list of integers and one list of doubles. Use this function prototype: double avgx(double&, double&, int, ...);

Compile and run. You should have one function named avg, one named davg, and two functions named avgx


My code does not compile and I think I'm not declaring my function prototype correctly? If anyone could give me any help I'd greatly appreciate it.


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
#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, ...);
double avgx(double&, double&, 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, 5, 81, 92, 73, 84, 95); 
	cout << "Average: " << a << " Max: " << aMax << " Min: " << aMin << endl;

	double b, bMax, bMin;
	b = avgx(bMax, bMin, 5, 2.1, 5.2, 1.3, 9.4, 5.3); 
	cout << "Average: " << b << " Max: " << bMax << " Min: " << bMin << endl;


}

// 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 avgx(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;
	}
My code does not compile.....


Please post the compilation errors in full.

Consider learning to use the debugger - should be easy if you have an IDE.

HTH
My code does not compile and I think I'm not declaring my function prototype correctly? If anyone could give me any help I'd greatly appreciate it.


It compiles fine. It doesn't link, though, because you haven't defined the second version of avgx.

Odd to use the va_ stuff in C++ code.
Topic archived. No new replies allowed.