### 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293`` ``````#include using std::cout; using std::endl; #include // 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.