setting a overload function for average?

Feb 3, 2013 at 11:40pm
so I have to write a program using lists and everything works fine till I get to the overload function to take doubles from the list and have to output average max and min for floating points dont know why it doesnt work its outputting the wrong values for my double average max and min what do I need to do or change?
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
#include <iostream>
using std::cout;
using std::endl;

#include <cstdarg>

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

int main()
{
 
  cout << endl;  
  cout << "The average of 5 test scores is " << avg(5, 81, 92, 73, 84, 95) << endl;
  cout << "The average of 5 test scores is " << davg(5, 56.8, 87.9, 99.2, 76.5, 85.3) << endl;\
  
  int a, aMax, aMin;
  a = avgx(aMax, aMin, 5, 45, 62, 89, 99, 34);
  double b, bMax, bMin;
  b = avgx(bMax, bMin, 5, 67.34, 67.8, 99.9, 76.3, 83.2);
  cout << "The average of the 5 test scores is " << a << endl;
  cout << "The maximum test score is " << aMax << endl;
  cout << "The minimum test score is " << aMin << endl;
  cout << "the average of the 5 test scores is " << b << endl;
  cout << "The maximum of the 5 test scores is " << bMax << endl;
  cout << "The minimum of the 5 test scores is " << bMin << endl;
  
}
// return average of a variable length list of integers
int avg(int n, ...)// "n" is the number of numbers in the list; "..." is 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 parameter "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
    total = total + num; // increment the total
  }
    va_end(list); // close the list -- REQUIRED
	// compute and return the average

	return total / n; 
}

double davg(int n, ...)
{
  va_list list;
  double sum2 = 0.0;
  double total2 = 0;
  
  va_start (list, n);
  for (int x = 0; x < n; x++)
  { 
    sum2 = va_arg(list, double);
    total2 = total2 + sum2;
  }
    va_end(list);
    total2 = total2 / n;
    
	return total2;
}
  int avgx(int& mx, int& mn, int n, ...) // n is the number 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 parameter "n"
	int num;	// store the numbers from the list in "num" as they are "read"
	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;
  }
  double avgx(double& my, double& mz, int n, ...) // n is the number 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 parameter "n"
	double numD;	// store the numbers from the list in "num" as they are "read"
	double totalD = 0.0;
   
    for (int y = 0; y < n; y++)
     {
	   numD = va_arg(list, int); // set num equal to the next number in the list, as an int
       if ((y == 0) || (my > numD)) // update min value 
 	   my = numD;
	   if ((y == 0) || (mz < numD)) // update max value
	   mz = numD;
       totalD = totalD + numD;
	 }
	   va_end(list); // close the list -- REQUIRED
	   // compute and return the average
	   return totalD / n;
  }
Feb 3, 2013 at 11:43pm
What's it outputting (and what do you expect it to output?)

Jim
Feb 3, 2013 at 11:54pm
im expecting it to output the average of the double number b = avgx(bMax, bMin, 5, 67.34, 67.8, 99.9, 76.3, 83.2);

its outputting -1.xxxxx ^+008 and such instead of a nice double number?
Feb 3, 2013 at 11:57pm
Line 96 is pulling ints from the doubles list
Feb 3, 2013 at 11:59pm
Was the idea of this code to specifically try using the vargs feature?

If not, you'd be much better off using std::list or std::vector to pass your lists of numbers in.

Cheers,
Jim
Feb 4, 2013 at 12:02am
yes need to use the vargs function hes very specific on his guidelines used a sstream function to pull input out of a string but wouldnt let me because its not something we learned in class yet had to use cin instead and check for numerics so better stick with just vargs so how would i fix the issue so that it pulls what i want? the int average max and min output correctly though?
Feb 4, 2013 at 12:04am
ahaha so just changed my numD = va_arg(list, int); to numD = va_arg(list, double); think it solved it thanks for your help
Feb 4, 2013 at 12:06am
but would writing a whole function for double avgx be considered a overload function?
Feb 4, 2013 at 12:06am
No worries

Jim
Feb 4, 2013 at 12:08am
Yes it would; overloaded functions are functions with the same name but different parameters.
Feb 4, 2013 at 12:10am
ok sweet appreciate the quick response and help!
Topic archived. No new replies allowed.