Created
August 13, 2014 10:04
-
-
Save shaina7837/8490a72a5eac6f6e82ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdarg> | |
using namespace std; | |
double average (int num, ...) | |
{ | |
va_list arguments; | |
va_list arg2; | |
double sum = 0; | |
int pro =0; | |
va_start (arguments, num); | |
va_start (arg2, num); | |
for (int x =0; x < num; x++) | |
{ | |
sum+= va_arg(arguments, double); | |
pro+= va_arg(arg2, int); | |
} | |
cout<< "sum = "<<sum<<endl; | |
cout<< "pro = "<<pro<<endl; | |
va_end(arguments); | |
return sum/ num; | |
} | |
int main(){ | |
average(5, 2, 5, 1.67, 6.55, 3); | |
} |
include
include
using namespace std;
double average (int num, ...)
{
va_list arguments;
va_list arg2;
double sum = 0;
int pro =0;
va_start (arguments, num);
va_start (arg2, num);
for (int x =0; x < num; x++)
{
if(x == 2 && x ==3)
sum+= va_arg(arguments, double);
else
sum+= va_arg(arg2, int);
}
cout<< "sum = "<<sum<<endl;
cout<< "pro = "<<pro<<endl;
va_end(arguments);
return sum/ num;
}
int main(){
average(5, 2, 5, 1.67, 6.55, 3);
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
/* I was thinking something like */ for (int x =0; x < num; x++) { if (i == 1) { sum+= va_arg(arguments, double); } else if (i ==2) { sum+= va_arg(arguments, int); } /* or also use switch , but I am not sure if that will work too. So we have to search "variable arguments with different types" */ }