forked from imnurav/Hactoberfest2021-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariadicFunction.cpp
68 lines (60 loc) · 1.26 KB
/
variadicFunction.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Kashish Ahuja
// Program for variadic function
#include<iostream>
using namespace std;
#include<stdarg.h>
void sum(int count, ...)
{
int sum=0;
va_list args;
va_start(args, count);
for(int i=0; i<count; i++)
sum += va_arg(args, int);
va_end(args);
cout<<"\nSum is: "<<sum;
}
void min(int count, ...)
{
int min, minima;
va_list args;
va_start(args, count);
minima = min = va_arg(args, int);
for(int i=0; i<count; i++)
{
min = va_arg(args, int);
if(min<minima)
minima=min;
}
va_end(args);
cout<<"\nMinimum value: "<<minima;
}
void max(int count, ...) {
int max, maxima;
va_list args;
va_start( args, count );
maxima=max=va_arg(args,int);
for(int i=0; i<count; i++)
{
max=va_arg(args,int);
if(max>maxima)
maxima=max;
}
va_end( args );
cout<<"\nMaximum value: "<<maxima;
}
int main()
{
cout<<"\n\nThree arguments: ";
sum(3,5,7,9);
min(3,5,7,9);
max(3,5,7,9);
cout<<"\n\nFive arguments: ";
sum(5,6,7,8,9,10);
min(5,6,7,8,9,10);
max(5,6,7,8,9,10);
cout<<"\n\nTen arguments: ";
sum(10,9,8,7,6,5,4,3,2,1,0);
min(10,9,8,7,6,5,4,3,2,1,0);
max(10,9,8,7,6,5,4,3,2,1,0);
return 0;
}