home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C Programming Starter Kit 2.0
/
SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso
/
tyc
/
list18_3.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-10-16
|
982b
|
45 lines
/* Functions with a variable argument list. */
#include <stdio.h>
#include <stdarg.h>
float average(int num, ...);
main()
{
float x;
x = average(10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
printf("\nThe first average is %f.", x);
x = average(5, 121, 206, 76, 31, 5);
printf("\nThe second average is %f.", x);
}
float average(int num, ...)
{
/* Declare a variable of type va_list. */
va_list arg_ptr;
int count, total = 0;
/* Initialize the argument pointer. */
va_start(arg_ptr, num);
/* Retrieve each argument in the variable list. */
for (count = 0; count < num; count++)
total += va_arg( arg_ptr, int );
/* Perform clean-up. */
va_end(arg_ptr);
/* Divide the total by the number of values to get the */
/* average. Cast the total to type float so the value */
/* returned is type float. */
return ((float)total/num);
}