how to handle variable length argument in c++ code example
Example: how to handle variable length argument in c++
#include <cstdarg>
#include <iostream>
using namespace std;
// this function will take the number of values to average
// followed by all of the numbers to average
double average ( int num, ... )
{
va_list arguments; // A place to store the list of arguments
double sum = 0;
va_start ( arguments, num ); // Initializing arguments to store all values after num
for ( int x = 0; x < num; x++ ) // Loop until all numbers are added
sum += va_arg ( arguments, double ); // Adds the next value in argument list to sum.
va_end ( arguments ); // Cleans up the list
return sum / num; // Returns the average
}
int main()
{
// this computes the average of 13.2, 22.3 and 4.5 (3 indicates the number of values to average)
cout<< average ( 3, 12.2, 22.3, 4.5 ) <<endl;
// here it computes the average of the 5 values 3.3, 2.2, 1.1, 5.5 and 3.3
cout<< average ( 5, 3.3, 2.2, 1.1, 5.5, 3.3 ) <<endl;
}