Calculating an average using variable argument lists : Function Parameters « Function « C / ANSI-C

Home
C / ANSI-C
1.assert.h
2.Console
3.ctype.h
4.Data Structure Algorithm
5.Data Type
6.Development
7.File
8.Function
9.Language Basics
10.Macro Preprocessor
11.Math
12.math.h
13.Memory
14.Pointer
15.setjmp.h
16.signal.h
17.Small Application
18.stdio.h
19.stdlib.h
20.String
21.string.h
22.Structure
23.time.h
24.wctype.h
C Tutorial
C++
C++ Tutorial
Visual C++ .NET
C / ANSI-C » Function » Function ParametersScreenshots 
Calculating an average using variable argument lists
 Calculating an average using variable argument lists

#include <stdio.h>
#include <stdarg.h>

double average(double v1 , double v2,...);

int main()
{

  printf("\n Average = %lf", average(3.54.50.0));
  printf("\n Average = %lf", average(1.02.0));
  printf("\n Average = %lf\n", average(0.0,1.2,1.5));
}

double averagedouble v1, double v2,...)
{
  va_list parg;
  double sum = v1+v2;
  double value = 0;
  int count = 2;

  va_start(parg,v2);

  while((value = va_arg(parg, double)) != 0.0)
  {
    sum += value;
    printf("\n in averge = %.2lf", value);
    count++;
  }
  va_end(parg);                /* End variable argument process      */
  return sum/count;
}



           
       
Related examples in the same category
1.Computes the area of three triangles
2.Demonstrate the use of pointers and parameter passing
3.Pass value
4.Pass reference
5.A function to increase your salaryA function to increase your salary
6.Pass array with different dimension into function
7.Pass Array into a function
8.Use function with pointer parameters
9.Define int pointer parameter for a function
10.Pass reference of an int value into function
11.Pass char pointer into function
12.Passing the data type addess into the functionPassing the data type addess into the function
13.Length of the function parameters
14.Return value though parameterReturn value though parameter
15.Pass return value through function parameter
16.Pass array value into function: by array, by empty array and by pointer
17.Define constant function parameter
18.Char pointer as the function parameter
19.Passing parameter by pointer
20.Pass double value into functionPass double value into function
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.