Re: Passing # of Arguments
You could store the numbers in say, a std::vector and when the data collection is done, just calculate the average of the numbers in the container.
Re: Passing # of Arguments
you could write getAvg() to use a variable number of arguments, but the std::vector is a lot better and easier to implement solution for c++ programs. There must be at least one non-variable argument. and you need to devise for that function to know where the last argument is -- maybe pass -1 or something else that will not occur normally in the other arguments. for variable arguments you need to use functions in stdarg.h . Scroll down the page of that link and you will find some example programs.
Code:
int getAvg(int a, ...);
int main()
{
int n = getAvg(1,2,3,4,5,6,-1);
}
Re: Passing # of Arguments
Re: Passing # of Arguments
The problem I see is that for "the main function collects the #'s" to work, there must be some way to store the numbers. Or, if the average is the arithmetic mean, one can just sum and keep a counter of the number of numbers input, then divide. That doesnt even need a container (aside from the sum), but "average" here is rather vague.