-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSample.c
66 lines (57 loc) · 1.57 KB
/
Sample.c
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
#include <stdio.h>
#include <stdarg.h>
#include <nArgs.h>
#define minimum(...) minimum_(nArgs(__VA_ARGS__),__VA_ARGS__)
int minimum_ (int numberOfArguments, ... );
#define maximum(...) maximum_(nArgs(__VA_ARGS__),__VA_ARGS__)
int maximum_(int numberOfArguments, ... );
#define average(...) average_(nArgs(__VA_ARGS__),__VA_ARGS__)
float average_(int numberOfArguments, ... );
int main(void)
{
int n = nArgs(3,2,3,4);
printf("Number of arguments is: %d \n", n);
printf("Minimum value is : %d \n", minimum(-3,2,3,-4));
printf("Maximum value is : %d \n", maximum(-3,2,3,7,8,9,100,-4));
printf("Average value is : %.2f \n", average(-3,2,3,7,8,9,100,-4));
return 0;
}
int minimum_(int n, ... )
{
va_list variable_list;
int temp_minimum = 2147483647;
int temp;
va_start(variable_list, n);
for (int i = 0; i < n; i++) {
temp = va_arg(variable_list, int);
if(temp_minimum > temp)
{temp_minimum = temp;}
}
va_end(variable_list);
return temp_minimum;
}
int maximum_ (int n, ... )
{
va_list variable_list;
int temp_maximum = -2147483648;
int temp;
va_start(variable_list, n);
for (int i = 0; i < n; i++) {
temp = va_arg(variable_list, int);
if(temp_maximum < temp)
{temp_maximum = temp;}
}
va_end(variable_list);
return temp_maximum;
}
float average_ (int n, ... )
{
va_list variable_list;
int temp = 0;
va_start(variable_list, n);
for (int i = 0; i < n; i++) {
temp += + va_arg(variable_list, int);
}
va_end(variable_list);
return (float)temp/(float)n;
}