-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariadic_function_ex.c
69 lines (55 loc) · 1.44 KB
/
variadic_function_ex.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
67
68
//
// Created by moezgen on 12/6/24.
//
#include <math.h>
#include <stdio.h>
#include <stdarg.h>
double average(double n, ...);
double sample_stddev(double n, ...);
int main(void) {
// Use 0.0 as the sentinel value to indicate the end of the arguments
double result = average(16.0, 2.0, 3.0, 4.0, 5.0);
printf("%f\n", result);
double stddev = sample_stddev(4.0, 2.0, 3.0, 4.0, 5.0);
printf("%f\n", stddev);
return 0;
}
double average(double n, ...) {
va_list ap;
double sum = 0.0;
double value = 0.0;
int count = 0;
va_start(ap, n);
// Include the first argument in the sum
sum += n;
count++;
// Loop through the remaining arguments until 0.0 is encountered
while ((value = va_arg(ap, double)) != 0.0) {
sum += value;
count++;
}
va_end(ap);
return sum / count;
}
double sample_stddev(double n, ...) {
va_list ap;
double sum = 0.0;
double value = 0.0;
// First pass: calculate the mean
va_start(ap, n);
for (int i = 0; i < n; i++) {
sum += va_arg(ap, double);
}
double mean = sum / n;
va_end(ap);
// Second pass: calculate the sum of squared differences
double sum_sq_diff = 0.0;
va_start(ap, n);
for (int i = 0; i < n; i++) {
value = va_arg(ap, double);
sum_sq_diff += (value - mean) * (value - mean);
}
va_end(ap);
// Return the standard deviation
return sqrt(sum_sq_diff / n);
}